English 中文(简体)
我如何运用同步?
原标题:How do I apply synchronization?

我有自动数据系统自动化测试。 试验涉及三个实体:

Data object class - data to be filled in forms. Herein each form on a page could be represented by a different data object.
Helper class - which fills in data in a form on page Test class - which uses data object and helper class to perform test.

下面是测试的缩略本——

public class ParallelDataObject {

    HelperClass helperClass = new HelperClass();
    Data data;

    @BeforeMethod
    public void setTestData() {
        data = new Data();
        helperClass.setData(data);
    }

    @Test
    public void passM1() {
        helperClass.verifyFlag();
    }

    @Test
    public void failM2() {
        data.setFlag(false);
        helperClass.setData(data);
        helperClass.verifyFlag();
    }

    @Test
    public void passM3() {
        helperClass.verifyFlag();
    }

    @Test
    public void failM4() {
        data.setFlag(false);
        helperClass.setData(data);
        helperClass.verifyFlag();
    }
}

class HelperClass {
    Data data;  

    public void setData(Data data) {
        synchronized (data) {
            this.data = data;
        }
    }

    public void verifyFlag() {
        synchronized (data) {
            assert data.getFlag();
        }
    }
}

class Data {
    private boolean flag;

    public Data() {
        flag = true;
    }

    public Data setFlag(boolean flag) {
        synchronized (this) {
            this.flag = flag;
            return this;
        }
    }

    public boolean getFlag() {
        synchronized (this) {
            return flag;
        }
    }

When executing methods in parallel I encountered weird results as data is not thread safe. Then I incorporated synchronize blocks but yet I encounter weird results. I am sure I have messed up how synchronization should be used here in. Any insight?

我做了另外一次工作。 我设置了另一类试验,与第一个测试类别完全相同。 我从助手和数据类别删除了所有同步。 当我同时工作时(而不是方法)。 测试结果与预期相同。 当我同时执行课堂时,我为什么不赞成,尽管他们使用同样的助手类别和数据物体?

最佳回答

考虑使用Thread Local 。 这样,每版都有自己的<代码>HelperClass。 值得注意的是,相互协调的单独方法给你留下了任何东西——在一次试验(一对一看)中所作的改动在其他试验中可见一斑。

class ParallelDataObject {

    private final ThreadLocal<HelperClass> helperClassThreadLocal = new ThreadLocal<HelperClass>() {

        @Override
        protected HelperClass initialValue() {
            return new HelperClass(new Data());
        }
    };

    private HelperClass helperClass() {
        return helperClassThreadLocal.get();
    }

    @Test
    public void passM1() {
        helperClass().verifyFlag();
    }

    @Test
    public void failM2() {
        helperClass().getData().setFlag(false);
        helperClass().verifyFlag();
    }

}

class HelperClass {

    private final Data data;

    public HelperClass(Data data) {
        this.data = data;
    }

    public Data getData() {
        return data;
    }

    public void verifyFlag() {
        assert data.getFlag();
    }
}

class Data {
    private boolean flag = true;

    public Data setFlag(boolean flag) {
        this.flag = flag;
        return this;
    }

    public boolean getFlag() {
        return flag;
    }
}

Other improvements:

  • passM3 and failM4 were superfluous
  • since HelperClass requires an instance of Data to work, it should declare it using constructor dependency
  • 使用时:

    synchronized(this)
    

    总结整个方法体,考虑使用<代码>同步在方法申报中的关键词(可读)。

  • 不再需要与<代码>同步。 校对地方

Test statelessness

@gpeche提出检验应当独立的良好建议。 不幸的是(hy,为什么!) 日尼特重复使用所有测试方法执行相同的试样(ParallelDataObject。 这意味着,转让任何国有物体以测试个案类别领域是危险的,必须避免。

In this particular case the OP would have to create a new instance of HelperClass in each test method (which, in fact, isn t such a bad idea):

class ParallelDataObject {

    @Test
    public void passM1() {
        final HelperClass helperClass = new HelperClass(new Data());

        helperClass.verifyFlag();
    }

    @Test
    public void failM2() {
        final Data data = new Data();
        data.setFlag(false);

        final HelperClass helperClass = new HelperClass(data);

        helperClass.verifyFlag();
    }

}
问题回答

<代码>HelperClass和Data为thread-safe。

问题是,你的一些测试方法开展了若干行动。 试验方法操作的顺序只要不同步,就不是原子的。

例如,failM4 可在其他校对<代码>的状态加以修改。

d 我建议你不要在试验方法之间使用共同状态,因为同步将取消同时进行试验的好处。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签