Thread: introduction to Threads, Thread Model; Priority of Threads

Thread એ એક program ની અંદર execution નો independent path છે: 5-state life cycle, extends Thread vs implements Runnable, start() vs run(), અને priorities 1 થી 10 સુધીના hints.

12 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

desk 5 pm પર freeze થાય છે

નવું BookBridge feature: closing time પર, બધા issued books ને scan કરો અને near due date વાળા anything માટે reminders print કરો. 6000 records, લગભગ 40 seconds.

સમસ્યા: તે scan ચાલે છે ત્યારે, return desk frozen છે. program busy છે; librarian click કરે છે, કંઈ respond નથી કરતું, queue બને છે.

scan ને desk ની જરૂર નથી; તેઓ just same program માં live કરે છે, execution ના એક path પર turns લેતા. આપણે જે જોઈએ છે એ second path છે. Java એને thread કહે છે.

Theory

બે assistants, એક library

અત્યાર સુધી તમે જે દરેક program લખ્યું છે એ એક assistant ધરાવતી library છે જે everything ને sequence માં કરે છે: member ને serve કરો, પછી reminders check કરો, પછી register update કરો.

thread એ same library ની અંદર બીજો assistant hire કરે છે: same shelves, same records (same objects અને memory), simultaneously working. એક desk ને serve કરે છે, બીજો quietly shelves પર walk કરીને reminder list બનાવે છે. building walk ને finish થવા માટે wait નથી કરતું.

Theory

Thread અને તેનું life, ઔપચારિક રીતે

thread એ program ની અંદર execution નો independent path છે. દરેક Java program પાસે already એક છે: JVM main ને main thread પર run કરે છે; તમે more create કરો છો.

thread model એ life cycle છે જે દરેક thread walk કરે છે:

  • New: created, start() હજુ called નથી
  • Runnable: ready, scheduler ની turn ની waiting
  • Running: currently CPU પર executing
  • Blocked/Waiting: resource, sleep અથવા બીજા thread માટે paused
  • Dead: run() finished; dead thread ને restart નથી કરી શકાતો

Practical

reminder ને તેનો own assistant મળે છે

class Reminder implements Runnable {   // contract ને sign કરો
    public void run() {                 // thread નું task અહીં lives કરે છે
        for (int i = 1; i <= 3; i++) {
            System.out.println("Reminder check " + i);
        }
    }
}

public class BookBridge {
    public static void main(String[] args) {
        Thread t = new Thread(new Reminder());
        t.setPriority(Thread.MAX_PRIORITY);  // 10: hint, order નહીં
        t.start();                           // second path નો birth
        System.out.println("Desk keeps serving");
    }
}

Theory

બે ways in, એક clear winner

Java 2 creation routes offer કરે છે:

  • extends Thread: તમારી class IS thread છે; run() ને override કરો, start() call કરો
  • implements Runnable: તમારી class HAS task છે; તેને new Thread(task) ને hand કરો

Runnable ને prefer કરો, અને exam માં કહો શા માટે: Java classes ને એક extends slot મળે છે (single inheritance). Reminder ને કદાચ someday કોઈ બીજી class ને extend કરવાની જરૂર પડી શકે; Runnable implement કરવાથી તે slot free રહે છે. એ task (શું કરવું) ને machinery (Thread જે તેને run કરે છે) થી separate પણ કરે છે, અને છેલ્લા unit નો interface pattern living માટે work કરે છે.

Quiz

વિદ્યાર્થી t.start() ને બદલે t.run() લખે છે. program work કરે છે, તો actually શું changed?

  1. કંઈ નહીં; run() અને start() synonyms છે
  2. કોઈ new thread create નથી થયો: run() current thread પર ordinary method call જેવો execute થયો
  3. thread ran, પણ minimum priority પર
  4. Compile error: run() ને directly call નથી કરી શકાતું
Show the answer

કોઈ new thread create નથી થયો: run() current thread પર ordinary method call જેવો execute થયો

start() એ magic step છે: તે JVM ને genuinely new path of execution create કરવા માટે asks કરે છે, જે પછી તે new thread પર run() ને calls કરે છે. run() ને yourself call કરો અને તે just normal method call છે: reminder loop MAIN thread પર ચાલે છે, sequentially, desk ફરી frozen, exactly સમસ્યા જેને solve કરવા આપણે આવ્યા. output identical look કરી શકે છે, જે એટલે આ bug testing ને survive કરે છે. Options C અને D behaviour ને invent કરે છે: priority untouched છે અને call perfectly legal છે, just wrong.

Think first

કોણ first print કરે છે?

listing માં, reminder thread પાસે MAX_PRIORITY છે. tap કરતા પહેલા: શું "Reminder check 1" "Desk keeps serving" પહેલાં appear થવાની guarantee છે?

Show the answer

No. Priority (MIN_PRIORITY 1, NORM_PRIORITY 5, MAX_PRIORITY 10) એ thread scheduler ને suggestion છે, command નહીં; scheduler અને operating system હજુ decide કરે છે કે કોણ ક્યારે run કરે છે. main તેની line ને new thread ને તેની first turn મળે તે પહેલાં પણ print કરી શકે છે, અને same program ના 2 runs અલગ રીતે interleave થઈ શકે છે. exam sentence: thread priority scheduling ને influence કરે છે પણ કંઈ guarantee નથી કરતો. ક્યારેય program correctness ને priorities પર build ન કરો.

Watch out

ત્રણ thread traps

start() ને બદલે run(): કોઈ new thread નથી, silently sequential: ઉપર નું quiz, અને #1 practical bug.

dead thread ને restart કરવું: એકવાર run() complete થાય, પછી start() ફરી call કરવાથી IllegalThreadStateException throw થાય છે; તેના બદલે new Thread object બનાવો.

output order પર trust કરવું: કોઈ પણ answer જે 2 running threads ના fixed interleaving ને claim કરે છે તે wrong છે; "order is unpredictable" લખો અને તમે precise હોવ છો.

Theory

threads જેને તમે already meet કર્યા છે

BCA305-02 નું દરેક Android app તેના screens ને main thread પર run કરે છે (એને freeze કરો અને OS "app not responding" show કરે છે, same 5 pm desk problem). last unit ના StringBuffer ના synchronized methods exactly એટલે exist કરે છે કારણ કે 2 threads એક buffer ને edit કરી શકે છે. અને today ની listing નો Runnable એ Unit 2 નો interface contract છે, signed અને earning its keep. next lesson zoom out કરે છે: BookBridge ના growing pile of classes ને packages માં organise કરવું.

Summary

Key takeaways

  • thread એ એક program ની અંદર independent execution path છે; main main thread પર run થાય છે.
  • Life cycle: new, runnable, running, blocked/waiting, dead; dead threads ક્યારેય restart નથી થતા.
  • extends Thread અથવા (preferred) implements Runnable + new Thread(task) દ્વારા create કરો: extends slot ને free રાખે છે.
  • start() new thread ને birth આપે છે જે run() ને calls કરે છે; run() ને directly call કરવું એ current thread પર just method call છે.
  • Priorities: 1 (MIN), 5 (NORM), 10 (MAX), setPriority() સાથે set કરો: scheduler ને hints, ક્યારેય guarantees નહીં.
  • running threads વચ્ચે output order unpredictable છે; exams માં કહો.
  • Memory hook: start() assistant ને hire કરે છે, run() એ just તમે yourself work કરો છો.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Threads and Packages

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Thread: introduction to Threads, Thread Model; Priority of Threads · Java Programming Language · Gri-Learn