- Joined
- Jan 18, 2008
- Messages
- 1
- Reaction score
- 0
Hi,
with ref to below code why the sys out before the t.notifyAll() never gets printed on console when this program runs?
Is it because the thread entered in to wait state and never recovered and the program continues to run and does not terminate unless forcefully stopped.
whats going one can some one brief about it?
//Test.java
public class Test extends Thread {
private synchronized void amethod(Thread t) {
int i = 0;
boolean f =true;
while (f) {
i++;
System.out.println("The Thread " + t.getName()
+ " is printing the number i= " + i);
if(i==5){
f = false;
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("The Thread " + t.getName()+ "is put on wait... and now calling ..notifyAll().. on the same Thread");
// above sys out never appears on console
t.notifyAll();
}
public static void main(String argv[]) {
Test t1 = new Test(); t1.setName("T1");
Test t2 = new Test(); t2.setName("T2");
Test t3 = new Test(); t3.setName("T3");
t1.start();
t2.start();
t3.start();
}
public void run(){
System.out.println("a thread " + this.getName() +" has just started running");
amethod(this);
}
}
// below is the out put i got
a thread T1 has just started running
The Thread T1 is printing the number i= 1
The Thread T1 is printing the number i= 2
The Thread T1 is printing the number i= 3
The Thread T1 is printing the number i= 4
The Thread T1 is printing the number i= 5
a thread T2 has just started running
a thread T3 has just started running
The Thread T3 is printing the number i= 1
The Thread T3 is printing the number i= 2
The Thread T3 is printing the number i= 3
The Thread T3 is printing the number i= 4
The Thread T3 is printing the number i= 5
The Thread T2 is printing the number i= 1
The Thread T2 is printing the number i= 2
The Thread T2 is printing the number i= 3
The Thread T2 is printing the number i= 4
The Thread T2 is printing the number i= 5
with ref to below code why the sys out before the t.notifyAll() never gets printed on console when this program runs?
Is it because the thread entered in to wait state and never recovered and the program continues to run and does not terminate unless forcefully stopped.
whats going one can some one brief about it?
//Test.java
public class Test extends Thread {
private synchronized void amethod(Thread t) {
int i = 0;
boolean f =true;
while (f) {
i++;
System.out.println("The Thread " + t.getName()
+ " is printing the number i= " + i);
if(i==5){
f = false;
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("The Thread " + t.getName()+ "is put on wait... and now calling ..notifyAll().. on the same Thread");
// above sys out never appears on console
t.notifyAll();
}
public static void main(String argv[]) {
Test t1 = new Test(); t1.setName("T1");
Test t2 = new Test(); t2.setName("T2");
Test t3 = new Test(); t3.setName("T3");
t1.start();
t2.start();
t3.start();
}
public void run(){
System.out.println("a thread " + this.getName() +" has just started running");
amethod(this);
}
}
// below is the out put i got
a thread T1 has just started running
The Thread T1 is printing the number i= 1
The Thread T1 is printing the number i= 2
The Thread T1 is printing the number i= 3
The Thread T1 is printing the number i= 4
The Thread T1 is printing the number i= 5
a thread T2 has just started running
a thread T3 has just started running
The Thread T3 is printing the number i= 1
The Thread T3 is printing the number i= 2
The Thread T3 is printing the number i= 3
The Thread T3 is printing the number i= 4
The Thread T3 is printing the number i= 5
The Thread T2 is printing the number i= 1
The Thread T2 is printing the number i= 2
The Thread T2 is printing the number i= 3
The Thread T2 is printing the number i= 4
The Thread T2 is printing the number i= 5
Last edited: