File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Thread" Watch "Doubt in Thread" New topic
Author

Doubt in Thread

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
class MyRun1 implements Runnable
{
public void run()
{
System.out.println("run by"+" "+Thread.currentThread().getName());
}
}
public class MyThread
{
public static void main(String[] a)
{
MyRun1 m=new MyRun1();
Thread t=new Thread(m);
Thread t1=new Thread(m);
Thread t2=new Thread(m);
t.setName("a");
t1.setName("b");
t2.setName("c");
t.start();
t1.start();
t2.start();
try
{
t.join();
}
catch(InterruptedException e)
{ }
}
}

when i run this program i am getting output as

first time:

run by b
run by a
run by c

second time:

run by a
run by c
run by b


it is unpredictable output.but how join() method is working in this program?

can you explain it please?

Thanks in advance...


SCJP5 and SCWCD1.5
Think Twice Act Wise...
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
Hi Ganeshkumar,
Here join() ensures that t must be finish before the current thread which was at that time t2.So a will always printed before c.
[ October 15, 2008: Message edited by: subhasish nag ]

Thanks,<br />Subhasish
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Howdy Ganesh


Many people face this doubt. search a bit and you will find many questions and explanation related to this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i think that:

threads which are in runnable state will go to running state depending upon the thread scheduler.
that means we cant predict the ouput.

when thread is completed the thread scheduler can give priority to the threads which are not running yet in any manner.


is it correct....?
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Originally posted by subhasish nag:

Here join() ensures that t must be finish before the current thread which was at that time t2.So a will always printed before c.


The current thread will be main not t2.
Here t.join() means main thread will wait till the time thread t finishes.


Thanks,<br />Srilatha M
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Originally posted by Ganeshkumar cheekati:

when thread is completed the thread scheduler can give priority to the threads which are not running yet in any manner.

is it correct....?


Could you please specify what you meant by which are not running yet in any manner.?

When the current executing thread has completed, thread scheduler will choose one from the threads which are in runnable state. And we cannot say which one thread scheduler will choose. It all depends on the implementation of thread scheduler which is platform dependant.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
which are not running yet in any manner means we cant predict which will choose for running by thread scheduler..

if i am not using join() method in the above code then i am getting the same output then what is the need of join method?
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Srilatha nails it.
The output in the original post is completely undefined, since the main thread blocks on thread-a and there are not print statements after the join method.
I suggest the original poster to read up on this topic. This type of question has been discussed many many times and can be found in this forum if someone browses history threads.


Preparing for SCJP 6.0
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
it is not yet cleared for me...
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
Hi Ganesh,

There are four threads running here:
  • main
  • t
  • t1
  • t2


  • Of course, the output cannot be determined by any means. So a, b, c may be printed with any order.

    What about join?

    The following statement means that main should pause until t completes.



    Since there are no more statements after this call, it has actually no effect.

    You should place another System.out.println() statement at the end of the main method and see the result!

    Thanks


    Nikos' Java blog
    Ganeshkumar cheekati
    Ranch Hand

    Joined: Oct 13, 2008
    Posts: 362
    yeah i put System.out.println statement at the end of the main method.

    class MyRun1 implements Runnable
    {
    public void run()
    {
    System.out.println("run by"+" "+Thread.currentThread().getName());
    }
    }
    public class MyThread
    {
    public static void main(String[] a)
    {
    MyRun1 m=new MyRun1();
    Thread t=new Thread(m);
    Thread t1=new Thread(m);
    Thread t2=new Thread(m);
    t.setName("a");
    t1.setName("b");
    t2.setName("c");
    //t.setPriority(1);
    //t1.setPriority(5);
    //t2.setPriority(10);
    t.start();
    t1.start();
    t2.start();
    try
    {
    t.join();
    }
    catch(InterruptedException e)
    { }
    System.out.println("main method....");
    }
    }

    it is coming at last in output.

    output is:

    run by a
    run by c
    run by b
    main method....

    or

    run by c
    run by a
    run by b
    main method....

    my doubt is that :

    the current thread(main thread) has to stop executing until the thread to which it is joined sud b completed...

    in program

    i am joining current thread to the thread a.
    that means after completing the thread a the current thread has to execute..

    but here i am not getting..

    can anyone clear my doubt?
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9291
        
      17

    Originally posted by Ganeshkumar cheekati:
    output is:

    run by a
    run by c
    run by b
    main method....

    my doubt is that :

    the current thread(main thread) has to stop executing until the thread to which it is joined sud b completed...

    in program

    i am joining current thread to the thread a.
    that means after completing the thread a the current thread has to execute..

    but here i am not getting..


    can anyone clear my doubt?[/QB]


    You are getting it dude. the output contains "main method", this signifies that main method stopped when you called t.join, and when t finished execution, then main started again and "main method" was displayed...
    subhasish nag
    Ranch Hand

    Joined: Apr 25, 2008
    Posts: 101
    you call the join method at last.If the other thread already finished the work before the join call then the currently running thred will be from left of them. And so that some threads completes even before a. If you put the t.join() just after t.start() then a will always print first.
    Ganeshkumar cheekati
    Ranch Hand

    Joined: Oct 13, 2008
    Posts: 362
    but main thread has to execute after completing the a thread.
    then println statement should come after run by a in output.
    why it is not displaying like that?
    instead of that it is executed after the execution of all the threads..
    Ganeshkumar cheekati
    Ranch Hand

    Joined: Oct 13, 2008
    Posts: 362
    hi nag.i put join method after the t.start() then in the output
    run by a is printed first its ok

    but in some executions i am getting output as

    run by a
    run by c
    main method....
    run by b


    here main thread has to execute after the run by a because the current thread(main thread) join with the thread 'a'.

    can you explain?
    subhasish nag
    Ranch Hand

    Joined: Apr 25, 2008
    Posts: 101
    join only ensures the thread it joins will exucute before current thread.That the output is reflecting. No other execution it ensures.
    Ganeshkumar cheekati
    Ranch Hand

    Joined: Oct 13, 2008
    Posts: 362
    When one thread calls the join() method of another thread, the currently
    running thread will wait until the thread it joins with has completed.
    Think
    of the join() method as saying, "Hey thread, I want to join on to the end
    of you. Let me know when you're done, so I can enter the runnable state."


    can you explain it?
    Ganeshkumar cheekati
    Ranch Hand

    Joined: Oct 13, 2008
    Posts: 362
    so when join method is used the current thread(main thread) stopped and waits until the thread(a) to which it join will complete.

    if the thread-a completes its execution then main thread go to runnable state.mean while the other threads can go for running state.

    is it reason for that output?

    run by a
    run by c
    run by b
    main thread....


    so it is not gaurantee that main thread should execute immediately after the execution of thread-a...

    is it correct?
    subhasish nag
    Ranch Hand

    Joined: Apr 25, 2008
    Posts: 101
    I think yes.
    Nikos Pougounias
    Ranch Hand

    Joined: Jan 16, 2008
    Posts: 110
    Originally posted by Ganeshkumar cheekati:
    so it is not gaurantee that main thread should execute immediately after the execution of thread-a...


    That's correct, because we can never know the exact behavior of the Scheduler. After thread-a executes, the main thread may run or another thread that is still waiting.

    For sure, the main thread is going to continue its execution after thread a; this is a guaranty.
    [ October 18, 2008: Message edited by: Nikos Pugunias ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt in Thread
     
    Similar Threads
    How to make one thread die before another thread
    Needs Explanation
    thread priority
    deadlock?
    Synchronization problem