【发布时间】:2011-08-14 18:37:15
【问题描述】:
场景
我有这个类,比如说Foo,它唯一的工作就是顺序执行一组任务。听起来很简单,对吧?嗯,所有这些任务都在它们自己的独立线程中执行。它不是简单地在每个任务上调用Thread.join() 以确保顺序执行,而是使用ReentrantLock 和Condition。例如,
public class Foo
{
private final Lock lock;
private final Condition condition;
private Future<?> task;
public Foo()
{
lock = new ReentrantLock();
condition = lock.newCondition();
run();
}
/**
* Runs Foo
*/
public void run()
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
lock.lock();
try
{
// do tasks
}
finally
{
lock.unlock();
}
}
};
// Submit task
task = Executors.newSingleThreadExecutor().submit(runnable);
}
/**
* Blocks execution until signal is received
*/
public void waitForNotification()
{
try
{
condition.await();
}
catch (InterruptedException e)
{
}
}
/**
* Gets the lock
*
* @return Lock
*/
public final Lock getLock()
{
return lock;
}
/**
* Gets the condition
*
* @return Condition
*/
public final Condition getCondition()
{
return condition;
}
/**
* Gets the sender's task
*
* @return Task
*/
public Future<?> getTask()
{
return task;
}
}
在每个任务之后,waitForNotification() 被调用并等待另一个类,比如Bar,以唤醒它。 Bar 的唯一工作是处理通知应用程序任务是通过还是失败的响应。话虽如此,它将做以下两件事之一:
-
完全取消话题(即
getTask().cancel) -
唤醒线程(即
getCondition().signal)
第 2 项工作正常,但第 1 项不行。例如,
public class Bar
{
private Foo foo;
// doesn't work!
public void handleFailed()
{
// Cancel task
foo.getTask().cancel(true)
}
public void handlePassed()
{
// Signal waiting Foo
foo.getLock().lock();
try
{
foo.getCondition().signal();
}
finally
{
foo.getLock().unlock();
}
}
}
而不是取消线程,它似乎只是中断它,这导致Foo 继续执行。抱歉冗长,但我想给你们一个清晰的画面。有什么建议吗?
【问题讨论】:
标签: java conditional-statements future reentrantlock