Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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线程的查询_Java - Fatal编程技术网

关于Java线程的查询

关于Java线程的查询,java,Java,请让我知道如何打印“等待后”;如何在以下代码中通知主线程: import java.util.*; public class Test { public static void main(String[] args) throws InterruptedException { ArrayList al = new ArrayList(6); al.add(0, "abc"); al.add(1, "abc");

请让我知道如何打印“等待后”;如何在以下代码中通知主线程:

import java.util.*;  

public class Test {

      public static void main(String[] args) throws InterruptedException {
            ArrayList al = new ArrayList(6);
            al.add(0, "abc");
        al.add(1, "abc");
        al.add(2, "abc");
        synchronized(al){
            System.out.println("Before wait");
            al.wait();
            System.out.println("After wait");
        }       

      }

}
wait()
调用正在阻塞,直到有人
notify()
s它。。。基本上,当主线程在
wait()
中阻塞时,您需要创建一个调用
al.notify()
的新线程

此程序在等待前打印
,暂停一秒钟,然后在等待后打印

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        final ArrayList al = new ArrayList(6);
        al.add(0, "abc");
        al.add(1, "abc");
        al.add(2, "abc");

        // Start a thread that notifies al after one second.
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (al) {
                    al.notify();      // <-- this "releases" the wait.
                }
            }
        }.start();

        synchronized (al) {
            System.out.println("Before wait");
            al.wait();
            System.out.println("After wait");
        }
    }
}
import java.util.ArrayList;
公开课考试{
公共静态void main(字符串[]args)引发InterruptedException{
最终ArrayList al=新ArrayList(6);
新增(0,“abc”);
新增(1,“abc”);
新增(2,“abc”);
//启动一个线程,在一秒钟后通知al。
新线程(){
公开募捐{
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
同步(al){
al.notify();//在有人
notify()
s它之前,
wait()调用一直处于阻塞状态……基本上,当主线程在
wait()
中阻塞时,您需要创建一个调用
al.notify()
的新线程

此程序在等待前打印
,暂停一秒钟,然后在等待后打印

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        final ArrayList al = new ArrayList(6);
        al.add(0, "abc");
        al.add(1, "abc");
        al.add(2, "abc");

        // Start a thread that notifies al after one second.
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (al) {
                    al.notify();      // <-- this "releases" the wait.
                }
            }
        }.start();

        synchronized (al) {
            System.out.println("Before wait");
            al.wait();
            System.out.println("After wait");
        }
    }
}
import java.util.ArrayList;
公开课考试{
公共静态void main(字符串[]args)引发InterruptedException{
最终ArrayList al=新ArrayList(6);
新增(0,“abc”);
新增(1,“abc”);
新增(2,“abc”);
//启动一个线程,在一秒钟后通知al。
新线程(){
公开募捐{
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
同步(al){

al.notify();//您没有创建任何其他线程,因此很难看到还有什么其他内容可以通知主线程。但是,如果您确实有另一个线程引用了
al
,则您可以使用以下内容:

synchronized(al) {
    al.notify();
}


(两者之间的区别在于,
notify
将只唤醒单个线程的等待;
notifyAll
将唤醒该对象上等待的所有线程。)

您没有创建任何其他线程,因此很难看到还有什么其他内容可以通知主线程。但是,如果您确实有另一个线程引用了
al
,您可以使用以下内容:

synchronized(al) {
    al.notify();
}


(两者之间的区别在于,
notify
只会唤醒一个线程的等待;
notifyAll
会唤醒该对象上等待的所有线程。)

正如其他答案更全面地解释的那样,
wait()
不是睡眠调用。正如其他答案更全面地解释的那样,
wait()
不是睡眠调用。您还必须在al.wait()周围添加try-catch…但除此之外,它是完美的。:)不,OP在main方法中已经有了
抛出InterruptedException
。因此,wait-notify仅在线程具有对象监视器时才起作用。感谢您的帮助,您必须在al.wait()周围添加try-catch还有…但除此之外,它是完美的。:)不,OP在main方法中已经有了
抛出InterruptedException
。因此,等待通知仅在线程有对象监视器时有效。谢谢帮助