Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中同步块后的代码不可访问,但semp.release()不可访问_Java_Multithreading - Fatal编程技术网

java中同步块后的代码不可访问,但semp.release()不可访问

java中同步块后的代码不可访问,但semp.release()不可访问,java,multithreading,Java,Multithreading,在上面的代码中,我使用信号量实现进行同步和 如果(is_grab){Thread.sleep(1000);}可以执行 然而,在下面的代码中 Thread faculty = new Thread(() -> { try { while (true) { boolean is_grab = false; semp.acquire();

在上面的代码中,我使用信号量实现进行同步和 如果(is_grab){Thread.sleep(1000);}可以执行

然而,在下面的代码中

Thread faculty = new Thread(() -> {
                try {
                    while (true) {


         boolean is_grab = false;
                    semp.acquire();
                    if (candy > 0) {
                        System.out.println("No." + NO + " faculty grabs a candy");
                        candy--;
                        is_grab = true;
                        System.out.println("Candy num left:" + candy);
                    }
                    semp.release();
                    if (is_grab) {
                        Thread.sleep(1000);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        faculty.start();
    }
Thread faculty=新线程(()->{
同步(碗类){
while(true){

while(Bowl.candy在下面的示例中,没有
break
语句可以跳出
while(true)
循环。

没有代码跳出循环。第一个代码段中的
sleep
while(true)
的一部分。但是第二个代码段中的
sleep
while(true)之后
,但是在同一个线程上。由于
while(true)
没有逃脱的方法,执行永远无法进入
睡眠状态。谢谢。我太草率了。
Thread faculty = new Thread(() -> {
                synchronized (Bowl.class) {
                    while (true) {
                        while (Bowl.candy <= 0) {
                            try {
                                Bowl.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("No." + NO + " faculty grabs a candy");
                        Bowl.candy--;
                        System.out.println("Candy num left:" + Bowl.candy);
                        Bowl.class.notifyAll();
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });