• Post Reply Bookmark Topic Watch Topic
  • New Topic

threading issue  RSS feed

 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider there is a program in which several threads are running.i want to modify the program in such a manner that when the main thread terminates all other threads have been terminated and i don't want to scan the file.Only i want to add some lines of code at the closing brace of main(like calling join on each of the live thread.)
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not completely sure what you mean, but have you considered making your threads daemon thread. This will keep them from keeping your process alive.

See: Thread.setDaemon
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for quick reply now i am trying to do that and ask ypu again if unsuccessful.
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Exp extends Thread{
public static void main(String args[])
{
Thread t=new Exp();
t.setName("one");
Thread t1=new Exp();
t1.setName("two");
Thread t2=new Exp();
t2.setName("three");
Thread t3=new Exp();
t3.setName("Four");
t.start();
t1.start();
t2.start();
t3.start();



//insert code here
}

public void run()
{
try
{
Thread.sleep(10000);
}catch(InterruptedException ie)
{

}
System.out.println("in thread "+Thread.currentThread().getName());
}
}

the main method should be modified in such a manner that the main thread should be the last one to terminate.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prahlad Joshi:
the main method should be modified in such a manner that the main thread should be the last one to terminate.

That is a different question than what you originally asked and what Jan's answer is useful for.

If you want the JVM to continue running until all threads have finished, you don't have to change anything to your program, because that's already what happens if you run it as it is. Note that in that case the thread that runs the main() method exits, but the JVM doesn't stop running until all threads have stopped.

If you explicitly want the thread that runs the main() method to stop last, then you'll want to call join() on the other threads after starting them in the main() method. See the API documentation of class Thread for exact details of what join() does.
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post the same question to more than one forum

following your advice i am trying to continue this topic in the SCJP forum.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!