Java并发集合搜索
我用Java编程已经有一段时间了,但对并发编程来说还是新手,所以请耐心听我说 我试图开发一个类,它包含一组集合类[例如ArrayList],然后找到一个指定的值,它同时遍历所有集合,如果找到给定的值,则停止所有线程 我已经在下面粘贴了我的代码,我想知道是否有人知道如何在其中包含多个集合()我发现其中一个线程是否返回了true 谢谢 格雷厄姆Java并发集合搜索,java,concurrency,Java,Concurrency,我用Java编程已经有一段时间了,但对并发编程来说还是新手,所以请耐心听我说 我试图开发一个类,它包含一组集合类[例如ArrayList],然后找到一个指定的值,它同时遍历所有集合,如果找到给定的值,则停止所有线程 我已经在下面粘贴了我的代码,我想知道是否有人知道如何在其中包含多个集合()我发现其中一个线程是否返回了true 谢谢 格雷厄姆 public class CollectionGroup<V> extends ContainerGroup { //... publi
public class CollectionGroup<V> extends ContainerGroup
{
//...
public boolean contains(V value)
{
boolean containsValue = false;
if (mCollections.size() == 1)
{
containsValue = mCollections.get(0).contains(value);
}
else
{
containsValue = contains_multiple_collections(value);
}
return containsValue;
}
private boolean contains_multiple_collections(V value)
{
// thread pool
int numberProcessors = mCollections.size();
ExecutorService es = Executors.newFixedThreadPool(numberProcessors);
for (int i=0; i<numberProcessors; i++)
{
AbstractCollection<V> collection = mCollections.get(i);
MyCallable callable = new MyCallable(collection,value);
Future<Boolean> future = es.submit(callable);
//...
}
return true;
}
private class MyCallable implements Callable<Boolean>
{
protected AbstractCollection<V> mCollection;
protected V mValue;
public MyCallable(AbstractCollection<V> collection, V value)
{
mCollection = collection;
mValue = value;
}
@Override
public Boolean call() throws Exception
{
boolean ok = mCollection.contains(mValue);
return ok;
}
} // class MyCallable
} // class CollectionGroup
公共类集合组扩展了ContainerGroup
{
//...
公共布尔包含(V值)
{
布尔值containsValue=false;
if(mCollections.size()==1)
{
containsValue=mCollections.get(0).contains(值);
}
其他的
{
containsValue=包含多个集合(值);
}
返回包含值;
}
私有布尔值包含多个集合(V值)
{
//线程池
int numberProcessors=mCollections.size();
ExecutorService es=Executors.newFixedThreadPool(numberProcessors);
for(int i=0;icontains不会因为您可能在另一个线程中找到了该值而停止。唯一的方法是自己循环
对于CPU密集型进程,最佳线程数可能是您拥有的内核数。创建太多线程会增加开销,从而降低解决方案的速度
您还应该记住在完成ExecutorService时关闭它
如果您想加快搜索速度,我会保留一组所有值,这可能比使用多个线程快10-100倍。包含不会因为您可能在另一个线程中找到该值而停止。唯一的方法是自己循环
对于CPU密集型进程,最佳线程数可能是您拥有的内核数。创建太多线程会增加开销,从而降低解决方案的速度
您还应该记住在完成ExecutorService时关闭它
如果您想加快搜索速度,我会保留一组所有值,这可能比使用多个线程快10-100倍。您可以使用零或几乎零的超时重复循环所有未来并轮询它们,但可能更好的办法是提供对所有MyCallable
的回调,这然后在找到匹配项时调用它。然后回调应取消所有其他任务,可能通过关闭ExecutorService
您可以使用零或几乎零的超时重复循环所有未来并轮询它们,但可能更好的方法是为所有MyCallable
提供回调,然后在找到匹配项时调用它。然后,回调应取消所有其他任务,可能通过关闭ExecutorService
您可以使用ExecutorCompletionService。只需保持take()运行(take()阻塞,直到出现已完成的未来),直到得到正确的结果并立即关闭()找到某个值后,基础ExecutrService将立即停止。但是,一旦找到值,这不会立即停止所有线程。您可以使用ExecutorCompletionService。只需保持take()运行(take()阻塞,直到出现完整的将来),直到得到正确的结果并立即关闭()找到某个值后,基础ExecutrService将立即停止。但是,一旦找到某个值,这不会立即停止所有线程。我建议使用静态AtomicBoolean,该值在找到匹配项时设置。然后,每个线程都可以在继续之前检查该值。我建议使用静态AtomicBoolean,该值在找到匹配项时设置。然后,每个线程都可以在继续之前检查值。问题在于您的contains\u multiple\u collections方法没有等待搜索完成。我可以考虑两个选项。第一个选项涉及一些异步回调实现,其中contains方法不阻塞,可能需要回调/侦听器对象作为参数。第二个是使contains方法块,直到确定结果为止。我在下面概述了后一种方法的示例实现,它没有经过测试,所以要小心
/*
* contains_multiple_collections now blocks until the desired
* value is located or all searches have completed unsuccessfully...
*/
private boolean contains_multiple_collections(V value) {
// thread pool
int numberProcessors = mCollections.size();
ExecutorService es = Executors.newFixedThreadPool(numberProcessors);
Object lock = new Object();
AtomicBoolean outcome = new AtomicBoolean(false);
AtomicInteger remainingSearches = new AtomicInteger(mCollections.size());
for (int i = 0; i < numberProcessors; i++) {
AbstractCollection<V> collection = mCollections.get(i);
es.submit(new MyRunnable(collection, value, lock, outcome, remainingSearches));
}
/* Wait for searches to run. This thread will be notified when all searches
* complete without successfully locating the value or as soon as the
* desired value is found.
*/
synchronized (lock) {
while (!outcome.get() && remainingSearches.get() > 0) {
try {
lock.wait();
} catch (InterruptedException ex) {
// do something sensible.
}
}
es.shutdownNow();
}
return outcome.get();
}
private class MyRunnable implements Runnable {
final AbstractCollection<V> mCollection;
final V mValue;
final Object lock;
final AtomicBoolean outcome;
final AtomicInteger remainingSearches;
public MyRunnable(AbstractCollection<V> mCollection, V mValue,
Object lock, AtomicBoolean outcome, AtomicInteger remainingSearches) {
this.mCollection = mCollection;
this.mValue = mValue;
this.lock = lock;
this.outcome = outcome;
this.remainingSearches = remainingSearches;
}
public void run() {
boolean ok = mCollection.contains(mValue);
if (ok || remainingSearches.decrementAndGet() == 0) {
synchronized (lock) {
if (ok) {
outcome.set(true);
}
lock.notify();
}
}
}
}
/*
*包含多个\u集合现在阻止,直到达到所需的
*值已定位或所有搜索都已完成但未成功。。。
*/
私有布尔值包含多个集合(V值){
//线程池
int numberProcessors=mCollections.size();
ExecutorService es=Executors.newFixedThreadPool(numberProcessors);
对象锁=新对象();
AtomicBoolean结果=新的AtomicBoolean(false);
AtomicInteger remainingSearches=新的AtomicInteger(mCollections.size());
for(int i=0;i0){
试一试{
lock.wait();
}捕获(中断异常例外){
//做些明智的事。
}
}
es.shutdownNow();
}
返回结果get();
}
私有类MyRunnable实现Runnable{
最终摘要收集;
最终V值;
最终对象锁定;
最终原子结果;
最终原子整数剩余搜索;
public MyRunnable(抽象集合mCollection,V mValue,
对象锁,