Article View: borland.public.jbuilder.java.api
Article #179Re: Why can I not restart a thread which had stopped?
From: Kevin Dean
Date: Wed, 28 Jul 2004 00:47
Date: Wed, 28 Jul 2004 00:47
74 lines
3008 bytes
3008 bytes
It's not explicitly stated in the Thread class documentation, but once the
"run()" method returns, the thread is dead and may not be restarted. If you
want to see for yourself, look at the Thread source; you'll see that the
exit() routine, which is documented as being called internally by the
system, clears what would otherwise be critical information necessary for
the running of the thread. To restart a thread, you have to create a new
one.
P.S. "start()" is the correct method to call.
--
Check out our latest white papers at
http://www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
http://www.datadevelopment.com/
"jinxiaojin" <jinxiaojin@126.com> wrote in message
news:41020976$1@newsgroups.borland.com...
>
> I wrote follow codes,and found this thread only run once
> I hope this thread run twice,and i think the program will print:
> Thread is running
> Thread exit
> Thread is running
> Thread exit
>
> In fact ,it is :
> Thread is running
> Thread exit
>
>
>
>
> public class ThreadTest2 {
> MyThread mythread;
> /** Creates a new instance of ThreadTest2 */
> public ThreadTest2() {
> mythread = new MyThread();
> test();
> }
>
> private void test() {
> mythread.start();
> try {
> Thread.sleep(1000l);// mythread has finish his job
> }
> catch(Exception ex) {
> ex.printStackTrace();
> }
> mythread.start();
> }
>
> /**
> * test main method
> */
> static public void main(String[] args) {
> ThreadTest2 test = new ThreadTest2();
> }
> }
>
> class MyThread extends Thread {
> public MyThread() {
>
> }
>
> public void run() {
> System.out.println("Thread is running");
> System.out.println("Thread exit");
> }
> }
Technical Details
Message-ID:
<4106f78c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!proxad.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!demon!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41020976$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1089d0,643a33a4ab191a18 X-Google-Attributes: gid1089d0,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Kevin Dean" <NkOdSePaAnM@datadevelopment.com> Newsgroups: borland.public.jbuilder.java.api References: <41020976$1@newsgroups.borland.com> Subject: Re: Why can I not restart a thread which had stopped? Date: Tue, 27 Jul 2004 20:47:06 -0400 Lines: 76 Organization: Dolphin Data Development Ltd. X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 24.57.150.243 Message-ID: <4106f78c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1090975628 24.57.150.243 (27 Jul 2004 17:47:08 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!proxad.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!demon!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail