1

I am working with threads as a fledgling. So I need some help.

For certain work I need a single array-list, which value will be shared by all threads. I want something like this, main() class will provide the array-list to the threads in time of thread creation. Threads will add values to the array-list and a change made by a thread will be reflected in every copy of that array-list and importantly this have to be done in a synchronized fashion.

For example, main() class has given two threads the array-list. Then first thread added a value on slot 1 of the array, second thread while adding will see the change and when it will add,it will add in the second position. When main will give the array list to a new thread all this changes done previously will be readily included, and it will start adding from the third or later positions. Another thing is, only one thread can make a change at a time or it should be synchronized.

How can I do this in java? Can any one help me?

4
  • Pass the same List to all threads, and make sure every access is synchronized. Or better, encapsulate the list in a class that delegates to the list, and ensures the synchronization, and pass an instance of that class to all threads. Start coding, and come back with what you tried if you face a concrete problem. Post the description of the concrete problem, and the code. Commented Jan 24, 2016 at 9:17
  • Note that you need to make sure that the following can be done by a single thread without other threads "interrupting": 1. Find the next free index 2. Set a value at this index. Synchronisation is one way of achieving this. Commented Jan 24, 2016 at 11:13
  • I encapsulated the list in a class and passed it to calling threads. It works. For synchronization I used singelton class with synchronized access to the methods. Thanx @JBNizet Commented Jan 27, 2016 at 13:57
  • For proper synchronization I used singelton class with synchronized access to the methods. Thanx @DavidSoroko Commented Jan 27, 2016 at 13:58

1 Answer 1

1

The collections framework offers convenient wrappers for the synchronizing:

        List<TypeOfItem> list = Collections.synchronizedList( new ArrayList<>() );

You can pass around such a list for adding, removing, reading etc. by different threads. Each access will be synchronized.

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

4 Comments

This doesn't help in the scenarion when a thread needs to find the minimal unassigned index and populate it atomically.
Why do you want to do that? The ArrayList maintains these things for you. Just use add() and the item is added to the list.
Because thread2 may have decided that a[1] is free and about to write the value 2. At the same time thread3 will also determine that a[1] is free and about to write the value 3. You end up with a race condition where the same code sometimes produces a[1]=2 and sometimes a[1]=3
I encapsulated the list in a class and passed it to calling threads. It works. For synchronization I used singelton class with synchronized access to the methods. Thanx @Heri

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.