在同一实例上使用synchronized方法锁定

时间:2017-07-19 00:16:09

标签: java multithreading

我正在运行此代码:

public class SyncMethods {

    public static void main(String... args) {
        new SyncMethods().syncMethodsInvoker();
    }

    public void syncMethodsInvoker() {

        System.out.println("inside syncMethodsInvoker()");

        Thread t1 = new SyncMethodThread(this);
        Thread t2 = new SyncMethodThread(this);
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized void firstSyncMethod(String threadName) {

        System.out.format("begin firstSyncMethod() %s\n", threadName);

        for (int i = 0; i < 5; i++) {
            System.out.format("looping firstSyncMethod() %s #%s for object %s\n", threadName, i, this);
            try {
                this.wait(100);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("end firstSyncMethod() " + threadName);
    }

    private class SyncMethodThread extends Thread {

        final private SyncMethods sm;

        public SyncMethodThread(SyncMethods sm) {
            this.sm = sm;
        }

        @Override
        public void run() {
            System.out.format("inside run() %s, sm: %s\n", this.getName(), sm);
            sm.firstSyncMethod(this.getName());
        }
    }
}

我得到了这个输出:

inside syncMethodsInvoker()
inside run() Thread-0, sm: multithreading.SyncMethods@6189a0b9
inside run() Thread-1, sm: multithreading.SyncMethods@6189a0b9
begin firstSyncMethod() Thread-0
looping firstSyncMethod() Thread-0 #0 for object multithreading.SyncMethods@6189a0b9
begin firstSyncMethod() Thread-1
looping firstSyncMethod() Thread-1 #0 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #1 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #1 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #2 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #2 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #3 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #3 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #4 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #4 for object multithreading.SyncMethods@6189a0b9
end firstSyncMethod() Thread-1
end firstSyncMethod() Thread-0

由于两个线程都锁定在同一个实例上,我希望循环能够像这样:

looping firstSyncMethod() Thread-0 #0 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #1 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #2 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #3 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-0 #4 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #0 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #1 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #2 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #3 for object multithreading.SyncMethods@6189a0b9
looping firstSyncMethod() Thread-1 #4 for object multithreading.SyncMethods@6189a0b9

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

由于此行this.wait(100);导致当前线程释放锁并等待100ms。所以其他线程可以获得锁定。

Object.wait(long)

  

此方法使当前线程(称为T)将自身置于此对象的等待集中,然后放弃此对象上的所有同步声明。线程T因线程调度而被禁用,并处于休眠状态,直到发生以下四种情况之一:...

在问题上用erickson's comment补充答案:如果你想等待&#34;等待&#34;一点,使用Thread.sleep()(或TimeUnit.MILLIS.sleep())。