• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Multithreading: Sun Web Learning Center

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Courtesy of Sun Web Learning Center





Which two are true? (Choose two.)


A
The program never completes.

B
The program runs to completion.

C
The output could be 6 7 9 8.

D
The output could be 6 7 9 6.

E
The output could be 6 7 8 6 7 10.

F
The output could be 6 7 10 7 10 6.

Answer:

A and C are correct. Because there are two runnables, neither's state ever equals 3, so the else block is never executed and the threads wait forever. D is incorrect because four different threads are started and each must have a unique Id.


I can understand why A is correct; if there is a notify without a wait, the program won't complete. I am still trying to grasp this concept at depth....

However I cannot understand the following
- how are there two Runnables?
- if there are two Runnables, don't they have a separate copy of state?
- if there are two Runnables, why are four threads running, i.e 6 7 9 8?


Please clarify
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I can understand why A is correct; if there is a notify without a wait, the program won't complete.


notify won't ever be called. There are two objects of type Waiter, and two threads are executing the run method of each Waiter object. So the state variable's value won't ever reach 3.

Look at the code carafully. There are 2 runnables but 4 Threads. The two runnable objects are the Waiter objects and you are starting 4 Threads...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sandra Bachan wrote:
I am still trying to grasp this concept at depth....

However I cannot understand the following
- how are there two Runnables?
- if there are two Runnables, don't they have a separate copy of state?
- if there are two Runnables, why are four threads running, i.e 6 7 9 8?



There are two Runnable object, w1 and w2.
They w1 and w2 DO have separate copy of state variable as like any other objects in java.
Two Runnable object, and four workers(worker Threads), so four numbers as output.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:There are two Runnable object, w1 and w2.
They w1 and w2 DO have separate copy of state variable as like any other objects in java.
Two Runnable object, and four workers(worker Threads), so four numbers as output.




This will help me understand the multithreading code better - thanks!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sandra Bachan wrote:
This will help me understand the multithreading code better - thanks!



You are Welcome!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand the code. Below, I re-wrote the code such that the two runnable are executing eight threads each, and the state is being printed before and after the try block. Also, I removed the notify() and kept notifyAll() because notifyAll() will notify ALL the threads that are waiting to enter in the synchronized block, so notify() is really redundant.

Found an interesting discussion on notify() vs notifyAll() http://www.velocityreviews.com/forums/t148440-diffrence-between-notify-and-notifyall.html

Anyway, below is the modified code that may help grasp multithreading:


 
author
Posts: 9005
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sandra,

Just remember that wait() and notify() aren't on the exam any more.

The reason for that is that Sun feels the newer concurrency classes are what sold be used. From a "best practices" perspective, at this point Sun would say the only reason to understand wait() and notify() is for those times when you're dealing with legacy code.

hth,

Bert
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bert Bates wrote:Hey Sandra,

Just remember that wait() and notify() aren't on the exam any more.

The reason for that is that Sun feels the newer concurrency classes are what sold be used. From a "best practices" perspective, at this point Sun would say the only reason to understand wait() and notify() is for those times when you're dealing with legacy code.

hth,

Bert




Hi Bert,

Where can one find information about newer concurrency classes, particularly those that supercede usage of wait() and notify()?
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic