mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-23 22:46:56 +01:00
WalletFiles: migrate delay
field to java.time
API
This commit is contained in:
parent
d3162a4403
commit
99bc95a93d
2 changed files with 24 additions and 16 deletions
|
@ -113,6 +113,7 @@ import java.io.OutputStream;
|
|||
import java.math.BigInteger;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
@ -1600,29 +1601,26 @@ public class Wallet extends BaseTaggableObject
|
|||
* consistency. After connecting to a file, you no longer need to save the wallet manually, it will do it
|
||||
* whenever necessary. Protocol buffer serialization will be used.</p>
|
||||
*
|
||||
* <p>If delayTime is set, a background thread will be created and the wallet will only be saved to
|
||||
* disk every so many time units. If no changes have occurred for the given time period, nothing will be written.
|
||||
* <p>A background thread will be created and the wallet will only be saved to
|
||||
* disk every periodically. If no changes have occurred for the given time period, nothing will be written.
|
||||
* In this way disk IO can be rate limited. It's a good idea to set this as otherwise the wallet can change very
|
||||
* frequently, eg if there are a lot of transactions in it or during block sync, and there will be a lot of redundant
|
||||
* writes. Note that when a new key is added, that always results in an immediate save regardless of
|
||||
* delayTime. <b>You should still save the wallet manually using {@link Wallet#saveToFile(File)} when your program
|
||||
* is about to shut down as the JVM will not wait for the background thread.</b></p>
|
||||
*
|
||||
* <p>An event listener can be provided. If a delay greater than 0 was specified, it will be called on a background thread
|
||||
* with the wallet locked when an auto-save occurs. If delay is zero or you do something that always triggers
|
||||
* an immediate save, like adding a key, the event listener will be invoked on the calling threads.</p>
|
||||
* <p>An event listener can be provided. It will be called on a background thread
|
||||
* with the wallet locked when an auto-save occurs.</p>
|
||||
*
|
||||
* @param f The destination file to save to.
|
||||
* @param delayTime How many time units to wait until saving the wallet on a background thread.
|
||||
* @param timeUnit the unit of measurement for delayTime.
|
||||
* @param delay How much time to wait until saving the wallet on a background thread.
|
||||
* @param eventListener callback to be informed when the auto-save thread does things, or null
|
||||
*/
|
||||
public WalletFiles autosaveToFile(File f, long delayTime, TimeUnit timeUnit,
|
||||
@Nullable WalletFiles.Listener eventListener) {
|
||||
public WalletFiles autosaveToFile(File f, Duration delay, @Nullable WalletFiles.Listener eventListener) {
|
||||
lock.lock();
|
||||
try {
|
||||
checkState(vFileManager == null, "Already auto saving this wallet.");
|
||||
WalletFiles manager = new WalletFiles(this, f, delayTime, timeUnit);
|
||||
WalletFiles manager = new WalletFiles(this, f, delay);
|
||||
if (eventListener != null)
|
||||
manager.setListener(eventListener);
|
||||
vFileManager = manager;
|
||||
|
@ -1632,6 +1630,12 @@ public class Wallet extends BaseTaggableObject
|
|||
}
|
||||
}
|
||||
|
||||
/** @deprecated use {@link #autosaveToFile(File, Optional, WalletFiles.Listener)} */
|
||||
@Deprecated
|
||||
public WalletFiles autosaveToFile(File f, long delayTime, TimeUnit timeUnit, @Nullable WalletFiles.Listener eventListener) {
|
||||
return autosaveToFile(f, Duration.ofMillis(timeUnit.toMillis(delayTime)), eventListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Disables auto-saving, after it had been enabled with
|
||||
|
|
|
@ -50,8 +50,7 @@ public class WalletFiles {
|
|||
private final ScheduledThreadPoolExecutor executor;
|
||||
private final File file;
|
||||
private final AtomicBoolean savePending;
|
||||
private final long delay;
|
||||
private final TimeUnit delayTimeUnit;
|
||||
private final Duration delay;
|
||||
private final Callable<Void> saver;
|
||||
|
||||
private volatile Listener vListener;
|
||||
|
@ -77,7 +76,7 @@ public class WalletFiles {
|
|||
* saved automatically. The {@link Wallet} calls {@link #saveNow()} or {@link #saveLater()} as wallet state changes,
|
||||
* depending on the urgency of the changes.
|
||||
*/
|
||||
public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) {
|
||||
public WalletFiles(final Wallet wallet, File file, Duration delay) {
|
||||
// An executor that starts up threads when needed and shuts them down later.
|
||||
this.executor = new ScheduledThreadPoolExecutor(1, new ContextPropagatingThreadFactory("Wallet autosave thread", Thread.MIN_PRIORITY));
|
||||
this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
|
||||
|
@ -87,8 +86,7 @@ public class WalletFiles {
|
|||
// File must only be accessed from the auto-save executor from now on, to avoid simultaneous access.
|
||||
this.file = checkNotNull(file);
|
||||
this.savePending = new AtomicBoolean();
|
||||
this.delay = delay;
|
||||
this.delayTimeUnit = checkNotNull(delayTimeUnit);
|
||||
this.delay = checkNotNull(delay);
|
||||
|
||||
this.saver = () -> {
|
||||
// Runs in an auto save thread.
|
||||
|
@ -106,6 +104,12 @@ public class WalletFiles {
|
|||
};
|
||||
}
|
||||
|
||||
/** @deprecated use {@link #WalletFiles(Wallet, File, Duration)} */
|
||||
@Deprecated
|
||||
public WalletFiles(final Wallet wallet, File file, long delayTime, TimeUnit timeUnit) {
|
||||
this(wallet, file, Duration.ofMillis(timeUnit.toMillis(delayTime)));
|
||||
}
|
||||
|
||||
/** Get the {@link Wallet} this {@link WalletFiles} is managing. */
|
||||
public Wallet getWallet() {
|
||||
return wallet;
|
||||
|
@ -151,7 +155,7 @@ public class WalletFiles {
|
|||
public void saveLater() {
|
||||
if (executor.isShutdown() || savePending.getAndSet(true))
|
||||
return; // Already pending.
|
||||
executor.schedule(saver, delay, delayTimeUnit);
|
||||
executor.schedule(saver, delay.toMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/** Shut down auto-saving. */
|
||||
|
|
Loading…
Add table
Reference in a new issue