0% found this document useful (0 votes)
3 views15 pages

03&076 - Java Interthread

Uploaded by

Pratham Luthra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

03&076 - Java Interthread

Uploaded by

Pratham Luthra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inter-thread Communication and

Interrupting Thread

Submitted By: Aashna Luthra(03)


Nitakshi Mahajan(076)
Inter-thread Communication in Java

Inter-thread communication or Co-operation is all about


allowing synchronized threads to communicate with each other.

Cooperation (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.
It is implemented by following methods of Object class:

wait()
notify()
notifyAll()
1) wait() method :
The 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, so it must be
called from the synchronized method only otherwise it will throw
exception.

2) notify() method :
The 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.
Syntax:
public final void notifyAll()
Understanding the process of inter-thread
communication
The point to point explanation of the above
diagram is as follows:

Threads enter to acquire lock.

Lock is acquired by on thread.

Now thread goes to waiting state if you call wait() method


on the object. Otherwise it releases the lock and exits.

If you call notify() or notifyAll() method, thread moves to


the notified state (runnable state).

Now thread is available to acquire lock.

After completion of the task, thread releases the lock and


exits the monitor state of the object.
Why wait(), notify() and notifyAll() methods are
defined in Object class not Thread class?

It is because they are related to lock and object has a lock.


Example of Inter Thread Communication in Java:

Let's see the simple example of inter thread


communication.
class Customer{
int amount=10000;

synchronized void withdraw(int amount){


[Link]("going to withdraw...");

if([Link]<amount){
[Link]("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
}
[Link]-=amount;
[Link]("withdraw completed...");
}

synchronized void deposit(int amount){


[Link]("going to deposit...");
[Link]+=amount;
[Link]("deposit completed... ");
notify();
}
}
class Test{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){[Link](15000);}
}.start();
new Thread(){
public void run(){[Link](10000);}
}.start();

}}
Interrupting a Thread:
If any thread is in sleeping or waiting state (i.e. sleep() or wait() is
invoked), calling the interrupt() method on the thread, breaks out the
sleeping or waiting state throwing InterruptedException. If the thread is
not in the sleeping or waiting state, calling the interrupt() method
performs normal behaviour and doesn't interrupt the thread but sets
the interrupt flag to true. Let's first see the methods provided by the
Thread class for thread interruption.

The 3 methods provided by the Thread class for interrupting a thread

public void interrupt()


public static boolean interrupted()
public boolean isInterrupted()
Example of interrupting a thread that stops
working
In this example, after interrupting the thread, we are propagating it, so it
will stop working. If we don't want to stop the thread, we can handle it
where sleep() or wait() method is invoked. Let's first see the example
where we are propagating the exception.

class TestInterruptingThread1 extends Thread{


public void run(){
try{
[Link](1000);
[Link]("task");
}catch(InterruptedException e){
throw new RuntimeException("Thread interrupted..."+e);
}

}
public static void main(String args[]){
TestInterruptingThread1 t1=new TestInterruptingThread1();
[Link]();
try{
[Link]();
}
catch(Exception e)
{
[Link]("Exception handled "+e);
}
}
}
What about isInterrupted and interrupted method?
The isInterrupted() method returns the interrupted flag either true or
false. The static interrupted() method returns the interrupted flag
afterthat it sets the flag to false if it is true.

public class TestInterruptingThread4 extends Thread{

public void run(){


for(int i=1;i<=2;i++){
if([Link]()){
[Link]("code for interrupted thread");
}
else{
[Link]("code for normal thread");
}

}//end of for loop


}
public static void main(String args[]){

TestInterruptingThread4 t1=new TestInterruptingThread4


();
TestInterruptingThread4 t2=new TestInterruptingThread4
();

[Link]();
[Link]();

[Link]();

}
}
THANK YOU !

You might also like