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

Concurrency - Exchanger - Thread

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 -

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!"?
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!".
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic