codebytes: Concurrency : Using the "synchronized" keyword [Java]
Let us suppose you have two fields in a class and a method that modifies the fields non-atomically.Let's say you need to increment int fields by two and you use
1 field1++;
2 Thread.yield(); //This is optional. It just says "I've done the important work, some other thread may be //selected for execution now
3 field1++;
so that the current thread gets it's operations paused in the middle while other thread is selected by the JVM
for execution. According to our assumptions, the value of field1 should always be even, otherwise exception
is to be thrown.
Read full article from codebytes: Concurrency : Using the "synchronized" keyword [Java]
No comments:
Post a Comment