本文介绍了Java线程同步 - Thread.sleep()方法不按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说,sleep()会锁定当前的同步方法/块
但是在这里,当我在线程1上调用sleep()时,线程2能够访问同一个块吗?任何人都可以解释一下吗?

i heard, sleep() will lock the current sync method/blockBut here, when i call sleep() on thread 1, thread 2 is able to access the same block? Can anyone Explain?

public class Main {
    public static void main(String args[])
    {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        System.out.println("going to start t1");
        t1.start();
        System.out.println("going to start t2");
        t2.start();

    }

}

=== ================================================== ================

=====================================================================

public class Thread1 extends Thread{

    public void run() {
        Syncc s1 = new Syncc();
        s1.me("T1:");
    }

}

======= ================================================== ============

=====================================================================

public class Thread2 extends Thread{

    public void run() {
        Syncc s2 = new Syncc();
        s2.me("T2:");
    }
}

=========== ================================================== ========

=====================================================================

public class Syncc{

    public void me(String s){
        synchronized(this){
        for(int i=0; i<=5; i++)
        {
            System.out.println(s+" "+" "+i);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
}

==========================================

==========================================

going to start t1
going to start t2
T2:  0
T1:  0
T2:  1
T1:  1
T1:  2
T2:  2
T1:  3
T2:  3
T1:  4
T2:  4
T2:  5
T1:  5

但是根据sleep()方法,它不应该解锁当前的同步块吗?如果是这样,那么输出应该是..

BUT according to sleep() method, it should not unlock the current synchronization block right? if so the out put should be..

将开始t1
开始t2

going to start t1going to start t2

T1:  0
T1:  1
T1:  2
T1:  3
T1:  4
T1:  5
T2:  0
T2:  1
T2:  2
T2:  3
T2:  4
T2:  5

我的意思是在线程1执行后,只有线程2应该正确启动?
问题是什么?

i mean after thread 1 execution only thread 2 should start right?Whats the issue?

推荐答案

这是因为你在游戏中有两个不同的 Syncc 实例这里。每个线程都有自己的 Syncc 副本。

This is because you have two different instances of Syncc in play here. Each thread has its own copy of Syncc.

尝试对单个实例执行相同操作。您还可以在静态上下文中进行同步并尝试。

Try doing the same with a single instance. You could also synchronize on the static context and try.

要模拟,修改 Thread1 Thread2 接受 Syncc 的实例。

To simulate, modify Thread1 and Thread2 to accept an instance of Syncc.

public class Thread1 extends Thread {
    private Syncc syncc;

    public Thread1(Syncc syncc) {
        this.syncc = syncc;
    }

    public void run() {
        this.syncc.me("T1:");
    }
}

然后您可以这样启动它们:

You can then start them as so:

public static void main(String args[]) {
    Syncc syncc = new Syncc();

    Thread1 t1 = new Thread1(syncc);
    Thread2 t2 = new Thread2(syncc);

    System.out.println("going to start t1");
    t1.start();
    System.out.println("going to start t2");
    t2.start();
}

这篇关于Java线程同步 - Thread.sleep()方法不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:03