From 99bc95a93d14a72402afc62463951401e6998a60 Mon Sep 17 00:00:00 2001
From: Andreas Schildbach
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. + *
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. 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.
* - *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.
+ *An event listener can be provided. It will be called on a background thread + * with the wallet locked when an auto-save occurs.
* * @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); + } + /** *
* Disables auto-saving, after it had been enabled with
diff --git a/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java b/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java
index d1652505d..7160414f5 100644
--- a/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java
+++ b/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java
@@ -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