Results 1 to 2 of 2
  1. #1
    Hyder is offline Member
    Join Date
    Apr 2007
    Posts
    1
    Rep Power
    0

    Default 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,

  2. #2
    ali
    ali is offline Member
    Join Date
    Apr 2007
    Posts
    7
    Rep Power
    0

    Default

    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

Similar Threads

  1. how to add main function
    By spratana in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 02:33 PM
  2. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:37 PM
  3. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 10:25 AM
  4. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •