2

My doubt is that , is there any option that i can get values in Arraylist , that is added by the first thread , from the second thread and so on. And each thread will be modifying the list and updated list need to be available for all the threads .

Any help would be appreciated.

Thanks

deva
  • 49
  • 6
  • 2
    @PramodYadav: That would do nothing useful. – T.J. Crowder Sep 18 '17 at 10:20
  • You can achieve this with proper synchronization. But it is probably better to use a different data structure from the `java.util.concurrent` package if you need to communicate between threads. They have spezialized collections for things like queues. – Thilo Sep 18 '17 at 10:24
  • An example for using a queue: [How to use ConcurrentLinkedQueue?](https://stackoverflow.com/questions/616484/how-to-use-concurrentlinkedqueue) – Mick Mnemonic Sep 18 '17 at 10:40

2 Answers2

2

You can use java.util.concurrent.CopyOnWriteArrayList

Sándor Juhos
  • 1,535
  • 1
  • 12
  • 19
1

Try to create synchronized ArrayList using Collections class static method.

Below arraylist will be synchronized :

List<String> synArraylist = Collections.synchronizedList(new ArrayList<String>());
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41