简体   繁体   English

整数对象的同步块

[英]synchronized block for an Integer object

I just came across the synchronized block in Java and wrote a small programm to test how it works. 我刚刚遇到了Java中的同步块,并编写了一个小程序来测试它的工作方式。

I create 10 threads and let each thread increment an Integer object 1000 times. 我创建10个线程,并让每个线程将一个Integer对象增加1000次。

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 . 因此,使用同步时,我假设所有线程完成工作后的结果为10000,而没有同步时的结果小于10000。

However the synchronization is not wokring as I expected. 但是,同步并没有像我预期的那样。

I guess it has something to do with immutability of the object or so. 我猜想这与对象的不变性有关。

My program: 我的程序:

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;
    }
}

Can someone clarify this? 有人可以澄清吗?

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. 每次您对++进行同步时,syncObject都会发生变化(++会将其转换为原始int,对其进行递增,然后将其自动装箱回到Integer对象。Integer对象是不可变的……一旦创建,它们就无法更改。

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. 最重要的是,您并没有在所有线程中使用相同的syncPObj,不同的线程在不同的时间使用不同的syncObject进行同步。

use one object as the synchronization (call it syncObj), and declare it as a final Object: 使用一个对象作为同步对象(称为syncObj),并将其声明为最终对象:

private static final Object syncObject = new Object(); 

Then your counter should be a primitive (int) for perofrmance, call it 'counter' or something. 然后,您的计数器应该是用于性能的原始(int),称为“计数器”之类的东西。

Synchronize on syncObject, and increment counter. 在syncObject上同步,并递增计数器。

Edit: 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. 编辑:按照@jsn的方式,完成标志也被破坏了,因为您的代码在isAllDone()方法上有一个“紧密循环”,这是一种不好的做法。 You should use thread[i].join() to wait (blocking) on each thread's completion, and then check the status from that. 您应该使用thread [i] .join()等待(阻塞)每个线程的完成,然后从中检查状态。 Using an ExecutorService is the 'right way'. 使用ExecutorService是“正确的方法”。

As assumed it is because of the immutability of the Integer object. 如所假定的,这是因为Integer对象的不可变性。

I've changed the synchonized block to 我已将同步块更改为

Integer old = syncObj;
syncObj ++;
System.out.println(syncObj == old);

and my console gets filled with false s 并且我的控制台充满了false

So each time I increment the Integer a new object is createt. 因此,每次我增加Integer一个新对象。

Therefore I only read from the old Object and it will not be locked. 因此,我仅从旧对象读取,并且不会被锁定。

These operations are usually done with Atomic . 这些操作通常是通过Atomic完成的。 Have a look here . 在这里看看。 These structures are specifically designed for multi-threaded computation. 这些结构是专为多线程计算而设计的。 Normal implementations are not thread safe. 普通的实现不是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM