简体   繁体   English

线程安全对象数组

[英]Thread safe object array

I use 2-dimensional array in Java. 我在Java中使用二维数组。 But now, I want to use this class for multi-thread. 但是现在,我想将此类用于多线程。 How can I do that? 我怎样才能做到这一点?

I know that how can I do a thread safe function (add synchronized keyword). 我知道该如何执行线程安全功能(添加同步关键字)。 What happens if clear and getItem functions are triggered at the same time? 如果同时触发clear和getItem函数会怎样? How can I do thread safe inctance for this case? 在这种情况下,我该如何线程安全启动?

public class ThreadSafeArray {
    int ROW_MAX_COUNT = 1024;

    int rowCount = 0;
    int counterForRow = 0;
    private Object [][] objInstances = new Object[ROW_MAX_COUNT][];

    public synchronized void addItem(Object obj) {
        if(counterForRow == ROW_MAX_COUNT) {
            objInstances[++rowCount] = new Object[ROW_MAX_COUNT];
            counterForRow = 0;
        }

        objInstances[rowCount][counterForRow++] = obj;
    }

    public synchronized  void clear() {
        objInstances = new Object[ROW_MAX_COUNT][];
        rowCount = 0;
        counterForRow = 0;
    }

    public synchronized Object getItem(int index) {
        int row = index / ROW_MAX_COUNT;
        int column = index % ROW_MAX_COUNT;

        if((row <= rowCount) && (column <= counterForRow)) {
            return objInstances[row][column];
        }
        return null;
    }
}

IN your code, clear and getItem are instance methods. 在您的代码中, cleargetItem是实例方法。 Putting synchronized on an instance method means that the thread has to acquire the lock (the "intrinsic lock") on the object instance that the method is called on before the thread can start executing any code in that method. 将同步放在实例方法上意味着线程必须在该线程可以开始在该方法中开始执行任何代码之前,获取对该方法调用的对象实例的锁定(“固有锁定”)。

Making instance methods synchronized has two effects(from java guide): 使实例方法同步有两种效果(来自Java指南):

  • First, it is not possible for two invocations of synchronized methods on the same object to interleave. 首先,不可能对同一对象的两次同步方法调用进行交织。 When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object. 当一个线程正在执行对象的同步方法时,所有其他调用同一对象块的同步方法的线程(挂起执行),直到第一个线程对该对象完成。
  • Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. 其次,当同步方法退出时,它将自动与之前对同一对象的同步方法的任何调用建立先发生关系。 This guarantees that changes to the state of the object are visible to all threads. 这保证了对象状态的更改对所有线程都是可见的。

So,your class is already thread-safe for these two methods. 因此,对于这两种方法,您的类已经是线程安全的。

What happens if clear and getItem functions are triggered at the same time? 如果同时触发clear和getItem函数会怎样?

One will wait until another is finished. 一个将等到另一个完成。

How can I do thread safe inctance for this case? 在这种情况下,我该如何线程安全启动?

It is already thread-safe. 它已经是线程安全的。

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

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