[英]Future.cancel and ReentrantLocks
设想
我有这个 class,比如说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()
并等待另一个 class,比如说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
继续执行。 抱歉冗长,但我想给你们一个清晰的画面。 有什么建议么?
除非任务在中断时停止,否则标志将被设置并保持设置,直到任务结束或决定停止。
http://www.google.co.uk/search?q=how+do+I+stop+a+thread+in+Java 28,000,000 个结果的可能重复项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.