Multithreading in Java
Multithreading in Java
Multithreading in Java
Multitasking
Multitasking is a process of executing
multiple tasks simultaneously. We
use multitasking to utilize the CPU.
Multitasking can be achieved by two
ways:
Process-based
Multitasking(Multiprocessing)
Thread-based
Multitasking(Multithreading)
Process-based Multitasking
(Multiprocessing)
Each process have its own address in
memory i.e. each process allocates
separate memory area.
Process is heavyweight.
Cost of communication between the
process is high.
Switching from one process to another
require some time for saving and loading
registers, memory maps, updating lists etc.
Thread-based Multitasking
(Multithreading)
Threads share the same address
space.
Thread is lightweight.
Cost of communication between the
thread is low.
1)New
The thread is in new state if you create an
instance of Thread class but before the invocation
of start() method.
2) Runnable
The thread is in runnable state after invocation of
start() method, but the thread scheduler has not
selected it to be the running thread.
3) Running
The thread is in running state if the thread
scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but
is currently not eligible to run.
5) Terminated
A thread is in terminated or dead state when its
Runnable interface:
The Runnable interface should be implemented
by any class whose instances are intended to be
executed by a thread. Runnable interface have
only one method named run().
public void run():is used to perform action for a
thread.
Starting a thread:
start() methodof Thread class is used to start a
newly created thread. It performs following
tasks:A new thread starts(with new callstack).
The thread moves from New state to the
Runnable state.
When the thread gets a chance to execute, its
target run() method will run.
classMultiextendsThread{
publicvoidrun(){
System.out.println("threadisrunning...");
}
publicstaticvoidmain(Stringargs[]){
Multit1=newMulti();
t1.start();
}
}
Output:thread is running...
classMulti3implementsRunnable{
publicvoidrun(){
System.out.println("threadisrunning...");
}
publicstaticvoidmain(Stringargs[]){
Multi3m1=newMulti3();
Threadt1=newThread(m1);
t1.start();
}
}
Output:thread is running...
classTestSleepMethod1extendsThread{
publicvoidrun(){
for(inti=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedExceptione)
{System.out.println(e);}
System.out.println(i);
}
}
publicstaticvoidmain(Stringargs[]){
TestSleepMethod1t1=newTestSleepMethod1();
TestSleepMethod1t2=newTestSleepMethod1();
t1.start();
t2.start();
}
}
Output:1
1
2
2
3
3
4
4
As you know well that at a time only one thread
is executed. If you sleep a thread for the
specified time,the thread shedular picks up
another thread and so on.
No. After starting a thread, it can never be started again. If you does so,
anIllegalThreadStateExceptionis thrown. In such case, thread will run
once but for second time, it will throw exception.
EX: publicclassTestThreadTwice1extendsThread{
publicvoidrun(){
System.out.println("running...");
}
publicstaticvoidmain(Stringargs[]){
TestThreadTwice1t1=newTestThreadTwice1();
t1.start();
t1.start();
}
}
o/p: Running
Exception in thread "main" java.lang.IllegalThreadStateException
classTestCallRun2extendsThread{
publicvoidrun(){
for(inti=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedExceptione)
{System.out.println(e);}
System.out.println(i);
}
}
publicstaticvoidmain(Stringargs[]){
TestCallRun2t1=newTestCallRun2();
TestCallRun2t2=newTestCallRun2();
t1.run();
t2.run();
}
}
Output:1
2
3
4
5
1
2
3
4
5
As you can see in the above program that there is
no context-switching because here t1 and t2 will
be treated as normal object not thread object.