java中并发控制,JAVA:用于访问Java中列表的并发控制

I have a multi-threaded application that has a centrlaised list that is updated (written to) only by the main thread. I then have several other threads that need to periodically retrieve the list in its current state. Is there a method that can allow me to do this?

解决方案

That depends on how you want to restrict the concurrency. The easiest way is probably using CopyOnWriteArrayList. When you grab an iterator from it, that iterator will mirror how the list looked at the point in time when the iterator was created - subsequent modifications will not be visible to the iterator. The upside is that it can cope with quite a lot of contention, the drawback is that adding new items is rather expensive.

The other way of doing is locking, the simplest way is probably wrapping the list with Collections.synchronizedList and synchronizing on the list when iterating.

A third way is using some kind of BlockingQueue and feed the new elements to the workers.

Edit: As the OP stated only a snapshot is needed, CopyOnWriteArrayList is probably the best out-of-the-box alternative. An alternative (for cheaper adding, but costlier reading) is just creating a copy of a synchronizedList when traversion is needed (copy-on-read rather than copy-on-write):

List originalList = Collections.synchronizedList(new ArrayList());

public void mainThread() {

while(true)

originalList.add(getSomething());

}

public void workerThread() {

while(true) {

List copiedList;

synchronized (originalList) {

copiedList = originalList.add(something);

}

for (Foo f : copiedList) process(f);

}

}

Edit: Come to think of it, the copy-on-read version could simplified a bit to avoid all synchronized blocks:

List originalList = Collections.synchronizedList(new ArrayList());

public void mainThread() {

while(true)

originalList.add(getSomething());

}

public void workerThread() {

while(true) {

for (Foo f : originalList.toArray(new Foo[0]))

process(f);

}

}

Edit 2: Here's a simple wrapper for a copy-on-read list which doesn't use any helpers, and which tries to be as fine-grained in the locking as possible (I've deliberately made it somewhat excessive, bordering on suboptimal, to demonstrate where locking is needed):

class CopyOnReadList {

private final List items = new ArrayList();

public void add(T item) {

synchronized (items) {

// Add item while holding the lock.

items.add(item);

}

}

public List makeSnapshot() {

List copy = new ArrayList();

synchronized (items) {

// Make a copy while holding the lock.

for (T t : items) copy.add(t);

}

return copy;

}

}

// Usage:

CopyOnReadList stuff = new CopyOnReadList();

stuff.add("hello");

for (String s : stuff.makeSnapshot())

System.out.println(s);

Basically, when you to lock when you:

... add an item to the list.

... iterate over the list to make a copy of it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值