From c1070bd6aec59bcdbc0549fd9cf5ca6852263c6b Mon Sep 17 00:00:00 2001 From: bixilon Date: Tue, 9 Jun 2020 16:26:09 +0200 Subject: [PATCH] use interrupts in threads(use even less cpu in idle) --- .../java/de/bixilon/minosoft/logging/Log.java | 15 +++++++++------ .../minosoft/protocol/network/Connection.java | 11 ++++++++--- .../minosoft/protocol/network/Network.java | 11 +++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/logging/Log.java b/src/main/java/de/bixilon/minosoft/logging/Log.java index 0d4d28af9..43c40151a 100644 --- a/src/main/java/de/bixilon/minosoft/logging/Log.java +++ b/src/main/java/de/bixilon/minosoft/logging/Log.java @@ -13,8 +13,6 @@ package de.bixilon.minosoft.logging; -import de.bixilon.minosoft.util.Util; - import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; @@ -23,6 +21,7 @@ public class Log { static LogLevel level = LogLevel.PROTOCOL; final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); final static List queue = new ArrayList<>(); + static Thread logThread; public static void log(LogLevel l, String message) { if (l.getId() > level.getId()) { @@ -30,11 +29,11 @@ public class Log { return; } queue.add(String.format("[%s] [%s] %s", timeFormat.format(System.currentTimeMillis()), l.name(), message)); + logThread.interrupt(); } public static void initThread() { - - Thread logThread = new Thread(() -> { + logThread = new Thread(() -> { while (true) { while (queue.size() > 0) { // something to print @@ -44,9 +43,13 @@ public class Log { queue.remove(0); } - Util.sleep(1); - } + try { + // wait for interupt + Thread.sleep(100); + } catch (InterruptedException ignored) { + } + } }); logThread.start(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java index d446841e1..44f481f3f 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java @@ -25,7 +25,6 @@ import de.bixilon.minosoft.protocol.packets.serverbound.status.PacketStatusReque import de.bixilon.minosoft.protocol.protocol.ConnectionState; import de.bixilon.minosoft.protocol.protocol.PacketHandler; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import de.bixilon.minosoft.util.Util; import java.util.ArrayList; @@ -37,6 +36,7 @@ public class Connection { private final ArrayList handlingQueue; private Player player; private ConnectionState state = ConnectionState.DISCONNECTED; + Thread handleThread; private boolean onlyPing; @@ -115,6 +115,7 @@ public class Connection { public void handle(ClientboundPacket p) { handlingQueue.add(p); + handleThread.interrupt(); } public boolean isOnlyPing() { @@ -134,7 +135,7 @@ public class Connection { } private void startHandlingThread() { - Thread handleThread = new Thread(() -> { + handleThread = new Thread(() -> { while (getConnectionState() != ConnectionState.DISCONNECTED) { while (handlingQueue.size() > 0) { try { @@ -145,7 +146,11 @@ public class Connection { } handlingQueue.remove(0); } - Util.sleep(1); + try { + // sleep, wait for an interrupt from other thread + Thread.sleep(100); + } catch (InterruptedException ignored) { + } } }); handleThread.start(); diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Network.java b/src/main/java/de/bixilon/minosoft/protocol/network/Network.java index d27132b84..228b34b37 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Network.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Network.java @@ -40,6 +40,7 @@ public class Network { private boolean encryptionEnabled = false; private Cipher cipherEncrypt; private Cipher cipherDecrypt; + private Thread packetThread; public Network(Connection c) { this.connection = c; @@ -99,6 +100,7 @@ public class Network { byte[] raw = dIn.readNBytes(length); binQueueIn.add(raw); + packetThread.interrupt(); } Util.sleep(1); @@ -118,7 +120,7 @@ public class Network { // read data // safety first, but will not occur // sleep 1 ms - Thread packetThread = new Thread(() -> { + packetThread = new Thread(() -> { // compressed data, makes packets to binary data while (connection.getConnectionState() != ConnectionState.DISCONNECTED) { @@ -181,7 +183,11 @@ public class Network { binQueueIn.remove(0); } - Util.sleep(1); // sleep 1 ms + try { + // sleep, wait for an interrupt from other thread + Thread.sleep(100); + } catch (InterruptedException ignored) { + } } }); @@ -190,6 +196,7 @@ public class Network { public void sendPacket(ServerboundPacket p) { queue.add(p); + packetThread.interrupt(); } public void enableEncryption(SecretKey secretKey) {