English 中文(简体)
2. 与Join 一起守卫区
原标题:Guarded Blocks with Join

我需要协调几个线。 我没有 create子,但我确实知道有多少人。 因此,我写了这个内看守的栏目:

private class Guard {
    int waiters = 0;
    boolean wait;
    synchronized void addWaiter() {
        ++waiters;
        wait = true;
        while (wait && waiters != threadNum()) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        waiters = 0;
        wait = false;
        notifyAll();
    }
}

这种守卫的路障是在一间 lo子里实施的。 因此,问题在于,在从第1号呼吁中释放所有线索之前,它可能第二次被称作“wait(> loop”,这显然改变了警卫的整个逻辑。 因此,在他们从警卫队获释之前,我需要有线人。 是否有这方面的设计? 什么是实现这一目标的最合法途径。 我铭记,这些透镜不是由我创造的。

感谢。

最佳回答

http://download.oracle.com/javase/1.5.0/docs/api/java/util/con 当前/CountDownLatch.html 出生日期:。 你们可以确定lat子等待N计票。 See the Javadoc for example.

问题回答

暂无回答




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

热门标签