From 04d6af362fae1cf194284c5577231a9aa4b278b6 Mon Sep 17 00:00:00 2001 From: GeorgCantor Date: Sun, 22 Sep 2024 18:43:33 +0300 Subject: [PATCH] Update DispatchQueuePriority.java --- .../org/telegram/DispatchQueuePriority.java | 79 ++++++++++--------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/DispatchQueuePriority.java b/TMessagesProj/src/main/java/org/telegram/DispatchQueuePriority.java index 29d67c7fc..c417514ea 100644 --- a/TMessagesProj/src/main/java/org/telegram/DispatchQueuePriority.java +++ b/TMessagesProj/src/main/java/org/telegram/DispatchQueuePriority.java @@ -10,46 +10,37 @@ import java.util.concurrent.TimeUnit; public class DispatchQueuePriority { - ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new PriorityBlockingQueue<>(10, new Comparator() { - - @Override - public int compare(Runnable o1, Runnable o2) { - int priority1 = 1; - int priority2 = 1; - if (o1 instanceof PriorityRunnable) { - priority1 = ((PriorityRunnable) o1).priority; - } - if (o2 instanceof PriorityRunnable) { - priority2 = ((PriorityRunnable) o2).priority; - } - return priority2 - priority1; - } - })) { - @Override - protected void beforeExecute(Thread t, Runnable r) { - CountDownLatch latch = pauseLatch; - if (latch != null) { - try { - latch.await(); - } catch (InterruptedException e) { - FileLog.e(e); - } - } - } - }; - + private final ThreadPoolExecutor threadPoolExecutor; private volatile CountDownLatch pauseLatch; public DispatchQueuePriority(String threadName) { + this.threadPoolExecutor = new ThreadPoolExecutor( + 1, + 1, + 60, + TimeUnit.SECONDS, + new PriorityBlockingQueue<>(10, new PriorityRunnableComparator()) + ) { + @Override + protected void beforeExecute(Thread t, Runnable r) { + awaitPauseLatch(); + } + }; + } + private void awaitPauseLatch() { + if (pauseLatch != null) { + try { + pauseLatch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // Restore interrupt status + FileLog.e(e); + } + } } public static Runnable wrap(Runnable runnable, int priority) { - if (priority == 1) { - return runnable; - } else { - return new PriorityRunnable(priority, runnable); - } + return priority == 1 ? runnable : new PriorityRunnable(priority, runnable); } public void postRunnable(Runnable runnable) { @@ -65,10 +56,9 @@ public class DispatchQueuePriority { } public void cancelRunnable(Runnable runnable) { - if (runnable == null) { - return; + if (runnable != null) { + threadPoolExecutor.remove(runnable); } - threadPoolExecutor.remove(runnable); } public void pause() { @@ -81,7 +71,7 @@ public class DispatchQueuePriority { CountDownLatch latch = pauseLatch; if (latch != null) { latch.countDown(); - pauseLatch = null; + pauseLatch = null; // Allow future pauses } } @@ -99,4 +89,19 @@ public class DispatchQueuePriority { runnable.run(); } } + + private static class PriorityRunnableComparator implements Comparator { + @Override + public int compare(Runnable o1, Runnable o2) { + int priority1 = getPriority(o1); + int priority2 = getPriority(o2); + return Integer.compare(priority2, priority1); // Higher priority first + } + + private int getPriority(Runnable runnable) { + return (runnable instanceof PriorityRunnable) + ? ((PriorityRunnable) runnable).priority + : 1; // Default priority + } + } }