Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrency - Exchanger - Thread  RSS feed

 
Waylon Wolf
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying concurrency. I tried to run the code below and the results are undetermined for which Thread comes first.
The book explained about the indeterministic behavior of threads but for this specific codes, it did not say why the threads are not in
order. Can anyone explain why?

Source -

 
Stephan van Hulst
Saloon Keeper
Posts: 7693
139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you expect the threads to run in order?

What's stopping the coffee shop from saying "Who's there?" before Duke says "Knock Knock!"?
 
Zachary Griggs
Ranch Hand
Posts: 82
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When running threads, there is no guarantee of any sort of ordering. You'll receive different orderings when you run this multiple times. Both of these were from running the app:
Duke: Who's there?
Coffee shop: Knock Knock!
Coffee shop: Duke
Duke: Duke who?

Coffee shop: Knock Knock!
Duke: Who's there?
Duke: Duke who?
Coffee shop: Duke


As to why: because the operating system decides arbitrarily what threads to run. There's really no predictable order that the operating system will run your threads. I tried to get this code working for some time, and was unable to get the threads to print in order. Due to the use of Exchanger, normal locking or synchronizing will not work. Threading is a difficult topic! Does the book have a solution that prints in order?
 
Waylon Wolf
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book doesn't give the solution. The book is only explaining how to use Exchanger and I thought the first thread to start would execute the run() method first.
 
Piet Souris
Rancher
Posts: 1941
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to get it done with the Exchanger class as well, but I had to use quite ugly tricks to make sure that the "Knock Knock" thread always executed first, no matter in which order they were started, and I didn't manage to get it 100% reliable.

This problem is much easier solved with a SynchronousQueue and an ExcecutorService with two Threads. Send both Threads (better: Runnables) to the service and let the service start them. It doesn't matter if the first Runnable is blocking, waiting for the second Runnable, since the second Runnable will be executed anyway.

The classic example is the producer-consumer problem, but you can write a class, implementing Runnable, that can play both roles, and so it doesn't matter which of the two starts first.

 
Stephan van Hulst
Saloon Keeper
Posts: 7693
139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waylon Wolf wrote:I thought the first thread to start would execute the run() method first.

Nope. Most of the time, it probably will, but there is no guarantee. It depends on a lot of factors. Let's just imagine for now that when you start two threads, the first one just happens to run on a very very slow processor. Even if it does start first, the second one might overtake it and print "Who's there?" before the first one prints "Knock Knock!".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!