English 中文(简体)
寻找一个令人惊讶的并行的 Java方案
原标题:Looking for a surprising concurrent Java program

自2006年以来 我撰写了profiler,重点是concurrency方面,我正在利用synchronization mechanisms上树立一个良好的人为榜样。 我的画面显示一些与翻新相关的行动;例如:

  • calling notify/wait
  • thread changes its state
  • a thread is contended with another thread for a monitor lock
  • a monitor lock has been acquired by a thread after contending for it with another
  • measure the execution time of each method
  • which thread has accessed a certain method and how often
  • etc.

因此,我所期待的是“ Java”方案,该方案似乎在第一期上得到理解,但在执行该方案时,你开始怀疑结果。 我希望,我的简介员可能能够发现正在经历的情况。


澄清自己 我举例说,Brian Goetz著《Java Concurrency in Practice》提供了“毒性”编码实例,用于学习。

@NotThreadSafe
public class ListHelper<E> {
    public List<E> list =
        Collections.synchronizedList(new ArrayList<E>());
    ...
    public synchronized boolean putIfAbsent(E x) {
        boolean absent = !list.contains(x);
        if (absent)
            list.add(x);
        return absent;
    }
}

目的是通过的方法,推广阅读安全等级。 由于<代码>>是同步的,但putIfAbsent则使用另一锁保护国家,作为清单中界定的方法。

The profiler could display the used monitor locks and to the suprise of the user (or not...) the user would see there are two possible monitor locks instead of one.

我不像这个例子那么多,但我要提的是,我是否已经有了一些好的例子。


我发现我的问题与此类似:。 Java中遇到的最常见的一致问题是什么?

But they refer only to broken concurrent programs. I am also looking for thread-safe implementations, but where it still not obvious that they are thread-safe.

最佳回答

Have a look at the list of FindBugs bug descriptions, specifically those belonging to category of Multithreaded correctness (right table column).

以上每一段都提到某个特定异构体为何不好以及如何解决。

问题回答

象七年或七年以上的时间一样,我不时地发现一些开放源码。 仅仅就自食其力的任何事情而言,在其中会出现一些微妙的弊端,因此难以取得正确性。

How about this?

class ObjectReference {

  private volatile Object obj = null;      

  public void set(Object obj) {
    if (obj == null) {
      throw new IllegalArgumentException();
    }
    this.obj = obj;
    synchronized (this) {
      notifyAll();
    }
  }

  /**
   * This method never returns null
   */
  public Object waitAndGet() {
    if (obj != null) {
      return obj;
    }
    synchronized (this) {
      wait();
      return obj;
    }
  }
}

可在<条码>null上查阅<>waitAndGet()。 见:。 是否确实发生了假暴?

哲学家问题是一个典型一致的例子:。 这一链接有一个可能的解决办法,可在网上找到更多的解决办法。

正如第一点所述,这一例子表明了许多共同的共识问题。 请您的介绍者表明,它能够追踪多少人。

。 Java专家通讯,用于一流小 Java布,其中许多应符合你的测试需要。

我建议研究(或请作者)IBM Con 试验基准适合,因为其中含有一些 Java合力的灯泡(不宜大型开放源方案)。 这一基准的好事是,这些ug已经记录下来(类型和地点)。

如果你想找到更多的方案,我建议研究在软件测试/并行方案质量方面的一些研究论文。 他们应说明他们在研究中使用的抽样方案。

如果其他所有因素都失败,你可以尝试寻找含有必要一致性机制(即同步)的储存库。 你可能会发现大量的 Java法,因此,唯一的问题是,这些 b没有记录(除非你看上去做定点)。

我认为,这三项建议将为你提供充分的方案,以测试你的一致形象。

Maybe Eclipse Tomcat 部署? 两者都不是人为的,但我可以想象,在混淆一种或另一种工具时,会有好的工具。





相关问题
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 ...