5

我运行了以下代码:

class Counter extends Thread {

 static int i=0;
//method where the thread execution will start
public void run(){
    //logic to execute in a thread

    while (true) {
        increment();
    }
}

public synchronized void increment()  {
    try {
        System.out.println(this.getName() + " " +  i++);
        wait(1000);
        notify();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
//let’s see how to start the threads
public static void main(String[] args){
    Counter c1 = new Counter();
    Counter c2 = new Counter();
    c1.setName("Thread1");
    c2.setName("Thread2");
    c1.start();
    c2.start();
}
}

此代码的结果是(添加的行号):

1: Thread1 0
2: Thread2 1
3: Thread2 2
4: Thread1 3
5: Thread2 4
6: Thread1 4
7: Thread1 5
8: Thread2 6
stopping...

由于增量方法是同步的并且因为它包含 wait(1000) 我没想到:1. Thread2 打印 2 个连续打印:第 2,3 行我希望线程交错它们的打印 2. 在第 5,6 行 i 仍然是 4。

谁能给我一个解释?

4

3 回答 3

9

像这样的同步实例方法:

public synchronized void foo() { 
    ... 
}

大致相当于:

public void foo() {
   synchronized(this) {
       ...
   }
}

你看到这里的问题了吗?同步在当前实例上完成。

由于您正在创建两个单独的对象,因此Thread每个increment方法都会在不同的对象上同步,从而使锁变得无用。

您应该使您的增量方法静态(因此锁定在类本身上完成)或使用静态锁定对象:

private static final Object locker = new Object();

public void foo() {
   synchronized(locker) {
       ...
   }
}

最后一个建议:在java中创建线程的首选方法是实现Runnable,而不是扩展Thread

于 2012-05-19T12:26:06.867 回答
1

您仅在实例级别进行同步。要在所有Counter实例之间同步,您需要该increment方法staticsynchronized.

就目前而言,您的所有线程都可以自由运行,彼此并发,因为它们不共享同步机制。

于 2012-05-19T12:22:55.930 回答
0

这可能是您正在寻找的代码

class Counter implements Runnable {

    static int i = 0;
    private Lock lock;
    private Condition condition;

    public Counter(Lock lock, Condition condition) {

        this.lock = lock;
        this.condition = condition;
    }


    public void run() {
        while (true) {
            lock.lock();
            try {
                condition.await(1, TimeUnit.SECONDS);
                System.out.append(Thread.currentThread().getName()).append(" ").println(i++);
                condition.signalAll();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        Lock lock = new ReentrantLock(true);
        Condition condition = lock.newCondition();
        Executor e = Executors.newFixedThreadPool(2);
        e.execute(new Counter(lock, condition));
        e.execute(new Counter(lock, condition));

    }
}
于 2012-05-19T13:12:32.943 回答