Home arrow static arrow Java Programming [Archive] - Thread.isAlive() method returns alway: false
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Thread.isAlive() method returns alway: false
This topic has 3 replies on 1 page.

Posts:48
Registered: 7/26/04
Thread.isAlive() method returns alway: false  
Aug 5, 2004 3:50 AM



 
Hi all,

I've a problem with the isAlive method of the class Thread. Here is the example:
// main program                ThreadTest tt = new ThreadTest();		(new Thread(tt)).start(); 		while(true) {			System.out.println("is alive = "+tt.isAlive() );			try{Thread.sleep(1000);}catch(InterruptedException e){}		} // thread classpublic class ThreadTest extends Thread { 	public ThreadTest(){} 	public void run(){		try {			while(true){				sleep(500);				System.out.println("test");			}		}		catch(InterruptedException e){}	}}


So the proble is that isAlive always returns false!!!

What is wrong with this construction??

Luca
 

Posts:3,081
Registered: 2/15/99
Re: Thread.isAlive() method returns alway: false  
Aug 5, 2004 3:59 AM (reply 1 of 3)



 
You aren't starting "tt". You create two threads: "tt" and another "new Thread(tt)", and start the latter, not the former.
 

Posts:48
Registered: 7/26/04
Re: Thread.isAlive() method returns alway: false  
Aug 5, 2004 4:10 AM (reply 2 of 3)



 
Hi

Thanks!!

Here is something else, when I want to use wait() ont the thread I get:
java.lang.IllegalMonitorStateException        at java.lang.Object.wait(Native Method)        at java.lang.Object.wait(Object.java:426)        at ThreadTest.run(ThreadTest.java:12)


I like to stop the Thread for a monent, and when isAlive() returns false I want to call
      tt.notify();


Do you know how to do this in a way that works??

Luca
 

Posts:19,725
Registered: 9/26/01
Re: Thread.isAlive() method returns alway: false  
Aug 5, 2004 5:12 AM (reply 3 of 3)



 
Hi

Thanks!!

Here is something else, when I want to use
wait() ont the thread I get:
java.lang.IllegalMonitorStateExceptionat java.lang.Object.wait(Native Method)at java.lang.Object.wait(Object.java:426)at ThreadTest.run(ThreadTest.java:12)

Read the docs. Hint: you have to "own" the "monitor": synchronized(t) { ... t.wait() (or t.notify())... }
 
This topic has 3 replies on 1 page.