我有以下Runnable
实施:
public class Example implements Runnable
{
private Thread runThread = null;
// Runs in its own thread
@Override
public void run() {
assert runThread == null; // must not call run() twice
runThread = Thread.currentThread();
try {
while (!Thread.currentThread().isInterrupted()) {
// do something
}
} finally {
runThread = null;
}
}
// Might be called from another thread
public void stop() {
Thread t = runThread; // use temporary variable to avoid race condition with run()
if (t != null) {
// without the temporary variable, the run() thread could be set to null at this point
t.interrupt();
}
}
}
我对此代码有两个问题:
Example
实例的所有者处理?