多线程奇数偶数

时间:2018-01-30 18:25:13

标签: java multithreading

交替打印奇数/偶数数2个解析线程序。程序没有结束。 EVEN线程处于等待状态。尝试在循环外添加'notifyAll()'。另外如果我在while循环中设置evenFlag / oddFlag,则线程名称会改回Thread1和Thread2,不知道为什么..

import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;

public class MultiThreading {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Runnable r = new PrintNum();
        Thread t0 = new Thread(r, "ZERO");
        Thread todd = new Thread(r, "Thread1");
        Thread teven = new Thread(r, "Thread2");
        t0.start();
        todd.start();
        teven.start();
    }

    public static class PrintNum implements Runnable {
        boolean zeroFlag = true;
        boolean evenFlag = false;
        boolean oddFlag = false;
        int count = 0;
        int limit = 100;

        @Override
        public void run() {
            synchronized (this) {
                while (count <= limit) {
                    if (zeroFlag) {
                        System.out.println(Thread.currentThread().getName() + ":" + count);
                        count++;
                        zeroFlag = false;
                        oddFlag = true;
                        break;
                    }
                    if (evenFlag) {
                        if (count == 2)
                            Thread.currentThread().setName("EVEN");
                        System.out.println(Thread.currentThread().getName() + ":" + count);
                        count++;
                        evenFlag = false;
                        oddFlag = true;
                        notify();
                        while (!evenFlag) {
                            try {
                                wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }
                    if (oddFlag) {
                        if (count == 1)
                            Thread.currentThread().setName("ODD");
                        System.out.println(Thread.currentThread().getName() + ":" + count);
                        count++;
                        evenFlag = true;
                        oddFlag = false;
                        notify();
                        while (!oddFlag) {
                            try {
                                wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我不想变得粗鲁,在学习的同时可以提出问题,但看起来你正试图让Stackoverflow上的某人完成你的作业......

只需在while块中的run方法中添加以下行,否则其中一个线程将保持等待状态。

evenFlag = true;
oddFlag = true;
notify();

尝试添加System.out.println / debug行,看看下次发布问题之前会发生什么。此外,尝试使用Java并发API而不是手动同步线程,AtomicInteger将在几行代码中执行您尝试执行的操作。

答案 1 :(得分:0)

这是工作解决方案,

> str(gc_all)
List of 3
$ 1: num [1:102, 1:2] -74 -73.5 -73 -72.5 -71.9 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:2] "lon" "lat"
$ 2: num [1:102, 1:2] -74 -73.3 -72.5 -71.8 -71 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:2] "lon" "lat"
$ 3:List of 2
..$ : num [1:37, 1:2] -74 -74.4 -74.8 -75.3 -75.8 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "lon" "lat"
..$ : num [1:65, 1:2] 180 169 163 158 154 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "lon" "lat"