开发者

Is there a way to ensure some statements are executed in atomic way

Having some line of statements, is there a simple way to assure it is executed i开发者_如何学Cn atomic way?


Atomic? No. Despite what people are saying here, thread-safe doesn't mean atomic:

// this is NOT atomic!
synchronized(this) {
    makeChangeA();
    makeChangeB();
}

if makeChangeB() throws an exception, makeChangeA() will not rollback it's change.

Definition of atomic is "executed either completely, or not at all". Synchronized block is not atomic.


If your emphasis is on "simple way", you can try out the @Synchronized annotation of Project Lombok.


synchronized (obj)
{
    //any other thread synchronizing against obj waits until this block is done
}

Edit:

As road to yamburg mentioned, this is not atomicity; I had assumed you simply wanted to ensure that two blocks do not overlap in execution. If in fact you are looking for an atomic action, then you need to employ the user of transactions, which are by no means easy. See Atomicity and Transaction Processing for more info.

Furthermore, if you're guaranteeing atomicity, chances are you're also looking for consistency, isolation, and durability, collectively known as the ACID properties. These are also explained on the second page in detail.


As mentioned by others, synchronized does not provide true atomicity. However, it depends on what you want to achieve? If you’re interested in mutual exclusion only, than a synchronized block may help you very well.

Some atomic operations, however, are possible. Have a look at the package java.util.concurrent.atomic, which, for example, provides atomicIntegers which may be incremented atomically and retrieved in one step.

Otherwise you’ll need to implement your own solution, like with Semaphors which halt all other threads. But note, that even if you achieve to to block all other threads of your process their might as well other processes which interfere with what you are doing (I’m speaking of other OS processes, not only those running at the JVM).

But truly, what do you want to achieve? I’m pretty sure most daily use cases can be be tackled with synchronized blocks / methods and/or the mentioned atomic-pacakge.


put them in a synchronized block, then it is atomic for the JVM.

public class MyClass {

   //the English word is synchronizer, not syncronisator
   private static final Object syncronisator = new Object();

   public void doSomething() {
      doSomethingNotSyncronized();

      synchronized(syncronisator) {
        doItAtomic1():
        doItAtomic2():
      }

      doSomethingNotSyncronized2();
   }

}

BTW: if you want to synchronize the complete method with the instance of MyClass, then you could use:

public class MyClass {

   public void synchronized doSomething() {
       doItAtomic1():
      doItAtomic2():
   }  
}

Edit: Road to yamburg is right, atomic is not only synchronization, but mean also everything or none.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜