Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-29-2007, 12:10 AM
Member
 
Join Date: Apr 2007
Posts: 1
Hyder is on a distinguished road
Problem with main
Hi,

I created a thread application but the main thread ends before the other threads.
Here is my code and its output


public class driver_thread
{
public static void main(String[] args)
{
thread t1=new thread("A");
thread t2=new thread("B");
thread t3=new thread("C");

try
{
Thread.sleep(2500);
}
catch (InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
}
}



public class thread implements Runnable
{
String name;
Thread t;

thread(String tname)
{
name=tname;
t=new Thread(this,name);
System.out.println("New Thread: "+t);
t.start();
}

public void run()
{
try
{
for (int i=5; i>0; i--)
{
System.out.println(name+": "+i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + " Interrupted");
}
System.out.println(name+" Finished");
}
}


New Thread: Thread[A,5,main]
New Thread: Thread[B,5,main]
A: 5
New Thread: Thread[C,5,main]
B: 5
C: 5
A: 4
B: 4
C: 4
A: 3
B: 3
C: 3
Main Thread Exiting
A: 2
B: 2
C: 2
A: 1
B: 1
C: 1
A Finished
B Finished
C Finished

I would appreciate any help with how to make the main thread finish in the end,
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-29-2007, 12:21 AM
ali ali is offline
Member
 
Join Date: Apr 2007
Posts: 7
ali is on a distinguished road
Hi

To stop the main thread from ending before any of the other threads, you can use two approaches

1. Increase the sleep time for main ie


public class driver_thread
{
public static void main(String[] args)
{
thread t1=new thread("A");
thread t2=new thread("B");
thread t3=new thread("C");

try
{
Thread.sleep(7500);
}
catch (InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
}
}
This code would make the main thread to last the longest and by the time it ends, all the other threads would have already finished.

You might notice that through this approach although we are ensuring that main lasts the longest, the other threads can end at 3000 mili seconds or 5000 miliseconds or they can go on till 10,000 miliseconds. In the first two cases, although the threads have ended the main keeps sleeping for a longer period of time and, in the third case, the same problem is faced again.

To avoid that, the best approach is:

public class driver_thread
{
public static void main(String[] args)
{
thread t1=new thread("A");
thread t2=new thread("B");
thread t3=new thread("C");

try
{
t1.t.join();
t2.t.join();
t3.t.join();
}
catch (InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
}
}
The join statements ensure that threads t1, t2 and t3 would end before the main thread.

Hopefully, this solves your problem.

Regards
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add main function spratana New To Java 5 04-16-2008 03:33 PM
Arguments in Main CyberFrog New To Java 2 03-30-2008 11:37 PM
main method eva New To Java 5 12-19-2007 11:25 AM
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main barney New To Java 1 08-07-2007 09:10 AM
exception in thred main java.lang.nosuchmethoderror: main fernando Java Applets 1 08-06-2007 11:11 AM


All times are GMT +3. The time now is 09:54 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org