• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

threading issue

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic