为什么输出中存在NullPointerException?

时间:2018-03-14 07:59:32

标签: java multithreading

我正在学习Threads,而且我坚持这个程序,为什么这会导致NPE?为什么线程没有运行?

这是我的代码:

class Chicks {
        synchronized void yack(long id) {
        for(int x = 1; x < 3; x++) {
            System.out.print(id + " ");
            Thread.yield();
        }
    }
}
class ChicksYack implements Runnable {
    Chicks c;
    public static void main(String[] args) {
        new ChicksYack().go();
    }
    void go() {
        c = new Chicks();
        new Thread(new ChicksYack()).start();
        new Thread(new ChicksYack()).start();
    }
    public void run() {
        c.yack(Thread.currentThread().getId());
    }
}

0 个答案:

没有答案