![]() |
![]() |
|
02152 Concurrent Systems Fall 2008 |
CP Mini Lab 2: Shared variables in Java |
Home | Plan | Material |
To experience the race condition problem with shared variables in Java.
See the note about processes and threads [Proc], section 4, for a description of threads in Java.
[The volatile-modifier on a variable declaration
ensures that any change of the variable is actually written to the
variable (and not just held in a register for optimization).]
If you get the same (correct) result every time, try the following:
Note that on a multiprocessor (such as in the G-databar or a
dual-core machine), you may have to go very low and even then, the
result may not become stable. Explain this.
Enjoy!
new VIP().start();
This will start a thread that will first give itself maximal priority and then repeatedly go to sleep for 10 ms. The thread declares itself a deamon such that the program will not wait for it at termination. Thus, there is no need to save a reference to the thread object.
Hans Henrik Løvengreen, Sep 5, 2008 |