Page 160 - CTS - CSA TP - Volume 2
P. 160

COMPUTER SOFTWARE APPLICATION - CITS




              •  A new class named ThreadExample is defined with a main method.
              •  Inside the main method:
              •  Two instances of MyRunnable are created with different messages.
              •  Two Thread instances (t1 and t2) are created, each initialized with a different MyRunnable instance and a
                 thread name.
              •  The start method is called on each Thread instance, initiating the execution of the run method in a separate
                 thread.
           Step 3: Execution


























              •  When the program runs, two threads (t1 and t2) are created, and each executes its run method independently.
              •  The run method contains a loop that prints the thread name, the specified message, and a loop variable
                 from 0 to 4.

              •  As a result, you’ll see interleaved output from both threads, showing the concurrent execution.







































                                                           145
 CITS : IT & ITES - Computer Software Application - Exercise 103  CITS : IT & ITES - Computer Software Application - Exercise 103
   155   156   157   158   159   160   161   162   163   164   165