3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 18:55:52 +00:00

WalletFiles: use a ContextPropagatingThreadFactory instead of propagating the context explicitly.

Fixes bug found by devrandom
This commit is contained in:
Mike Hearn 2015-07-14 13:15:33 +02:00
parent 1352264e86
commit a52c8669a4

View File

@ -18,18 +18,15 @@
package org.bitcoinj.wallet; package org.bitcoinj.wallet;
import org.bitcoinj.core.*; import org.bitcoinj.core.*;
import org.bitcoinj.utils.Threading; import org.bitcoinj.utils.*;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import org.slf4j.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull; import javax.annotation.*;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.*;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.*;
/** /**
* A class that handles atomic and optionally delayed writing of the wallet file to disk. In future: backups too. * A class that handles atomic and optionally delayed writing of the wallet file to disk. In future: backups too.
@ -67,15 +64,8 @@ public class WalletFiles {
} }
public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) { public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) {
final ThreadFactoryBuilder builder = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("Wallet autosave thread")
.setPriority(Thread.MIN_PRIORITY); // Avoid competing with the GUI thread.
Thread.UncaughtExceptionHandler handler = Threading.uncaughtExceptionHandler;
if (handler != null)
builder.setUncaughtExceptionHandler(handler);
// An executor that starts up threads when needed and shuts them down later. // An executor that starts up threads when needed and shuts them down later.
this.executor = new ScheduledThreadPoolExecutor(1, builder.build()); this.executor = new ScheduledThreadPoolExecutor(1, new ContextPropagatingThreadFactory("Wallet autosave thread", Thread.MIN_PRIORITY));
this.executor.setKeepAliveTime(5, TimeUnit.SECONDS); this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
this.executor.allowCoreThreadTimeOut(true); this.executor.allowCoreThreadTimeOut(true);
this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
@ -88,7 +78,6 @@ public class WalletFiles {
this.saver = new Callable<Void>() { this.saver = new Callable<Void>() {
@Override public Void call() throws Exception { @Override public Void call() throws Exception {
Context.propagate(wallet.getContext());
// Runs in an auto save thread. // Runs in an auto save thread.
if (!savePending.getAndSet(false)) { if (!savePending.getAndSet(false)) {
// Some other scheduled request already beat us to it. // Some other scheduled request already beat us to it.