1.Sleep():this is method that use to sleep the current thread for specified time in millisecond,this is static method in Thread class,if current thread is sleeping and again we call sleep method then it will throw Interrupted Exception,So Basically it is used to remove the current thread from runnable state for specified period of time.But it still hold the lock on object.the syntax is:
try
{
Thread.sleep(5000);
}
catch(Exception e)
{
}
2.wait():This is a method available in object class.this method is used to wait the current thread until some another thread notify.after executing this method object lock is released,this is main method that used in Synchronization.Always used wait in synchronized method or block.If we use it outside synchronized then it will return IllegalMonitorStateException because of Race condition,you can get good useful about Race condition on link http://java.sun.com/developer/Books/performance2/chap3.pdf there is different version of wait method available also, that take a time parameter,but it will not guarantee of waiting on that much time.
3.notify(): This method is also available in object Class.This method is used to notify the thread that is waiting in waiting Queue,the notified thread depend on underlying OS.Some OS notify of thread having same priority as running thread,some uses algorithm like Round robin and any thing else.So try to avoid that,Instead of notify call notifyAll() method,this should be also called from synchronized method or block.this method also release the lock
4.notifyAll():This method is also available in object class,this notifies all the thread waiting for notification in waiting Queue irrespect of their underlying platform like in notify.So I insist to use this in place of notify.This should also be used in synchronized block or method,because of same condition like in wait method. this method also release the lock.
5.join(): This method is available in Thread class.if there is two threads like a main thread and thread t1.and t1 is calling join method then the thread (Main Thread)that runs the thread (t1)that is calling join method will wait for
t1 to finish.this is implemented internally in jdk5 and jdk6 in different way,specially for windows.It is not in scope of this blog.For more internal specification see oracle docs.
6.yield():this method remove the thread status from running to idle state,Frankly speaking Don't use this method
in your real world application because you don't know when CPU resume that thread,The only purpose to distract its state,I did not find any useful practical implementation in my programming.So if anyone know the special uses please give comment.
Now on behalf of this knowledge we elaborate a synchronization problem .I will give you a complete code you understand it clearly.
First the Main Class Run.java
public class Run {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Item i=new Item();
Producer p=new Producer(i);
Consumer q=new Consumer(i);
//new Thread(p).start();
//new Thread(q).start();
}
}
then the next Producer.java class:
import java.util.Random;
public class Producer implements Runnable {
Item _item;
public Producer(Item i)
{
_item=i;
new Thread(this).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
_item.put(new Random().nextInt(_item.s.length));
}
Now Consumer.java:
public class Consumer implements Runnable {
Item temp;
public Consumer(Item i)
{
temp=i;
new Thread(this).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
temp.get();
}
}
}
Now the Most important classs Item.java:
public class Item {
String[]s={"Ashwani","Abhishek","Ashu","james","simple","john"};
boolean set=true;
String temp;
public void get()
{
synchronized (this) {
try{
if(!set)
{
System.out.println("Get Value:"+temp);
set=true;
notifyAll();
}
else
//System.out.print("Performance hit in Consumer"+"\n");
wait();
}
catch(Exception e)
{
}
}
}
public void put(int k)
{
synchronized (this) {
try {
if(set)
{
temp=s[k];
System.out.println("Set Value"+temp);
set=false;
notifyAll();
}
else
wait();
// System.out.print("Performance hit in Producer"+"\n");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
then if you run the Run.java you will see the synchronized value.This is all about synchronization in java.Any comment or suggestion is highly appreciated.
Thanks:
No comments:
Post a Comment