Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

After run() method is finished it can not be restarted again while thread reference exist into .Why?

 
Mr Anil Kumar Pandey
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove Thread.currentThread().start(); . You cannot start a thread once it is started
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseCodeTags when posting code.
 
Anantha Sharma
Ranch Hand
Posts: 43
Android Hibernate jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20817
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic