Java Inter Thread Communication

Java Threads are sequence of instructions which execute in parallel. In this article we will explain how two threads can share the same resource to communicate with each other (Java Inter Thread Communication). This is one of the most asked concept in Thread related Interview questions. The Inter Thread communication explains how 2 or more threads share a same resource without creating deadlock.

If you are new to Java Threads, Do check this article before you proceed.

The mechanism where the resource access is made without creating any conflicts is called thread safe. Lets understand what happens if two threads try to access a shared resource without any thread safe mechanism with an example.

Java Tutorials

Whats Synchronisation ?

The non synchronised methods / blocks are those which will not restrict the access of one resource at a time. rather it allows multiple threads to access the resource at once. This results in error in data as one is trying to read where other is trying to write to it. You can use the bellow class as an example of shared class with the producer and consumer classes mentioned bellow.

  1. class shared_unsync{
  2. int data;
  3. boolean flag = false;
  4.  
  5. public void put(int a) throws InterruptedException{
  6. while( flag == true){
  7. wait();
  8. }
  9. data = a;
  10. flag = true;
  11. notifyAll();
  12. }
  13. int get() throws InterruptedException{
  14. while( flag == false){
  15. wait();
  16. }
  17.  
  18. flag = false;
  19. notifyAll();
  20. return data;
  21. }
  22. }

Java Inter Thread Communication Example

Right way to implement the inter thread communication is by introducing thread synchronisation. A Synchronised block or method allows only one access per time, thus eliminating read/write at once. When both read and write occurs concurrently it causes error in data access. This issue can be eliminated by using a synchronised block. Here is an example where we have methods get() and put() which are synchronised, and hence these will be thread safe. In following program Producer keeps writing data where as consumer reads data. But when someone reads or writes other should wait.

  • notify() and notifyAll() are the methods which will alert other blocks waiting for the resource.
  • wait() is used wait a thread until someone calls notify().
  1. class consumer extends Thread{
  2. shared ss;
  3. consumer(shared s){
  4. ss = s;
  5. }
  6. public void run(){
  7. for(int i = 0;i<5;i++){
  8. try {
  9. Thread.sleep(2000);
  10. System.out.println(ss.get());
  11. } catch (InterruptedException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. }
  15. }
  16.  
  17. }
  18. }
  19.  
  20. class producer extends Thread{
  21. shared ss;
  22. producer(shared s){
  23. ss = s;
  24. }
  25. public void run(){
  26. for(int i = 0;i<5;i++){
  27. try {
  28. ss.put(i);
  29. Thread.sleep(2000);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40. class shared{
  41. int data;
  42. boolean flag = false;
  43.  
  44. synchronized public void put(int a) throws InterruptedException{
  45. while( flag == true){
  46. wait();
  47. }
  48. data = a;
  49. flag = true;
  50. notifyAll();
  51. }
  52. synchronized int get() throws InterruptedException{
  53. while( flag == false){
  54. wait();
  55. }
  56.  
  57. flag = false;
  58. notifyAll();
  59. return data;
  60. }
  61. }
  62.  
  63. public class Threadtest {
  64.  
  65. public static void main(String[] args){
  66.  
  67. shared s = new shared();
  68. producer p = new producer(s);
  69. consumer c = new consumer(s);
  70.  
  71. p.start();
  72. c.start();
  73.  
  74. }
  75. }

LEAVE A REPLY

Please enter your comment!
Please enter your name here