• 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

Thread execution with 100 Threads starting the same Runnable target

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from the ePractice exams for 310-055 purchased directly from Sun Microsystems.

Given:



Which is true?

A - The output can never contain the value 10.
B - The output can never contain the value 30.
C - The output can never contain the value 297.
D - The output can never contain the value 1020.
E - The output can never contain the value 1130.

Sun's answer is:
Option E is correct. With or without the yield, and with no synchronization in place, any value up to 1100 can be displayed.

I agree with option E, but I don't agree that any value upt 1100 can be displayed. The variable i starts at 0. Any thread that starts will start by either incrementing i by 1 due to the if (i%10 != 0) condition or entering the for loop. Since the print statement is OUTSIDE of the for loop, every thread will have to increment i by a value of 9 before it can print it. Hence I don't see the values 0 to 8 being displayed.

Can someone please confirm?

Thank you!
Bonnie
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bon Thanh wrote:The variable i starts at 0. Any thread that starts will start by either incrementing i by 1 due to the if (i%10 != 0) condition or entering the for loop. Since the print statement is OUTSIDE of the for loop, every thread will have to increment i by a value of 9 before it can print it. Hence I don't see the values 0 to 8 being displayed.



I would agree with zero, but probably not agree with any other number... for example, here is how the number one would be printed.

1. Thread 1 runs first. Doesn't do anything at line 5. Does one loop. And context switches in the middle of incrementing the variable i, during reinitialization of the loop. Remember the post increment operator isn't atomic -- it is a read from memory, increment in register, and then write to memory. So... let's say it reads from memory, increments, but before it can write back to memory, a context switch occurs.

2. Thread 2 runs. Blah. Blah. Blah.

3. Thread 3 runs. Blah. Blah. Blah.

4. Thread X run. Blah. Blah. Blah. It finishes the loop, but before it load the variable from memory, to print the variable, it context switches.

5. Thread 1 runs again, and writes the one back to memory. Continues running, but context switches before it can change i again.

6. Thread X runs again, loads the variable (whose value is one) from memory, and prints it.

Henry
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, but the x value is function local, so each run() has to go through the whole for loop (no changing of x from other thread possible) before it reaches the print statement...
so i would agree that values <10 will not occur...
 
Rein de Boer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh actually i see the issue now...
the value to print (i) might be changed by a different thread right before it is printed.... So i agree with Henry Wong :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic