-2

My Analysis of program run but it isn't happening that way::

-I created 2 threads namely Child1 and Child2 in main method.

-Both the threads were then started

-Child1 enters run() method as a separate thread and enters the synchronized block and prints 1 and sleeps due to wait method called on it.

-Child2 enters run() method as a separate thread and enters the synchronized block and prints 1 and notifies for Child1 to wake up.

-This process continues upto 5

package multi_threading;

public class inter_thread implements Runnable {
    static inter_thread obj;
    boolean val=false;
    Thread t;

    public inter_thread(){}
    public inter_thread(String msg){
        t=new Thread(obj,msg);
        t.start();
    }
    public static void main(String args[]){
        obj=new inter_thread();
        inter_thread obj1=new inter_thread("Child1"); 
        inter_thread obj2=new inter_thread("Child2");
        try{
            obj1.t.join();
            obj2.t.join();
        }catch(InterruptedException e){
            System.out.println("Interrupted");
        }
    }

    public void run(){
        int i;
        synchronized(obj){
        for(i=1;i<=5;i++){
            System.out.println(i);
            val=!val;
            while(val)
                try{
                    wait();
                }catch(InterruptedException e){
                    System.out.println("Interrupted");
            }
            notify();
        }
     }
    }

}

I want to display output like this using multithreading::

1
1
2
2
3
3
4
4
5
5

OUTPUT::

1
1
2

Could anyone please tell me what is the problem??

EDIT2::I have edited the previous code

tcp
  • 289
  • 3
  • 5
  • 15
  • You haven't initialized `Thread t;`. Also, you are synchronizing on `this` and you are using two different instances. You should synchronize on a shared object and call `notify` and `wait` on that object. – Sotirios Delimanolis Nov 05 '13 at 15:32
  • In addition, as both threads work on different instances of inter_thread, they don't modify the same val field. So after the first loop both threads will wait forever, as 'their' val field is always true. – isnot2bad Nov 05 '13 at 15:59

2 Answers2

0

Thread t; is not initialized.

System.out.println(t.getName()+" has "+i); // you will get Exception here

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

-Since each thread has a copy of obj I was just getting

1
1
2

as the output

--I modified the program to make object shared for the thread Child1 and Child2.

package multi_threading;

 public class inter_thread {
    Thread t;
    public inter_thread(test_value obj,String msg){
        t=new Thread(obj,msg);
        t.start();
    }

}

 class test_value implements Runnable{
     boolean val=false;
    public static void main(String args[]){
        test_value obj=new test_value();
        inter_thread obj1=new inter_thread(obj,"Child1"); 
        inter_thread obj2=new inter_thread(obj,"Child2");
        try{
            obj1.t.join();
            obj2.t.join();
        }catch(InterruptedException e){
            System.out.println("Interrupted");
        }
    }
    public void run(){
        int i;
        synchronized(obj){
        for(i=1;i<=5;i++){
            System.out.println(i);
            obj.val=!obj.val;
            while(obj.val)
                try{
                    wait();
                }catch(InterruptedException e){
                    System.out.println("Interrupted");
                }
               notify();
           }
       }
    }
 }

-I am getting a compilation error saying that obj cannot be resolved.

-How to make obj shared between threads and how to produce the output as below:

1
1
2
2
3
3
4
4
5
5
tcp
  • 289
  • 3
  • 5
  • 15