Java线程计数不起作用

时间:2017-03-30 03:24:46

标签: java multithreading

为什么算0?

我启动线程1然后我开始线程2.计数应该是2000.但它显示计数为0.有人请用简单的术语解释。

public class App {
    private int count = 0;

    public static void main(String[] args) {
        App app = new App();
        app.doWork();
    }

    public void doWork(){
        Thread t1 = new Thread(new Runnable(){
            public void run(){
                for(int i = 0;i < 10000;i++){
                    count++;
                }
            }
        });

        Thread t2 = new Thread(new Runnable(){
            public void run(){
                for(int i = 0;i < 10000;i++){
                    count++;
                }
            }
        });
        t1.start();
        t2.start();

        System.out.println("Count is: " + count);
    }
}

4 个答案:

答案 0 :(得分:4)

当你打印出你的线程数时,线程还没有完成执行。

要演示,请在打印线程数之前添加Thread.sleep()指令:

public class App {
    private int count = 0;

    public static void main(String[] args) throws InterruptedException {
        App app = new App();
        app.doWork();
    }

    public void doWork() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    count++;
                }
            }
        });

        t1.start();
        t2.start();

        Thread.sleep(5000);

        System.out.println("Count is: " + count); // Count is: 20000
    }
}

另请注意,对基元的操作不是线程安全的,count++操作不是atomic。您应该同步对count变量的访问权限,或使用AtomicIntegerLongAdder代替int。按照目前的情况,你最终可能会计算在0到20,000之间的数量。

答案 1 :(得分:1)

整数增量对多线程不安全。您应该像这样使用AtmoicInteger:

public class App {

    private AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        App app = new App();
        app.doWork();
    }

    public void doWork() throws InterruptedException {
        Thread t1 = new Thread(new Runnable(){
            public void run(){
                for(int i = 0;i < 10000;i++){
                    count.getAndIncrement();
                }
            }
        });

        Thread t2 = new Thread(new Runnable(){
            public void run(){
                for(int i = 0;i < 10000;i++){
                    count.getAndIncrement();
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("Count is: " + count);
    }
}

答案 2 :(得分:0)

这是因为你在主线程中打印计数。 在子线程启动之前,主线程以正常方式执行。 这就是你的计数为0的原因,  尝试在一段时间后打印计数,比如在打印之前放入Thread.sleep(2000)。

答案 3 :(得分:0)

您需要制作代码块,将计数增加为同步,以避免race condition

除此之外,还有多种方法可以解决这个问题 1.主线程中的硬编码Thread.sleep(毫秒值)。 [不推荐]
2.将两个线程join()连接到主线程,以便只有当两个线程的执行完成时,控制才返回主线程。

public class App implements Runnable {
    private int count = 0;

    public static void main(String[] args) throws InterruptedException {
        App app = new App();
        app.doWork();
    }

    public void doWork() throws InterruptedException {
        Thread t1 = new Thread(this, "Thread - 1");
        Thread t2 = new Thread(this, "Thread - 2");

        t1.start();
        t2.start();

        //1.
        t1.join();
        t2.join();

        //2.
        //Thread.sleep(1000);

        System.out.println("Count is: " + count); // Count is: 2000
    }

    @Override
    public synchronized void run() {

        for (int i = 0; i < 1000; i++) {
            count++;
        }
    }
}