mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 18:25:51 +00:00
Threading: fix the user thread to be daemon so programs can shut down properly again.
This commit is contained in:
parent
a8a8d3a044
commit
573e04491c
@ -21,10 +21,7 @@ import com.google.common.util.concurrent.CycleDetectingLockFactory;
|
|||||||
import com.google.common.util.concurrent.Futures;
|
import com.google.common.util.concurrent.Futures;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
@ -51,7 +48,7 @@ public class Threading {
|
|||||||
public static final Executor SAME_THREAD;
|
public static final Executor SAME_THREAD;
|
||||||
|
|
||||||
// For safety reasons keep track of the thread we use to run user-provided event listeners to avoid deadlock.
|
// For safety reasons keep track of the thread we use to run user-provided event listeners to avoid deadlock.
|
||||||
private static final Thread executorThread;
|
private static Thread userThread;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
|
* Put a dummy task into the queue and wait for it to be run. Because it's single threaded, this means all
|
||||||
@ -64,7 +61,7 @@ public class Threading {
|
|||||||
// If this assert fires it means you have a bug in your code - you can't call this method inside your own
|
// If this assert fires it means you have a bug in your code - you can't call this method inside your own
|
||||||
// event handlers because it would never return. If you aren't calling this method explicitly, then that
|
// event handlers because it would never return. If you aren't calling this method explicitly, then that
|
||||||
// means there's a bug in bitcoinj.
|
// means there's a bug in bitcoinj.
|
||||||
checkState(executorThread != Thread.currentThread(), "waitForUserCode() run on user code thread would deadlock.");
|
checkState(userThread != Thread.currentThread(), "waitForUserCode() run on user code thread would deadlock.");
|
||||||
Futures.getUnchecked(USER_THREAD.submit(Callables.returning(null)));
|
Futures.getUnchecked(USER_THREAD.submit(Callables.returning(null)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,15 +73,15 @@ public class Threading {
|
|||||||
// from that point onwards.
|
// from that point onwards.
|
||||||
throwOnLockCycles();
|
throwOnLockCycles();
|
||||||
|
|
||||||
USER_THREAD = Executors.newSingleThreadExecutor();
|
USER_THREAD = Executors.newSingleThreadExecutor(new ThreadFactory() {
|
||||||
// We can't directly get the thread that was just created, but we can fetch it indirectly. We'll use this
|
@Nonnull @Override public Thread newThread(@Nonnull Runnable runnable) {
|
||||||
// for deadlock detection by checking for waits on the user code thread.
|
checkState(userThread == null, "Not single threaded anymore?");
|
||||||
executorThread = Futures.getUnchecked(USER_THREAD.submit(new Callable<Thread>() {
|
userThread = new Thread(runnable);
|
||||||
@Override public Thread call() throws Exception {
|
userThread.setName("bitcoinj user thread");
|
||||||
Thread.currentThread().setName("bitcoinj user code thread");
|
userThread.setDaemon(true);
|
||||||
return Thread.currentThread();
|
return userThread;
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
SAME_THREAD = new Executor() {
|
SAME_THREAD = new Executor() {
|
||||||
@Override
|
@Override
|
||||||
public void execute(@Nonnull Runnable runnable) {
|
public void execute(@Nonnull Runnable runnable) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user