Consider the classic queuing problem, where one thread is producing
some data and another is consuming it. To make the problem more
interesting, suppose that the producer has to wait until the consumer is
finished before it generates more data.
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
The Preceding examples unconditionally blocked other threads from Asynchronous access to certain methods.This use of the implicit monitors in java objects is powerful,But you can achieve a more subtle level of control through inter process communication.
We can say like : Inter-thread communication is all about making synchronized threads communicate with each other. Inter-thread communication is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.
Java includes an elegant inter process communication mechanism via the following methods:
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.Syntax:
Wakes up all threads that are waiting on this object's monitor.This method wakes up all the threads that called wait( ) on the same object.c The highest priority thread will run first.
The sample Program and output is
Program SynMethod.java
class Customer
{
int amount=0;
int flag=0;
public synchronized int withdraw(int amount)
{
System.out.println(Thread.currentThread().getName()+" is going to withdraw");
if(flag==0)
{
try
{
System.out.println("waiting....");
wait();
}
catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed");
return amount;
}
public synchronized void deposit(int amount)
{
System.out.println(Thread.currentThread().getName()+" is going to deposit");
this.amount+=amount;
notifyAll();
System.out.println("deposit completed");
flag=1;
}
}
public class SynMethod
{
public static void main(String[] args)
{
final Customer c=new Customer();
Thread t1=new Thread()
{
public void run()
{
c.withdraw(5000);
System.out.println("After withdraw amount is"+c.amount);
}
};
Thread t2=new Thread()
{
public void run()
{
c.deposit(9000);
System.out.println("After deposit amount is "+c.amount);
}
};
t1.start();
t2.start();
}
}
G:\>javac SynMethod.java
G:\>java SynMethod
Thread-1 is going to deposit
deposit completed
Thread-0 is going to withdraw
After deposit amount is 9000
withdraw completed
After withdraw amount is4000
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
The Preceding examples unconditionally blocked other threads from Asynchronous access to certain methods.This use of the implicit monitors in java objects is powerful,But you can achieve a more subtle level of control through inter process communication.
We can say like : Inter-thread communication is all about making synchronized threads communicate with each other. Inter-thread communication is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.
Java includes an elegant inter process communication mechanism via the following methods:
1)wait() method:
Causes current thread to release the lock and wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. The current thread must own this object's monitor.This method tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).Syntax:
public final void wait()throws InterruptedException
public final void wait(long timeout)throws InterruptedException
2)notify() method:
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.Syntax:
public final void notify()
3)notifyAll() method:
Wakes up all threads that are waiting on this object's monitor.This method wakes up all the threads that called wait( ) on the same object.c The highest priority thread will run first.
public final void notifyAll()
The sample Program and output is
Program SynMethod.java
class Customer
{
int amount=0;
int flag=0;
public synchronized int withdraw(int amount)
{
System.out.println(Thread.currentThread().getName()+" is going to withdraw");
if(flag==0)
{
try
{
System.out.println("waiting....");
wait();
}
catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed");
return amount;
}
public synchronized void deposit(int amount)
{
System.out.println(Thread.currentThread().getName()+" is going to deposit");
this.amount+=amount;
notifyAll();
System.out.println("deposit completed");
flag=1;
}
}
public class SynMethod
{
public static void main(String[] args)
{
final Customer c=new Customer();
Thread t1=new Thread()
{
public void run()
{
c.withdraw(5000);
System.out.println("After withdraw amount is"+c.amount);
}
};
Thread t2=new Thread()
{
public void run()
{
c.deposit(9000);
System.out.println("After deposit amount is "+c.amount);
}
};
t1.start();
t2.start();
}
}
Output
G:\>javac SynMethod.java
G:\>java SynMethod
Thread-1 is going to deposit
deposit completed
Thread-0 is going to withdraw
After deposit amount is 9000
withdraw completed
After withdraw amount is4000
0 comments:
Post a Comment