Originally posted by Barkat Mardhani:
In many thread questions, I observed, it is automatically assumed that main thread will be executed first. Is that safe assumption?
The Inner that is named is not the true Inner.
The Inner that is named is not the true Inner.
The synchronized sections of code can still be accessed by two threads simultaneously. This could be responsible for the entire count. But I'm not even sure of my last try - could get a race condition between the main thread setting currentThread and the previous thread reading it. This is tricky stuff.
I gather from them that you agree that main thread can be preempted by 2nd thread more so in some situation than others.
I have not done any thread programming but some of the examples I have seen, assumed that main thread will never be preempted by 2nd thread. Is that assumption safe?
It is possible that main thread is preempted by thread b. When that happens, the program does not work logically right. It will wait for ever.
Then why this fact is ignored.
The synchronized sections of code can still be accessed by two threads simultaneously.
The Inner that is named is not the true Inner.
I should have said: The synchronized sections of code can still be accessed by two threads separately but simultaneously. As I looked at this I realized that only the main thread can access the synchronized block in the main method, and only a child thread can access its synchronized run method, so synchronization is not being used to prevent multiple threads accessing a particular piece of code, but to prevent two pieces of code from being executed simultaneously. This seems interesting and useful - is it a recognized idiom, a pattern even?
Please note though that if the common lock is the new child thread, then the main thread and the previous child thread can be executing their synchronized sections simultaneously, which is what I was trying to say. This is why I changed the code to use a single common lock.
We do not need to use an expiration time.
The Inner that is named is not the true Inner.
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|