Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused with join concept  RSS feed

 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m getting confused to as what exactly is the function of join. Below is the code that i used in two ways:
1st: Used with Join
2nd: Used with sleep:

Below is the code :


In both the cases I get the same output as follows :


Why is it that both the cases are giving the same output? I feel like there is no purpose to use join here
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you know about fork and join? The idea is you divide your task into several parts which can be done separately (fork) and then put them back together (join). Are you actually using them in the example above?
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The join() method called on a thread (ob2.t for example) blocks the thread making the call from going forward (here your main application thread) until the other thread finishes its processing. When you call Thread.sleep(n), you stop your current thread for about n milliseconds, then go on processing. Since your created threads take about six seconds to complete, the only effective difference in this particular example is that the Thread.sleep(10000) is probably slower. In general, you might not know how long a thread will take to finish its work, and so the join() method can be safer ... that is if it is actually important to wait until a thread finishes before doing some other work.
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:The join() method called on a thread (ob2.t for example) blocks the thread making the call from going forward (here your main application thread) until the other thread finishes its processing. When you call Thread.sleep(n), you stop your current thread for about n milliseconds, then go on processing. Since your created threads take about six seconds to complete, the only effective difference in this particular example is that the Thread.sleep(10000) is probably slower. In general, you might not know how long a thread will take to finish its work, and so the join() method can be safer ... that is if it is actually important to wait until a thread finishes before doing some other work.


All i could muster from your answer is that thread is basically unpredictable. I guess i will have to study more in order to understand the concept. and clear it. Perhaps i will try more programming. May be involving more interfaces will help, i have a plan.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, threads are unpredictable. Have you read the tutorial link I posted yesterday?
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did. I guess the concept becomes clear when I see it in a big picture or atleas a scenario big enough, but I have to study many topics before I can understand and I have to try programming few scenarios. Untill then I will have doubts. So let me try programming and debugging for couple of days. But that's for the link
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:The join() method called on a thread (ob2.t for example) blocks the thread making the call from going forward (here your main application thread) until the other thread finishes its processing. When you call Thread.sleep(n), you stop your current thread for about n milliseconds, then go on processing. Since your created threads take about six seconds to complete, the only effective difference in this particular example is that the Thread.sleep(10000) is probably slower. In general, you might not know how long a thread will take to finish its work, and so the join() method can be safer ... that is if it is actually important to wait until a thread finishes before doing some other work.


I just wanted to check one more thing if i m getting the concept right. Lets just say threads one two and three have started and are in line for the resource. At the moment ob1.t.join() executes , ob1.t is calling sleep in run method and releasing the control making way for ob2.t to execute and then ob3.t. And the process goes on in which case ob1.t comes into life again and the same process goes on. Am i getting it right.
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhananjay Deshmukh wrote:
I just wanted to check one more thing if i m getting the concept right. Lets just say threads one two and three have started and are in line for the resource. At the moment ob1.t.join() executes , ob1.t is calling sleep in run method and releasing the control making way for ob2.t to execute and then ob3.t. And the process goes on in which case ob1.t comes into life again and the same process goes on. Am i getting it right.


Yes, I think you have it right. When the main thread calls ob1.t.join(), that doesn't actually have any effect at all on the ob1.t thread. Whatever it's doing, it's going to keep doing, whether that's executing or sleeping. All the join does is tell the main thread to wait until the ob1.t thread completes it processing and sleeping entirely, before it (the main thread) continues its own processing.

If an analogy helps, let's say the boss needs some information for a call she will make to the board later in the day. She splits the work of obtaining the information into three pieces and gives one piece to each of her three employees, and she tells them to meet her in the conference room when they finish. As soon as all three return to the conference room with the results of their work, she makes her call to the board. That's like the join() method. It doesn't matter if the three employees slept for most of the time they said they were working. She still waits for all three to be done before going on.

On the other hand, she could have also set her three employees to work, and then herself have gone to sleep on the conference room table for an hour. In that case, when she wakes up, she calls the board immediately without waiting for her employees. That's like the sleep() method. If they had all finished their work by that time, then she has all the information she needs for her phone call. It's not ideal though, because the employees might have all finished their work in five minutes, and she made the board wait 55 extra minutes for no good reason. It's worse though if it took one ore more of those employees more than an hour to finish the work. In that case, the boss calls the board and then doesn't have all the information she promised them.
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:
Dhananjay Deshmukh wrote:
I just wanted to check one more thing if i m getting the concept right. Lets just say threads one two and three have started and are in line for the resource. At the moment ob1.t.join() executes , ob1.t is calling sleep in run method and releasing the control making way for ob2.t to execute and then ob3.t. And the process goes on in which case ob1.t comes into life again and the same process goes on. Am i getting it right.


Yes, I think you have it right. When the main thread calls ob1.t.join(), that doesn't actually have any effect at all on the ob1.t thread. Whatever it's doing, it's going to keep doing, whether that's executing or sleeping. All the join does is tell the main thread to wait until the ob1.t thread completes it processing and sleeping entirely, before it (the main thread) continues its own processing.

If an analogy helps, let's say the boss needs some information for a call she will make to the board later in the day. She splits the work of obtaining the information into three pieces and gives one piece to each of her three employees, and she tells them to meet her in the conference room when they finish. As soon as all three return to the conference room with the results of their work, she makes her call to the board. That's like the join() method. It doesn't matter if the three employees slept for most of the time they said they were working. She still waits for all three to be done before going on.

On the other hand, she could have also set her three employees to work, and then herself have gone to sleep on the conference room table for an hour. In that case, when she wakes up, she calls the board immediately without waiting for her employees. That's like the sleep() method. If they had all finished their work by that time, then she has all the information she needs for her phone call. It's not ideal though, because the employees might have all finished their work in five minutes, and she made the board wait 55 extra minutes for no good reason. It's worse though if it took one ore more of those employees more than an hour to finish the work. In that case, the boss calls the board and then doesn't have all the information she promised them.


Thanks i guess i have the general idea of how the join works and it will become more clear with time, i just have to practice more examples.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!