Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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,我正在编写一个小程序来创建4个线程来执行4个任务:增加列表中每个元素的值,减少奇数元素的值并打印输出,每20毫秒删除列表中的最后一个元素,每20毫秒向列表中添加元素,但线程2不显示输出 public class MyClass { static volatile ArrayList<Integer> list = new ArrayList<Integer>(); public static void main(String[] args) {

我正在编写一个小程序来创建4个线程来执行4个任务:增加列表中每个元素的值,减少奇数元素的值并打印输出,每20毫秒删除列表中的最后一个元素,每20毫秒向列表中添加元素,但线程2不显示输出

public class MyClass {
    static volatile ArrayList<Integer> list = new ArrayList<Integer>();

    public static void main(String[] args) {

    Thread th1 = new Thread() {
        public void run() {
            synchronized (list) {
                while (true) {
                    if (list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            list.set(i, list.get(i) + 1);
                        }
                    }
                }
            }
        }
    };

    Thread th2 = new Thread() {
        public void run() {
            while (true) {
                synchronized (list) {
                    if (list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            if ((list.get(i) % 2) != 0) {
                                list.set(i, list.get(i) - 1);
                                System.out.println(list.get(i));
                            }
                        }
                    }
                }
            }
        }
    };

    Thread th3 = new Thread() {
        public void run() {
            while (true) {
                synchronized (list) {
                    if (list.size() > 0) {
                        list.remove(list.size() - 1);
                    }
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    Thread th4 = new Thread() {
        public void run() {
            for (int i = 0;; i++) {
                synchronized (list) {
                    list.add(i);
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    th4.start();
    th2.start();
    th1.start();
    th3.start();        
}
}
公共类MyClass{
静态易失性ArrayList=新ArrayList();
公共静态void main(字符串[]args){
线程th1=新线程(){
公开募捐{
已同步(列表){
while(true){
如果(list.size()>0){
对于(int i=0;i0){
对于(int i=0;i0){
list.remove(list.size()-1);
}
}
试一试{
睡眠(20);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
};
Thread th4=新线程(){
公开募捐{
对于(int i=0;i++){
已同步(列表){
列表.添加(i);
}
试一试{
睡眠(20);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
};
th4.start();
th2.start();
th1.start();
th3.start();
}
}

我的代码有什么问题吗?

第一个线程锁定列表,然后进入无限循环。因此,第二个线程永远无法锁定列表并执行其任务,因为第一个线程永远持有锁。

@harpun当main方法返回时,程序不会结束。当所有非守护进程线程停止运行时结束。谢谢,但是在删除线程1的同步后,我遇到了IndexOutOfBoundsException,请您帮助我修复它。您不应该删除线程1的同步。你应该把它放在while循环里面,而不是外面。这样,在每次迭代中,您都会离开同步块,从而让其他线程等待锁来获取它。还可以在线程one impl中添加一些空闲时间,以便将处理器交付给其他线程。不是强制性的,但会有所帮助。