本文介绍了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

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.

要进行模拟,请修改 Thread1Thread2 以接受 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() 方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:12
查看更多