THREADS
Thread
A thread is lightweight.
PC client
Internet
Server
Local Area Network
PD
A
8
Best Example
Java Thread Methods
Stages of Thread Life
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 run()
method exits.
States of a Thread
New:
Declaration: public static final Thread.State NEW
Description: Thread state for a thread which has not yet
started.
Runnable:
Declaration: public static final Thread.State RUNNABLE
Description: Thread state for a runnable thread. A thread
in the runnable state is executing in the Java virtual
machine but it may be waiting for other resources from the
operating system such as processor.
Blocked:
2) extending a class
21
class MultithreadingDemo extends Thread
{
public void run()
{
try
{
// Displaying the thread that is running
System.out.println ("Thread " +
Thread.currentThread().getId() +
" is running");
}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
} }}
// Main Class
public class Multithread
{ public static void main(String[] args)
{ int n = 8; // Number of threads
for (int i=0; i<8; i++)
{ MultithreadingDemo object = new
MultithreadingDemo();
object.start();
}}
}
Output :
Thread 8 is running
Thread 9 is running
Thread 10 is running
Thread 11 is running
Thread 12 is running
Thread 13 is running
Thread 14 is running
Thread 15 is running
Implementing Runnable interface
24
Thread
class MultithreadingDemo implements Runnable{
public void run(){
try{
System.out.println ("Thread " + Thread.currentThread().getId() + " is
running");
}catch (Exception e){ Output:
System.out.println ("Exception is caught"); Thread8
} Thread9
}} Thread10
Thread11
class Multithread{ Thread12
public static void main(String[] args) { Thread13
int n = 8; Thread14
for (int i=0; i<8; i++){ Thread15
Thread object = new Thread(new MultithreadingDemo());
object.start();
} }}
Run VS Start
NORM_PRIORITY=5
MAX_PRIORITY=10
Synchronization in Java
Synchronization in java is the capability to control
the access of multiple threads to any shared
resource.
Why use Synchronization
The synchronization is mainly used to
To prevent thread interference.
To prevent consistency problem.
Thread Synchronization
There are two types of thread synchronization
mutual exclusive and inter-thread communication.
1.Mutual Exclusive
Synchronized method.
Synchronized block.
static synchronization.
2. Cooperation (Inter-thread communication in java)
Mutual Exclusive
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e)
{System.out.println(e);}
} }}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
} }
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
} }
public class TestSynchronization2{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
} }
Output:
5 Output: with out
10 synchronization
Output:
15 5
20 100
10
25 200
100 15
200 300
20
300 400
400 25
500
500
Synchronized block
yield():
Suppose there are three threads t1, t2, and t3.
Thread t1 gets the processor and starts its execution and
thread t2 and t3 are in Ready/Runnable state.
Completion time for thread t1 is 5 hour and completion
time for t2 is 5 minutes.
Since t1 will complete its execution after 5 hours, t2 has
to wait for 5 hours to just finish 5 minutes job.
In such scenarios where one thread is taking too much
time to complete its execution, we need a way to prevent
execution of a thread in between if something important
is pending.
yeild() helps us in doing so.
import java.lang.*;
thread1.start();
thread2.start();
thread3.start();
Thread Pool & Daemon Thread
wait()-It tells the calling thread to give up the lock and go to sleep until some
other thread enters the same monitor and calls notify().
notify()-It wakes up one single thread that called wait()
notifyAll()-It wakes up all the threads that called wait()
synchronized(this)
{
System.out.println("producer thread running");
wait();
System.out.println(“waiting thread");
}
MultiThread - Example