Article View: borland.public.jbuilder.java.api
Article #175Re: Why can I not restart a thread which had stopped?
From: Paul Nichols (TeamB)
Date: Sun, 25 Jul 2004 05:31
Date: Sun, 25 Jul 2004 05:31
81 lines
2848 bytes
2848 bytes
jinxiaojin wrote:
>
> 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();
> }
Hint: rather than mythread.start, try calling mythread.run.
Explanation: You are not overridding start(), you are overridding run(). If
you override start() in the MyThread class, then calling start() would work
as you are wishing it would.
Example:
public MyThread() {
}
public void run() {
System.out.println("Thread is running.");
System.out.println("Thread exit.");
}
public void start() {
run();
}
}
of course, another way to do this, without overridding stop() in the
MyThread class, is to call run() instead of start() in the test() method of
TestThread2.
Example:
private void test() {
mythread.start();
try {
Thread.sleep(1000l);// mythread has finish his job
}
catch(Exception ex) {
ex.printStackTrace();
}
mythread.start();
}
If you intend to use Threads to any extent, I highly suggest you read up on
the new thread standards. Quite a few methods have been changed and/or
deprecated due to thread safe concerns in the latest jdks.
Technical Details
Message-ID:
<410340be@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!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 Message-ID: <410340be@newsgroups.borland.com> From: "Paul Nichols (TeamB)" <paul@comp.net> Subject: Re: Why can I not restart a thread which had stopped? Newsgroups: borland.public.jbuilder.java.api Reply-To: paul@comp.net Mail-Copies-To: paul@comp.net Date: Sun, 25 Jul 2004 01:31:36 -0400 References: <41020976$1@newsgroups.borland.com> Lines: 87 Organization: clc User-Agent: KNode/0.7.6 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit NNTP-Posting-Host: 68.71.188.114 X-Trace: newsgroups.borland.com 1090732222 68.71.188.114 (24 Jul 2004 22:10:22 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail