• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt in Thread

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ganesh


Many people face this doubt. search a bit and you will find many questions and explanation related to this...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....?
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is not yet cleared for me...
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Ganeshkumar cheekati
    Ranch Hand
    Posts: 362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 9708
    43
    Android Google Web Toolkit Hibernate 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 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
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 362
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think yes.
     
    Nikos Pougounias
    Ranch Hand
    Posts: 110
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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 ]
     
    Poop goes in a willow feeder. Wipe with this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic