| Author |
After run() method is finished it can not be restarted again while thread reference exist into .Why?
|
Mr Anil Kumar Pandey
Greenhorn
Joined: Sep 13, 2010
Posts: 29
|
|
package com.rnd;
public class RD{
public static void main(String []args)
{
T t=new T();
Thread tt=new Thread(t);
tt.setName("tt");
tt.setPriority(Thread.MAX_PRIORITY);
System.out.println("Before run Ref --- "+tt);
tt.start();
System.out.println("After run Ref --- "+tt);
}
}
class T implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getPriority());
for(int i=0;i<5;i++){
System.out.println(i);
/*try{
Thread.sleep(1000);
}catch(InterruptedException ie){
System.out.println(ie);
}*/
}
System.out.println("Run existed --- ");
Thread.currentThread().start();
}
}
output======================
Before run Ref --- Thread[tt,10,main]
After run Ref --- Thread[tt,10,main]
tt
10
0
1
2
3
4
Run existed ---
Exception in thread "tt" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:571)
at com.rnd.T.run(RD.java:30)
at java.lang.Thread.run(Thread.java:595)
I want to know why it is giving exception : A solid reason
|
 |
Mandar Joshi
Greenhorn
Joined: Nov 21, 2007
Posts: 5
|
|
|
Remove Thread.currentThread().start(); . You cannot start a thread once it is started
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
And please UseCodeTags when posting code.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Anantha Sharma
Ranch Hand
Joined: Sep 01, 2010
Posts: 43
|
|
you can't start a stopped (completed/finished) thread as the thread state now says that it does not (cannot) execute (this is a restriction by JVM), however if you do want to reuse your threads.. you can use
this will start a reusable thread, if you add more than 10 threads into the pool then the system will reuse the previously created thread (after it finishes executing).
|
 |
Mr Anil Kumar Pandey
Greenhorn
Joined: Sep 13, 2010
Posts: 29
|
|
Mandar Joshi wrote:Remove Thread.currentThread().start(); . You cannot start a thread once it is started
I have written
Thread.currentThread().start() willingly to know reason why threads can not be restarted once run() finished
while thread's reference exist
is this reference becomes eligible for garbage collection as soon as run() finished. ??
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Mr Anil Kumar Pandey wrote:
is this reference becomes eligible for garbage collection as soon as run() finished. ??
Thread objects, like any other objects are eligible for GC when it is unreachable. A terminated thread does mean that at least one less reference to the Thread object, but the object will not be GC until it is unreachable.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: After run() method is finished it can not be restarted again while thread reference exist into .Why?
|
|
|