use interrupts in threads(use even less cpu in idle)

This commit is contained in:
bixilon 2020-06-09 16:26:09 +02:00
parent 09d0a34737
commit c1070bd6ae
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 26 additions and 11 deletions

View File

@ -13,8 +13,6 @@
package de.bixilon.minosoft.logging; package de.bixilon.minosoft.logging;
import de.bixilon.minosoft.util.Util;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -23,6 +21,7 @@ public class Log {
static LogLevel level = LogLevel.PROTOCOL; static LogLevel level = LogLevel.PROTOCOL;
final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
final static List<String> queue = new ArrayList<>(); final static List<String> queue = new ArrayList<>();
static Thread logThread;
public static void log(LogLevel l, String message) { public static void log(LogLevel l, String message) {
if (l.getId() > level.getId()) { if (l.getId() > level.getId()) {
@ -30,11 +29,11 @@ public class Log {
return; return;
} }
queue.add(String.format("[%s] [%s] %s", timeFormat.format(System.currentTimeMillis()), l.name(), message)); queue.add(String.format("[%s] [%s] %s", timeFormat.format(System.currentTimeMillis()), l.name(), message));
logThread.interrupt();
} }
public static void initThread() { public static void initThread() {
logThread = new Thread(() -> {
Thread logThread = new Thread(() -> {
while (true) { while (true) {
while (queue.size() > 0) { while (queue.size() > 0) {
// something to print // something to print
@ -44,9 +43,13 @@ public class Log {
queue.remove(0); queue.remove(0);
} }
Util.sleep(1); try {
} // wait for interupt
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
}
}); });
logThread.start(); logThread.start();
} }

View File

@ -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.ConnectionState;
import de.bixilon.minosoft.protocol.protocol.PacketHandler; import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import de.bixilon.minosoft.util.Util;
import java.util.ArrayList; import java.util.ArrayList;
@ -37,6 +36,7 @@ public class Connection {
private final ArrayList<ClientboundPacket> handlingQueue; private final ArrayList<ClientboundPacket> handlingQueue;
private Player player; private Player player;
private ConnectionState state = ConnectionState.DISCONNECTED; private ConnectionState state = ConnectionState.DISCONNECTED;
Thread handleThread;
private boolean onlyPing; private boolean onlyPing;
@ -115,6 +115,7 @@ public class Connection {
public void handle(ClientboundPacket p) { public void handle(ClientboundPacket p) {
handlingQueue.add(p); handlingQueue.add(p);
handleThread.interrupt();
} }
public boolean isOnlyPing() { public boolean isOnlyPing() {
@ -134,7 +135,7 @@ public class Connection {
} }
private void startHandlingThread() { private void startHandlingThread() {
Thread handleThread = new Thread(() -> { handleThread = new Thread(() -> {
while (getConnectionState() != ConnectionState.DISCONNECTED) { while (getConnectionState() != ConnectionState.DISCONNECTED) {
while (handlingQueue.size() > 0) { while (handlingQueue.size() > 0) {
try { try {
@ -145,7 +146,11 @@ public class Connection {
} }
handlingQueue.remove(0); handlingQueue.remove(0);
} }
Util.sleep(1); try {
// sleep, wait for an interrupt from other thread
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
} }
}); });
handleThread.start(); handleThread.start();

View File

@ -40,6 +40,7 @@ public class Network {
private boolean encryptionEnabled = false; private boolean encryptionEnabled = false;
private Cipher cipherEncrypt; private Cipher cipherEncrypt;
private Cipher cipherDecrypt; private Cipher cipherDecrypt;
private Thread packetThread;
public Network(Connection c) { public Network(Connection c) {
this.connection = c; this.connection = c;
@ -99,6 +100,7 @@ public class Network {
byte[] raw = dIn.readNBytes(length); byte[] raw = dIn.readNBytes(length);
binQueueIn.add(raw); binQueueIn.add(raw);
packetThread.interrupt();
} }
Util.sleep(1); Util.sleep(1);
@ -118,7 +120,7 @@ public class Network {
// read data // read data
// safety first, but will not occur // safety first, but will not occur
// sleep 1 ms // sleep 1 ms
Thread packetThread = new Thread(() -> { packetThread = new Thread(() -> {
// compressed data, makes packets to binary data // compressed data, makes packets to binary data
while (connection.getConnectionState() != ConnectionState.DISCONNECTED) { while (connection.getConnectionState() != ConnectionState.DISCONNECTED) {
@ -181,7 +183,11 @@ public class Network {
binQueueIn.remove(0); 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) { public void sendPacket(ServerboundPacket p) {
queue.add(p); queue.add(p);
packetThread.interrupt();
} }
public void enableEncryption(SecretKey secretKey) { public void enableEncryption(SecretKey secretKey) {