1

I use 2-dimensional array in 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? 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;
    }
}
2
  • You could just use java.lang.Vector, it's synchronized. Commented Feb 21, 2019 at 7:09
  • Why are you using a two dimensional array when your API is one-dimensional anyway? That makes you code unnecessarily complicated. Commented Feb 28, 2019 at 12:30

2 Answers 2

4

IN your code, clearand getItem are instance methods. 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):

  • 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.

Sign up to request clarification or add additional context in comments.

1 Comment

It means that the instance is thread-safe if its all public methods are synchronized. Is it ok?
2

What happens if clear and getItem functions are triggered at the same time?

One will wait until another is finished.

How can I do thread safe inctance for this case?

It is already thread-safe.

3 Comments

If I create a singleton class (with all static methods are synchronized), the class and singleton instance are tread-safe. Is it ok? If yes, this example don't say same thing. javatpoint.com/static-synchronization-example
@us2956 Why do you need instance of class if all method are static? Article you refer doesn't say anything. Or I missed something?
There are two ways for my task. 1- Singleton instance and synchronized public methods(It require the code refactoring for other parts of the project). 2- All public methods are static and synchronized(it doesn't require refactoring on other parts of the project). Sorry, I misunderstood the article. Duplicate numbers are confused me. 8 9 10 10 20 30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.