Multithreading in java is a process of executing multiple threads simultaneously.
Life Cycle
- New
- Runnable
- Running
- Non-Runnable (Blocked)
- Terminated

Create thread
- By extending Thread class
This essentially is another implementation of runnable interface.
thread runs by calling start function
1 | class Multi extends Thread{ |
- By implementing Runnable interface
1
2
3
4
5
6
7
8
9
10class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
} - By Callable and Future
To be finished.Thread method
####non-static method used frequently
- start() note that we can not start a thread twice
- run()
- (final) setName(String name)
- (final) setPriority(int priority)
3 constants defined in Thread class:- Default priority of a thread is 5 (NORM_PRIORITY)
- the value of MIN_PRIORITY is 1
- the value of MAX_PRIORITY is 10
- (final) setDaemon(boolean on)
set as daemon before start method - (final) join(long millisec)
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. - interrupt()
- (final) isAlive()
####static method - yield()
- sleep(long millisec)
The Thread class provides two methods for sleeping a thread:- public static void sleep(long miliseconds)throws InterruptedException
- public static void sleep(long miliseconds, int nanos)throws InterruptedException
- holdsLock(Object x)
- currentThread()
The currentThread() method returns a reference of currently executing thread. - dumpStack()
####Example 1 (join and sleep method)output:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}As you know well that at a time only one thread is executed. If you sleep a thread for the specified time, the thread scheduler picks up another thread and so on.1
2
3
4
5
6
7
8
9
10
11
12
13
14
151
2
3
4
5
1
1
2
2
3
3
4
4
5
5
####Example 2 (join and sleep method)output:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class TestJoinMethod2 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod2 t1=new TestJoinMethod2();
TestJoinMethod2 t2=new TestJoinMethod2();
TestJoinMethod2 t3=new TestJoinMethod2();
t1.start();
try{
t1.join(1500);
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}In the above example,when t1 is completes its task for 1500 miliseconds(3 times) then t2 and t3 starts executing.1
2
3
4
5
6
7
8
9
10
11
12
13
14
151
2
3
1
4
1
2
5
2
3
3
4
4
5
5