From 2ccb0cea9f52a621a7e04481881382ec9a67e79d Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Tue, 20 Sep 2016 18:44:03 +0200 Subject: [PATCH] Replaced Thread.sleep by TimeUnit.SECONDS.sleep and colleagues. Doesn't change the way the code works and still uses Thread.sleep internally, but now the code is much easier to read. --- .../fabianonline/telegram_backup/DownloadManager.java | 11 ++++++----- .../java/de/fabianonline/telegram_backup/Utils.java | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index 6a2ad68..047c617 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -45,6 +45,7 @@ import java.util.List; import java.util.Random; import java.net.URL; import java.util.concurrent.TimeoutException; +import java.util.concurrent.TimeUnit; import java.nio.file.Files; import java.nio.file.StandardCopyOption; @@ -84,7 +85,7 @@ public class DownloadManager { System.out.println(""); System.out.println("Telegram took too long to respond to our request."); System.out.println("I'm going to wait a minute and then try again."); - try { Thread.sleep(60*1000); } catch(InterruptedException e2) {} + try { TimeUnit.MINUTES.sleep(1); } catch(InterruptedException e2) {} System.out.println(""); } } while (!completed); @@ -185,7 +186,7 @@ public class DownloadManager { db.saveUsers(response.getUsers()); logger.trace("Sleeping"); try { - Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES); + TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_MESSAGES); } catch (InterruptedException e) {} } logger.debug("Finished."); @@ -212,7 +213,7 @@ public class DownloadManager { System.out.println(""); System.out.println("Telegram took too long to respond to our request."); System.out.println("I'm going to wait a minute and then try again."); - try { Thread.sleep(60*1000); } catch(InterruptedException e2) {} + try { TimeUnit.MINUTES.sleep(1); } catch(InterruptedException e2) {} System.out.println(""); } } while (!completed); @@ -302,7 +303,7 @@ public class DownloadManager { fos.write(response.getBytes().getData()); fos.flush(); - try { Thread.sleep(Config.DELAY_AFTER_GET_FILE); } catch(InterruptedException e) {} + try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_FILE); } catch(InterruptedException e) {} } while(offset < size && response.getBytes().getData().length>0); fos.close(); if (offset < size) { @@ -322,7 +323,7 @@ public class DownloadManager { } catch (IOException e) { logger.debug("Exception during move. rename_tries: {}. Exception: {}", rename_tries, e); last_exception = e; - try { Thread.sleep(Config.RENAMING_DELAY); } catch (InterruptedException e2) {} + try { TimeUnit.MILLISECONDS.sleep(Config.RENAMING_DELAY); } catch (InterruptedException e2) {} } } if (last_exception != null) { diff --git a/src/main/java/de/fabianonline/telegram_backup/Utils.java b/src/main/java/de/fabianonline/telegram_backup/Utils.java index daa2fb2..5904626 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Utils.java +++ b/src/main/java/de/fabianonline/telegram_backup/Utils.java @@ -19,6 +19,7 @@ import com.github.badoualy.telegram.tl.exception.RpcErrorException; import java.io.File; import java.util.List; import java.util.Vector; +import java.util.concurrent.TimeUnit; import com.google.gson.*; import java.net.URL; import org.apache.commons.io.IOUtils; @@ -59,7 +60,7 @@ public class Utils { "Ctrl+C. You can restart me at any time and I will just continue to download your\n" + "messages and media. But be advised that just restarting me is not going to change\n" + "the fact that Telegram won't talk to me until then."); - try { Thread.sleep((delay + 1) * 1000); } catch(InterruptedException e2) {} + try { TimeUnit.SECONDS.sleep(delay + 1); } catch(InterruptedException e2) {} System.out.println(""); }