Vous êtes sur la page 1sur 4

7/14/13

DeadThreads (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
latha Greenhorn Joined: Jul 17, 2002 Posts: 14

DeadThreads
posted 8/16/2000 8:25 PM

According to RHE(pg:204), once a thread is dead, it cannot be started again. But the dead thread continues to exist, and we can access its data and call its methods. Can anybody explain me with an example, how we can access data in the dead thread without creating another thread instance. Thanks in advance...

Savithri Devaraj Ranch Hand Joined: Jun 26, 2000 Posts: 103

posted 8/16/2000 9:20 PM

Originally posted by latha: According to RHE(pg:204), once a thread is dead, it cannot be started again. But the dead thread continues to exist, and we can access its data and call its methods. Can anybody explain me with an example, how we can access data in the dead thread without creating another thread instance. Thanks in advance...

A thread object is like any other object. If it has primitives/other data objects
www.coderanch.com/t/193106/java-programmer-SCJP/certification/DeadThreads 1/4

7/14/13

DeadThreads (SCJP forum at JavaRanch)

defined, they can be accessed. If it has other methods (other than the run()), they can be called with the object reference just like any other object. You just can't restart the thread as a process. HTH, Savithri

maha anna Ranch Hand Joined: Jan 31, 2000 Posts: 1467

posted 8/17/2000 5:13 AM

Latha, Just to give you an example. In this I am calling a method on a dead thread. Simillary you can access any other variables also. regds maha anna. <pre>

class SampleThread extends Thread{ public void run() { System.out.println("Hello Latha"); } public void printMe() { System.out.println("I am printing from printMe() method"); } } class Test { public static void main(String[] args) { SampleThread t1 = new SampleThread(); t1.start(); try{ Thread.sleep(1000); }catch(Exception e){} //Assuming the t1 thread has done its work by now //it is a deadThread now. t1.printMe(); // See! we can call methods on a dead thread object! //t1.start(); // But this is NOT legal. //Once started, you can't start again } }
Output Hello Latha I am printing from PrintMe() method
thomas Ranch Hand Joined: May 26, 2002

posted 8/17/2000 8:50 AM

. . . and you can even call the run() method after the thread has died i.e. you
2/4

www.coderanch.com/t/193106/java-programmer-SCJP/certification/DeadThreads

7/14/13

DeadThreads (SCJP forum at JavaRanch)

Posts: 79

can run it like any other ordinary method.

Lancy Mendonca Ranch Hand Joined: Aug 08, 2000 Posts: 54

posted 8/17/2000 3:45 PM

Hi MahaAnna, I think all your explainations are fab. The fantastic thing is U supplement all your explainations with simple code. I had a small query from your above code: Why is it not possible to call t1.start() on a dead thread

Sun C ertified Java Programmer<BR>Oracle C ertified DBA Ajith Kallambella Sheriff Joined: Mar 17, 2000 Posts: 5782

posted 8/17/2000 6:55 PM

It is just another rule of the game. Running a thread involves the thread scheduler to maintain a lot of state information about the thread itself. Once the thread has finished running, these state information needs to be discarded. Perhaps that's why Java doesnot allow you to restart a thread. Infact it is not illegal to restart a dead thread. You will not get any runtime errors if you try to do so. It just will not behave the way you expect it to, ie., a new thread will not get started and the call to start() is ignored. One thing you can do, however, is to create a new Thread object using the dead thread target. In the example code above, you can say Thread t2 = new Thread(t1); t2.start(); This is perfectly valid and it creates another thread. Just adding my 2 cents worth... Ajith

Open Group C ertified Distinguished IT Architect. Open Group C ertified Master IT Architect. Sun C ertified Architect (SC EA). Vivek Shrivastava Ranch Hand Joined: Jun 03, 2000 Posts: 277

posted 8/17/2000 10:13 PM

Hi ajith, sometime your 2 cents worth of more than 20 cents. keep doing it. regards vivek

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

www.coderanch.com/t/193106/java-programmer-SCJP/certification/DeadThreads

3/4

7/14/13

DeadThreads (SCJP forum at JavaRanch)

subject: DeadThreads

Similar Threads Thread Thread ( dead thread) When are Threads garbage collected? Question about Thread. dead thread
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:42:59 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/193106/java-programmer-SCJP/certification/DeadThreads

4/4

Vous aimerez peut-être aussi