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 有人能解释一下为什么程序不';结束?_Java_Multithreading_Concurrency_Wait_Notify - Fatal编程技术网

Java 有人能解释一下为什么程序不';结束?

Java 有人能解释一下为什么程序不';结束?,java,multithreading,concurrency,wait,notify,Java,Multithreading,Concurrency,Wait,Notify,嗨,我是java线程的新手。我正在尝试找出 wait()和notify()方法。所以我写了一个简单的程序,但我不能 找出无限执行的原因。请问,有人能帮我解决这个问题吗 公共级楼梯{ 公共静态void main(字符串[]args){ int[]lst={1,2,3,4,5,6,7}; Test1 t1=新的Test1(lst); t1.设置名称(“测试1”); Test2 t2=新的Test2(lst); t2.设置名称(“测试2”); t1.start(); t2.start(); } } 类

嗨,我是java线程的新手。我正在尝试找出 wait()和notify()方法。所以我写了一个简单的程序,但我不能 找出无限执行的原因。请问,有人能帮我解决这个问题吗

公共级楼梯{
公共静态void main(字符串[]args){
int[]lst={1,2,3,4,5,6,7};
Test1 t1=新的Test1(lst);
t1.设置名称(“测试1”);
Test2 t2=新的Test2(lst);
t2.设置名称(“测试2”);
t1.start();
t2.start();
}
}
类Test1扩展了线程{
int[]行;
公共测试1(int[]lst){
这条线=lst;
}
公开募捐{
同步(线路){
对于(int i=0;i<5;i++){
试一试{
如果(i==2)line.wait();
}捕获(例外e){
}
System.out.println(Thread.currentThread().getName()+行[i]);
}
}
}
}
类Test2扩展了线程{
int[]行;
公共测试2(int[]lst){
这条线=lst;
}
公开募捐{
同步(线路){
对于(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+行[i]);
}
}
试一试{
行。notify();
}捕获(例外e){
}
}
}

任务完成后是否尝试system.exit(0)?您的代码在第45行中获得java.lang.IllegalMonitorStateException。是。我认为问题出在线程上,但我无法解决。
catch(Exception ex){}
是一种在调试程序时彻底混淆自己的好方法。当异常发生时,它们会悄悄地消失在一个很深的黑洞中,而你却不知所措。永远,永远,永远,永远,在任何情况下都不要对自己造成如此残酷的伤害。至少放置
ex.printStackTrace()public class StairCase {
    public static void main(String[] args) {
        int[] lst = {1,2,3,4,5,6,7};
        Test1 t1 = new Test1(lst);
        t1.setName("Test 1 ");
        Test2 t2 = new Test2(lst);
        t2.setName("Test 2 ");
        t1.start();
        t2.start();
    }
}

class Test1 extends Thread {
    int[] line ;
    public Test1(int[] lst) {
        this.line = lst;
    }
    public void run(){
        synchronized(line) {
            for (int i = 0; i < 5; i++) {
                try{
                    if(i == 2) line.wait();
               } catch (Exception e) {

               }
               System.out.println(Thread.currentThread().getName() + line[i]);
            }
        }
    }

}

class Test2 extends Thread {
    int[] line ;
    public Test2(int[] lst) {
        this.line = lst;
    }
    public void run() {
        synchronized(line) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + line[i]);
            }
        }
        try {
            line.notify();
        } catch(Exception e) {
        }
    }
}
public class StairCase {
    public static void main(String[] args) {
        int[] lst = {1,2,3,4,5,6,7};
        Test1 t1 = new Test1(lst);
        t1.setName("Test 1 ");
        Test2 t2 = new Test2(lst);
        t2.setName("Test 2 ");
        t1.start();
        t2.start();
    }
}

class Test1 extends Thread {
    private final int[] line ;
    Test1(int[] lst) {
        this.line = lst;
    }
    public void run(){
        synchronized(line) {
            for (int i = 0; i < 5; i++) {
                try{
                    if(i == 2) line.wait();
                } catch (Exception e) {
                  e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + line[i]);
            }
        }
    }

}

class Test2 extends Thread {
    private final int[] line ;
    Test2(int[] lst) {
        this.line = lst;
    }
    public void run() {
        synchronized(line) {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + line[i]);
            }
        }
        try {
            synchronized (line) {
                line.notify();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
        synchronized (line) {
            line.notify();
        }