Integer对象的synchronized块 [英] synchronized block for an Integer object

查看:128
本文介绍了Integer对象的synchronized块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到Java中的synchronized块并编写了一个小程序来测试它是如何工作的。

I just came across the synchronized block in Java and wrote a small programm to test how it works.

我创建10个线程并让每个线程递增一个Integer对象1000次。

I create 10 threads and let each thread increment an Integer object 1000 times.

因此,在所有线程完成工作后,我会假设结果为10000,结果小于10000而没有同步。

So with synchronization I would assume a result of 10000 after all threads have finished their work and a result of less than 10000 without synchronization .

然而,同步并不像我预期的那样。

However the synchronization is not wokring as I expected.

我想它与对象的不变性有关。

I guess it has something to do with immutability of the object or so.

我的程序:

public class SyncTest extends Thread{

    private static Integer syncObj = new Integer(0);
    private static SyncTest[] threads = new SyncTest[10];

    private boolean done = false;

    public void run(){
        for(int i = 0; i < 1000; i++){
            synchronized(syncObj){
                syncObj ++;
            }
        }
        done = true;
    }

    public static void main(String[] args) {

        for(int i=0; i < threads.length; i++){
            threads[i] = new SyncTest();
            threads[i].start();
        }

        while(!allDone()); //wait until all threads finished

        System.out.println(syncObj);
    }

    private static boolean allDone(){
        boolean done = true;
        for(int i = 0; i < threads.length; i++){
            done &= threads[i].done; 
        }

        return done;
    }
}

有人可以澄清这个吗?

推荐答案

每次使用它时,syncObject都会发生变化(++将它转换为原始int,递增它,然后将其自动装箱回到Integer对象。Integer对象是不可变的...一旦创建,都无法改变。

syncObject is changing each time you ++ it (the ++ is converting it to a primitive int, incrementing it, and then autoboxing it back to the Integer object. Integer objects are immutable ... once they are created, they cannot change.

底INE是你没有使用相同的syncPObj中的所有线程,不同的线程在不同的时间使用不同的syncObject进行同步。

Bottom ine is that you are not using the same syncPObj in all the threads, different threads use different syncObjects at different times to sync on.

使用一个对象作为同步(称之为syncObj),并将其声明为最终对象:

use one object as the synchronization (call it syncObj), and declare it as a final Object:

private static final Object syncObject = new Object(); 

那么你的计数器应该是一个原始的(int)用于perofrmance,称之为'counter'或者什么。

Then your counter should be a primitive (int) for perofrmance, call it 'counter' or something.

syncObject同步,和增量计数器。

Synchronize on syncObject, and increment counter.

编辑:根据@jsn,完成标志也被打破了哟你的代码在isAllDone()方法上有一个'紧密循环',这是不好的做法。你应该使用thread [i] .join()来等待(阻塞)每个线程的完成,然后从中检查状态。使用ExecutorService是正确的方法。

as per @jsn, the done flag is also broken in that your code has a 'tight loop' on the isAllDone() method, and that is bad practice. You should use thread[i].join() to wait (blocking) on each thread's completion, and then check the status from that. Using an ExecutorService is the 'right way'.

这篇关于Integer对象的synchronized块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆