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