Cooperation(Inter-thread communication) is all about making synchronized threads 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:
public final void wait() throws InterruptedException public final void wait(long timeout)throws InterruptedException
public final void notify()
public final void notifyAll()
wait() | sleep() |
---|---|
called from synchronized block | no such requirement |
monitor is released | monitor is not released |
awake when notify() or notifyAll() method is called. | not awake when notify() or notifyAll() method is called |
not a static method | static method |
wait() is generally used on condition | sleep() method is simply used to put your thread on sleep. |
Thread Pooling
Pooling is usually implemented by loop i.e to check some condition repeatedly. Once condition is true appropriate action is taken. This waste CPU time.
Deadlock:
Deadlock is a situation of complete Lock, when no thread can complete its execution because lack of resources. In the above picture, Thread 1 is holding a resource R1, and need another resource R2 to finish execution, but R2 is locked by Thread 2, which needs R3, which in turn is locked by Thread 3. Hence none of them can finish and are stuck in a deadlock.
Example of Inter thread Communication:
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; System.out.println("deposit completed"); notifyAll(); flag=1; } } public class SyncThreadDemo { 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.setName("Dinesh"); t2.setName("Sweety"); t1.start(); t2.start(); } }
Output:
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…