Interthread Communication in Java

Interthread Communication in Java

Interthread Communication in Java

Interthread Communication in Java

As you know about producer-consumer problem of thread. In this problem one thread is producing data and another is consuming it, This problem become worst when produce needs to wait until the consumer is finished.

In a polling system, many CPU cycle is wasted by the consumer while waiting for the producer to produce. After finishing producer, producer wait for the consumer to finish. This would waste more CPU cycles.

To avoid wasting of cycle or polling, interthread communication is a good way.  Given below some function helpful in interthread communication :

Methods Description
wait( ) 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( ).
notify( ) This method wakes up the first thread that called wait( ) on the same object.
notifyAll( )  This method wakes up all the threads that called wait( ) on the same object.c The highest
priority thread will run first.

EXAMPLE

In the given below Example, it has four classes. The 'Queue' class is the class which we are trying to synchronize. 'Producer' class are producing queue entries. 'Consumer' Class consuming queue entries. And the class 'InterthreadDemo' which create 'Producer', and 'Consumer' and 'Queue'. Inside get( ), wait( ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready. After the data has been obtained, get( ) calls notify( ). This tells Producer that it is okay to put more data in the queue. Inside put( ), wait( ) suspends execution until the Consumer has removed the item from the queue. When execution resumes, the next item of data is put in the queue, and notify( ) is called. This tells the Consumer that it should now remove it.

package corejava;

class Queue {
int n;
boolean valueSet = false;

synchronized int get() {
if (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}

synchronized void put(int n) {
if (valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}

class Producer implements Runnable {
Queue q;

Producer(Queue q) {
this.q = q;
new Thread(this, "Producer").start();
}

public void run() {
int i = 0;
while (true) {
q.put(i++);
}
}
}

class Consumer implements Runnable {
Queue q;

Consumer(Queue q) {
this.q = q;
new Thread(this, "Consumer").start();
}

public void run() {
while (true) {
q.get();
}
}
}

public class InterthreadDemo {
public static void main(String args[]) {
Queue q = new Queue();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java InterthreadDemo
Press Control-C to stop.
Put: 0
Got: 0
Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3

The above given output is terminated forcibly by pressing Ctrl-C key.

 

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials