Thread State
Every thread has five states. Those are start, runnable,
running, waiting or suspend and dead.
i. A
thread comes from start state to runnable state before enter into the running
state.
ii. start()
method start the thread. In this method execution thread comes in runnable
state first and enters into running state. The JVM manages the state transition
of thread.
iii. According
to the processor execution time allotment the thread goes to wait or suspend
state.
iv. When
wait(), sleep() and join() methods are invoked then running thread shifts into
suspend or waiting state.
v. In
case of yield() method invocation the thread directly enter into runnable state
from running state.
vi. The
thread comes again runnable state before enter into running state.
vii. In
all above states thread is alive.
viii. After
completing the thread execution, thread enters into dead state. In dead state
thread is not alive.
Thread Synchronization
Synchronization is the technique to share an object
one-by-one by multiple threads. In other way, allow only single thread to
access an object at a time. Prevent an object from concurrent access of
multiple threads is called Thread-Safe.
How to synchronize?
There are two ways to synchronize
the code. Those are synchronized keyword and synchronized block.
1.
synchronize keyword
|
public class DomainClass {
public synchronized void methodName() {
// Code here
}
}
Points need to remember –
i.
synchronized keyword prevent only method from
concurrent access by threads.
ii.
synchronized is used for method only not for
class or properties.
iii.
synchronized method locks the same object at the
time of execution.
iv.
One class can contain synchronized and
non-synchronized methods.
v.
If multiple threads are using different instance
of an objects then threads accurse lock of their object instance.
vi.
Thread can get multiple if any synchronization
code invokes any other synchronized method.
2.
synchronize
block
|
public class DomainClass {
public void methodName () {
synchronized (this) {
// Code here
}
}
}
Points need to remember –
i.
Synchronized set of code in a method.
ii.
synchronized block provides multi object locking
functionality.
iii.
Provide locking object instance for synchronized
block.
iv.
‘this’ locking object indicates to lock the same
object.
Object Locking
Object is locked by thread when
executes synchronized code. When thread executes synchronized code then it
locks locking object to each other. After executing synchronized code it
releases the lock.
Thread Example#2
This example for wait() and notify() with synchronize method
function using three threads.
§
Create a domain class with synchronized block
(file name: WNDomainClass.java)
public class WNDomainClass {
private int wnPropItem;
private boolean flag = false;
public synchronized void setWNPropItem(int wnPropItem) {
if (!flag) {
try {
wait();
} catch (Exception e)
{}
}
System.out.println(Thread.currentThread().getName()
+ "
: "
+ this.getClass().getName()
+ "().setWNPropItem(int
wnPropItem): ["
+ wnPropItem + "]");
this.wnPropItem = wnPropItem;
flag = false;
notify();
}
public int getWNPropItem()
{
synchronized (this) {
if (flag) {
try {
wait();
} catch (Exception e)
{}
}
System.out.println(Thread.currentThread().getName()
+ "
: "
+ this.getClass().getName()
+ "().getWNPropItem(): ["+ wnPropItem + "]");
flag = true;
notify();
}
return this.wnPropItem;
}
}
§
Create thread class implemanting
java.lang.Runnable interface. (file name: WNMyThreadClass1.java)
public class WNMyThreadClass1 implements Runnable{
private WNDomainClass wnDomainClass;
public
WNMyThreadClass1(WNDomainClass wnDomainClass){
this.wnDomainClass=wnDomainClass;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()
+"
START ");
WNMyThreadClass2
wnMTC2=new WNMyThreadClass2(wnDomainClass);
Thread t2=new Thread(wnMTC2);
try{
t2.join();
}catch (Exception e)
{}
for(int i=0;
i<5 i="i" span="span"> 5>
wnDomainClass.setWNPropItem(i);
try{
Thread.sleep(1000);
}catch (Exception e)
{}
}
System.out.println(Thread.currentThread().getName()
+"
STOP ");
}
}
§
Create another thread class implemanting
java.lang.Runnable interface. (file name: WNMyThreadClass2.java)
public class WNMyThreadClass2 implements Runnable{
private WNDomainClass wnDomainClass;
public
WNMyThreadClass2(WNDomainClass wnDomainClass){
this.wnDomainClass=wnDomainClass;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()
+"
START ");
for(int i=0;
i<5 i="i" span="span"> 5>
wnDomainClass.getWNPropItem();
try{
Thread.sleep(1000);
}catch (Exception e) {
}
}
System.out.println(Thread.currentThread().getName()
+"
STOP ");
}
}
§
Create Main method class (file name: WNMyThreadMain.java)
public class WNMyThreadMain {
public static void main(String[]
args) {
WNDomainClass
wnDomainClass=new WNDomainClass();
WNMyThreadClass1
wnMTC1=new WNMyThreadClass1(wnDomainClass);
WNMyThreadClass2
wnMTC2=new WNMyThreadClass2(wnDomainClass);
Thread t1=new Thread(wnMTC1);
Thread t2=new Thread(wnMTC2);
t1.start();
t2.start();
}
}
§
Run and see output
Thread-0 START
Thread-1 START
Thread-1 : pack.thread.WNDomainClass().getWNPropItem(): [0]
Thread-0 : pack.thread.WNDomainClass().setWNPropItem(int
wnPropItem): [0]
Thread-1 : pack.thread.WNDomainClass().getWNPropItem(): [0]
Thread-0 : pack.thread.WNDomainClass().setWNPropItem(int
wnPropItem): [1]
Thread-1 : pack.thread.WNDomainClass().getWNPropItem(): [1]
Thread-0 : pack.thread.WNDomainClass().setWNPropItem(int
wnPropItem): [2]
Thread-1 : pack.thread.WNDomainClass().getWNPropItem(): [2]
Thread-0 : pack.thread.WNDomainClass().setWNPropItem(int
wnPropItem): [3]
Thread-1 : pack.thread.WNDomainClass().getWNPropItem(): [3]
Thread-0 : pack.thread.WNDomainClass().setWNPropItem(int
wnPropItem): [4]
Thread-0 STOP
Thread-1 STOP
No comments:
Post a Comment