This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
This is not really a problem that im facing, its more of what I have come across while doing questions on synchronization.
The following code (taken from a whizlabs question but I have modified it):
Will give the result:
Current Thread is A Current Thread is A Current Thread is B Current Thread is B
Current Thread is B Current Thread is B Current Thread is A Current Thread is A
As Line1 declares a String object which is shared by any ThreadTest object. If I changed Line1 to be:
The output would be unpredictable as each ThreadTest object gets its own String object. If the Line1 declaration was static then the object is shared by each ThreadTest object so the output can be predicted.
Now if the String object was local - i.e declared in the run() will that make a difference? Well I have found that the results are the same as above.
Creating a whole new string object will cause an upredicatble output while a string object which is in the pool of strings will give a predictable output.