Search code examples
javaeclipsethread-synchronization

Threading and Synchronization


I need to create a java program for the following:

  1. Create an ArrayList to store the names of the employees.
  2. Create two synchronized methods to add the employee names to the ArrayList and print the employee names.
  3. Print the employee names once both the threads have finished adding the employees.

I have done the following but it is not working.It is giving an exception at line "pr.print(X)".Can anyone help? THIS IS NOT MY HOMEWORK!!! am just trying to learn.

import java.util.*;

public class Ch5Ex2 
 {
  public static void main(String[] args) 
   {
    List<String> li = new ArrayList<String>();
    Print pri = new Print();
    pri.start();
    Insert in = new Insert(li);
    in.start();
   }
}   

class Insert extends Thread
{
 Print pr = new Print();
 List<String> x;
 public Insert(List<String> x)
  {
    this.x = x;
  }

public synchronized void run()
 {
    try
    {
      x.add("robin");
      x.add("ravi");
      x.add("raj");
      pr.print(x);
    }
    catch(Exception e)
     {
        e.printStackTrace();
     }
 }

}

class Print extends Thread
{
 List<String> y;
public void print(List<String> y)
 {
    this.y = y;
    notify();
 }

public synchronized void run()
 {
    try
    {
     wait();
     for(int i=0;i<y.size();i++)
      {
         System.out.println(y.get(i));
      }
     }
    catch(Exception e)
    {
        e.printStackTrace();
    }
 }
}   

Solution

  • I Think you need this:

    import java.util.*;
    
    public class Ch5Ex2 {
        public static void main(String[] args) {
            List<String> li = new ArrayList<String>();
            Print pri = new Print();
            pri.start();
            Insert in = new Insert(li, pri);
            in.start();
        }
    }
    
    
    class Insert extends Thread {
        Print pr;
        List<String> x;
        public Insert(List<String> x, Print p) {
            this.x = x;
            pr = p;
        }
    
        public synchronized void run() {
            try{
                x.add("robin");
                x.add("ravi");
                x.add("raj");
                pr.print(x);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    class Print extends Thread {
        List<String> y;
        public synchronized void print(List<String> y) {
            this.y = y;
            notify();
        }
    
        public synchronized void run() {
            try {
                while(y == null){
                    wait();
                }
                for(int i=0;i<y.size();i++) {
                    System.out.println(y.get(i));
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Now this should work...

    Solution is: As you are doing wait() in Print thread on pri object created in main() method, you must notify() to this pri object in order to make Print thread go ahead. Also if notify() is called before Print thread starts, it will make y in Print to non-null and your Print thread will not do wait() in that case.. :)