English 中文(简体)
多个物体的同步化和反射
原标题:Synchronization of many objects and counter

我知道,许多人在这个问题上有问题,你可能会 bore,但我自几天以来一直试图理解这个问题,并且仍然不知道它是如何运作的。 我有一个反目标,还有另一个类别的其他物体(今后一个类别)。 现在每个目标都应对反执行作出答复。 一个计算点——每个物体的一个步骤是操作方法。 我的法典是:

public class Th {

  private final static Object lock1 = new Object();

////////////////////////////////////////////////////////////////////////////////

  private class Stop implements Runnable {

    private int count, id;

    public Stop(int id) {
      this.count = 0;
      this.id = id;
    }

    @Override public void run() {
      synchronized(lock1){
        while (count < 20) {
          try {
            lock1.wait();
          } 
          catch (InterruptedException exception) {
                System.out.println("Error!");
          }
          System.out.println(count + " stop " + id);
          this.count++;

//          try {
//            Thread.sleep(360);
//          }
//          catch (InterruptedException exception) {
//            System.out.println("Error!");
//          } 

        }
      }
    }

  } 

////////////////////////////////////////////////////////////////////////////////

  private class Counter implements Runnable {

    private int count;

    public Counter() {
      this.count = 0;
    }

    @Override public void run() {

      synchronized(lock1){
        while (count<15) {
          lock1.notifyAll();

          System.out.println(count + " counter");
          this.count++;      

//          try {
//            Thread.sleep(360);
//          }
//          catch (InterruptedException exception) {
//            System.out.println("Error!");
//          }          

        }
      }
    }

  } 

   public void test() {

     Stop s1 = new Stop(1);
     Stop s2 = new Stop(2);
     Stop s3 = new Stop(3);

     Counter counter = new Counter();

     (new Thread(s1)).start();
     (new Thread(s2)).start();
     (new Thread(counter)).start();
     (new Thread(s3)).start();


   }

}

以及我回来的还有:

run:
0 counter
1 counter
2 counter
3 counter
4 counter
5 counter
6 counter
7 counter
8 counter
9 counter
10 counter
11 counter
12 counter
13 counter
14 counter
0 stop 1

我需要的是:

0 counter
0 stop 0
0 stop 1
0 stop 2
1 counter
1 stop 0
1 stop 1
1 stop 2
2 counter
2 stop 0
2 stop 1
2 stop 2
3 counter
...
最佳回答

<代码>Counter thread is coordinatedd on lock1。 这意味着,虽然你打电话<代码>,但注明 在座标上的所有,其他校对可在整整整座休息室(Counter上填满之前重新启用。

Make each iteration of the loop synchronized, instead of synchronizing outside of the loop.

值得注意的是,这一胜利已经足够了,但是,因为反线透镜可以在所有停线透镜重新获得之前重新开通。 你们需要把反read放在另一个状态下,并在所有“停止”的透镜都展示出点时重新开始。

你们应当调查诸如Cyclic Barrier和DownLatch等高层次的抽象。

问题回答

First, the best way to solve this is to use the higher level synchronization classes, as JB Nizet says.

But if you want to do it "the hard way" as a learning exercise, you need to recognize that this problem requires the Counter and Stop threads to wait for specific "events".

  • 停战线需要等到防战线透镜告诉他们要向下站挺进。

  • 反响需要等到all。 穿透镜已经推进和停止。

One problem with your current implementation is that the Counter thread has nothing to tell it when all of the Stop threads have advanced and stopped. Instead, it assumes that when it sees a notify event that it is ok to issue the next count.





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

热门标签