Integer 对象的同步块

synchronized block for an Integer object(Integer 对象的同步块)

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

问题描述

我刚刚在 Java 中遇到了同步块,并编写了一个小程序来测试它是如何工作的.

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 对象. 整数对象是不可变的……一旦被创建,就无法改变.

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.

底线是你没有在所有线程中使用相同的syncPObj,不同的线程在不同的时间使用不同的syncObjects来同步.

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),称之为计数器"或其他东西.

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

在syncObject上同步,并增加计数器.

Synchronize on syncObject, and increment counter.

根据@jsn,done 标志也被破坏,因为您的代码在 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 对象的同步块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Integer 对象的同步块

基础教程推荐