以原子方式执行代码块 [英] executing block of code atomically

查看:183
本文介绍了以原子方式执行代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何回答以下问题?


java类的方法包含一段必须为$ b $的代码块b原子地执行。使用适当的伪代码解释如何
将确保这个代码块以原子方式执行

A method of a java class contains a block of code that must be executed atomically. Explain, using appropriate pseudo-code, how you would ensure that this block of code is executed atomically

我能实现吗?通过制作方法..

Would I achieve this by making the method ..

public final AtomicInteger x = new AtomicInteger(0);

然后确保返回get语句:

then ensuring the get statement returned:

x.get()

如果我想增加x的值我会这样做吗?

and if I wanted to increment the value of x would I do this?

x.getAndIncrement();


推荐答案

答案取决于您对的定义原子

我知道 atomic 的三个有效定义:

I know of three valid definitions for atomic:


  1. 同步中的原子:一次只有一个线程可以执行代码;

  2. ACID中的Atomic:全部动作/阻止发生,或者没有发生;

  3. Atomic as uninterruptible:一旦块启动,它就不会被中断,即使是通过任务切换。

第一个可能是你教授的意思,而且很容易实现(见下文)。

The first is probably what your professor meant, and it's pretty easy to accomplish (see below).

第二个(ACID中的原子)可以近似。请参阅下文。

The second (atomic as in ACID) can be approximated. See below.

第三个在Java中无法保证 - 它不提供对不间断性所需的关键部分原语的访问。幸运的是,对此的需求几乎仅限于操作系统和设备驱动程序。

The third simply cannot be guaranteed in Java - it doesn't provide access to the "critical sections" primitives required for uninterruptibility. Fortunately, the need for this is pretty much restricted to operating systems and device drivers.

同步中的原子

这是相对简单的:只需将代码块包含在同步块中即可。我已将它显示为下面的离散块,但还有其他选项:

This is relatively straightforward: simply enclose your block of code in a synchronized block. I've shown it as a discrete block below, but there are other options:

public void doSomethingQuasiAtomic() {
   synchronized (exampleLock) {
      // Your code block goes here. 
      // Only one thread will ever be in this block at a time.
      ...
   }
}

Atomic如在ACID

ACID 原子性没有一般情况解决方案,但可以近似,也使用同步代码。为了做到这一点,行动的每个部分必须是安全可逆的。

There's no general-case solution for ACID atomicity, but it can be approximated, also using synchronized code. In order to do this, each of the parts of the action must be safely reversible.

这就是我接近它的方式:

This is how I'd approach it:

为了论证,假设你需要对我们称之为 exampleObj 的对象进行多部分操作,你有三个动作要做执行安全反转,并且示例的所有访问都在 exampleLock 。

For the sake of argument, assume there's a multipart action you need to do on an object we'll call exampleObj, that you have three actions to be performed which can be safely reversed, and that all access to example is synchronized on exampleLock.


    synchronized(exampleLock) {
        boolean actionOneDone=false;
        boolean actionTwoDone=false;
        boolean actionThreeDone=false;
        try {
            actionOneDone=doActionOne(exampleObj);    // or perhaps exampleObj.doActionOne();
            actionTwoDone=doActionTwo(exampleObj);
            actionThreeDone=doActionThree(exampleObj);
        } catch (Exception ex) {
            // Whatever seems appropriate here.
        } finally { 
            if (! (actionOneDone && actionTwoDone && actionThreeDone)) {
                /* At least one part failed.  Back out the completed actions in reverse order.  
                 * Note that we never need to reverse action three since if it completed, so did the others.
                 */
                if (actionTwoDone) {
                   reverseActionTwo(exampleObj);    // or perhaps exampleObj.reverseActionTwo();
                }
                if (actionOneDone) {
                   reverseActionOne(exampleObj);
                }
            }
        }
    }

这篇关于以原子方式执行代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆