Java Threads

Page 1

1


2


3


Getting the information about “main” thread. The “main” Thread is a Non-Daemon thread and will be alive until the main() method is executing. Once main() method is pop out of stack the “main” thread is dead. Thread.currentThread() returns a reference to the currently executing thread object, in this case it is the “main” thread Thread information when printed using toString() returns :Thread[ThreadGroup, PRIORITY, ThreadName]

4


5


6


1) Once a block of data has been read, the computation process can start, and as soon as the computation has been completed, the results can be written out. 2) If one thread is dependent on another you can’t afford to have one overtaking the other—otherwise, you’ll have chaos. This needs inter thread communication. 3) You could also organize the program so that reading a block from the file is one activity, performing the calculation is a second activity, and writing the results is a third activity.

7


8


9


10


11


12


13


14


Note: Each Thread has a separate stack, but all the threads are going to share heap area( instance variables ) and Method area( static variables).

15


16


17


18


19


20


Answer: The Thread starts, but since the TestThread class has not overridden the run() method, the thread has no task to execute. The default run() provided by the Thread class is executed and the thread dies.

21


Answer: We should invoke start() method of Thread class in order to run as a separate unit of work. Now since we are calling directly the run() method, it is a method call in main unit of work. It works like a normal method call.

Output will be “One One” followed by “Two Two”.

22


Answer: option 1

23


24


25


26


27


28


29


30


31


32


33


34


35


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.