Java |
|||||||||||||||||||
Java - Thread
How to Create / Start a Thread
There are largely two different ways of creating a thread in Java as below.
And there are a few different ways of implemeting Runnable Interface as listed below.
I will shows you all the possible examples of thread creation in this section. I don't think you need much explanation for the code itself. Use these examples as a cheatsheet for syntax check when you are creating a thread in your program.
Following is an example showing how to create a thread by extending Thread class.
Output :
SampleThread is running... Waiting for 5 Seconds... SampleThread is exiting...
Following is an example showing how to create a thread by implementing Runnable Interface. This is for implementing the interface as a separate class.
Output :
SampleThread is running... Waiting for 5 Seconds... SampleThread is exiting...
Following is another example showing how to create a thread by implementing Runnable Interface. This is for implementing the interface at the time of declaration, which is called in-line thread. In this example, the thread gets excuted at the time of declaration.
Output :
SampleThread is running... Waiting for 5 Seconds... SampleThread is exiting...
Following is another example showing how to create a thread by implementing Runnable Interface. This is for implementing the interface at the time of declaration, which is called in-line thread. In this example, the thread is assigned to a variable and gets excuted later.
Output :
SampleThread is running... Waiting for 5 Seconds... SampleThread is exiting...
How to Check the Status of a Thread
Output :
SampleThread is running... Waiting for 10 Seconds... Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true Status of the Thread st = TIMED_WAITING : Is the Thread Alive = true SampleThread is exiting... Status of the Thread st = TERMINATED : Is the Thread Alive = false Status of the Thread st = TERMINATED : Is the Thread Alive = false
Output :
SampleThread1 is running... SampleThread2 is running... SampleThead2 : 0 SampleThead1 : 0 SampleThead1 : 1 SampleThead2 : 1 SampleThead2 : 2 SampleThead1 : 2 SampleThead2 : 3 SampleThead2 : 4 SampleThead1 : 3 SampleThead2 : 5 SampleThead1 : 4 SampleThead2 : 6 SampleThead2 : 7 SampleThead2 : 8 SampleThead1 : 5 SampleThead2 : 9 SampleThead2 : 10 SampleThead1 : 6 SampleThread2 is exiting... SampleThead1 : 7 SampleThead1 : 8 SampleThead1 : 9 SampleThead1 : 10 SampleThread1 is exiting...
Wait until the Thread execution complete - Thread Join
Output :
Main : 0 SampleThread2 is running... SampleThread1 is running... SampleThead2 : 0 Main : 50 SampleThead2 : 100 SampleThead1 : 0 SampleThead2 : 200 Main : 100 SampleThead2 : 300 SampleThead1 : 100 SampleThead2 : 400 Main : 150 SampleThead2 : 500 SampleThead1 : 200 SampleThead2 : 600 Main : 200 SampleThead2 : 700 SampleThead1 : 300 SampleThead2 : 800 Main : 250 SampleThead2 : 900 SampleThead1 : 400 SampleThead2 : 1000 Main : 300 SampleThread2 is exiting... SampleThead1 : 500 Main : 350 SampleThead1 : 600 Main : 400 SampleThead1 : 700 Main : 450 SampleThead1 : 800 Main : 500 SampleThead1 : 900 Main : 550 SampleThead1 : 1000 Main : 600 SampleThread1 is exiting... Main : 650 Main : 700 Main : 750 Main : 800 Main : 850 Main : 900 Main : 950 Main : 1000
Output :
SampleThread1 is running... SampleThead1 : 0 SampleThead1 : 100 SampleThead1 : 200 SampleThead1 : 300 SampleThead1 : 400 SampleThead1 : 500 SampleThead1 : 600 SampleThead1 : 700 SampleThead1 : 800 SampleThead1 : 900 SampleThead1 : 1000 SampleThread1 is exiting... SampleThread2 is running... SampleThead2 : 0 SampleThead2 : 100 SampleThead2 : 200 SampleThead2 : 300 SampleThead2 : 400 SampleThead2 : 500 SampleThead2 : 600 SampleThead2 : 700 SampleThead2 : 800 SampleThead2 : 900 SampleThead2 : 1000 SampleThread2 is exiting... Main : 0 Main : 50 Main : 100 Main : 150 Main : 200 Main : 250 Main : 300 Main : 350 Main : 400 Main : 450 Main : 500 Main : 550 Main : 600 Main : 650 Main : 700 Main : 750 Main : 800 Main : 850 Main : 900 Main : 950 Main : 1000
Output :
SampleThread1 is running... SampleThread2 is running... SampleThead2 : 0 SampleThead1 : 0 SampleThead2 : 100 SampleThead1 : 100 SampleThead2 : 200 SampleThead1 : 200 SampleThead2 : 300 SampleThead1 : 300 SampleThead2 : 400 SampleThead1 : 400 SampleThead2 : 500 SampleThead1 : 500 SampleThead2 : 600 SampleThead1 : 600 SampleThead2 : 700 SampleThead1 : 700 SampleThead2 : 800 SampleThead1 : 800 SampleThead2 : 900 SampleThead1 : 900 SampleThead2 : 1000 SampleThead1 : 1000 SampleThread2 is exiting... SampleThread1 is exiting... Main : 0 Main : 50 Main : 100 Main : 150 Main : 200 Main : 250 Main : 300 Main : 350 Main : 400 Main : 450 Main : 500 Main : 550 Main : 600 Main : 650 Main : 700 Main : 750 Main : 800 Main : 850 Main : 900 Main : 950 Main : 1000
Reference :
|
|||||||||||||||||||