同步ArrayList并等待

问题描述 投票:-4回答:1

我正在同步2个线程使用的ArrayList。通知和等待模式。通知线程,但arrayList包含旧值虽然另一个线程打印新值。

TH1:

  synchronized( pairsBarBuffers ) {

            try {
                pairsBarBuffers.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Th2细胞:

    synchronized (pairs15MBarBuffers) {

         if( countSamples == 0 ){
             return;
         }

        updateBarBuffer( pairs15MBarBuffers , countSamples );

        pairs15MBarBuffers.notify();

        countSamples = 0;   

    }
java multithreading arraylist
1个回答
1
投票

有两种显式同步方法:

Using Collections.synchronizedList() method
Using thread-safe variant of ArrayList: CopyOnWriteArrayList
© www.soinside.com 2019 - 2024. All rights reserved.