Thread View: borland.public.jbuilder.java.api
5 messages
5 total messages
Started by jinxiaojin
Sat, 24 Jul 2004 07:02
Why can I not restart a thread which had stopped?
Author: jinxiaojin
Date: Sat, 24 Jul 2004 07:02
Date: Sat, 24 Jul 2004 07:02
52 lines
1861 bytes
1861 bytes
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");
}
}
Re: Why can I not restart a thread which had stopped?
Author: 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.
Re: Why can I not restart a thread which had stopped?
Author: jinxiaojin
Date: Mon, 26 Jul 2004 09:00
Date: Mon, 26 Jul 2004 09:00
3 lines
1030 bytes
1030 bytes
You are funny,and i think You do not understand my question. I wanna to restart a thread which had exit,not to print two sentences,it is only a sample code.
Re: Why can I not restart a thread which had stopped?
Author: David Orriss, Jr. [TeamB]
Date: Mon, 26 Jul 2004 17:59
Date: Mon, 26 Jul 2004 17:59
77 lines
2862 bytes
2862 bytes
You've asked about this same issue in the .language group. Please do
not multipost. It's a violation of Borland Newsgroup policies..
http://info.borland.com/newsgroups/guide.html#crossposts
On 24 Jul 2004 00:02:14 -0700, "jinxiaojin" <jinxiaojin@126.com>
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();
> }
>
> /**
> * 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");
> }
>}
--
David Orriss Jr. *TeamB*
http://www.davenet.net/
* Please limit all responses to the newsgroups. Thanks! *
Got a JBuilder website? Join the JBuilder Netring! Check out
http://www.davenet.net/jbwebring/jbwebring.htm for more information!
My blog: http://mywebpages.comcast.net/daorriss/
Save yourself some time and check these sites:
Borland Newsgroup Search:
http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's Faq-O-Matic:
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Re: Why can I not restart a thread which had stopped?
Author: 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");
> }
> }
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads