I have created 2 threads that works together for a socket connection: SocketIn and SocketOut (they are both working in a while(true) statement. In certain moments i need the SocketOut thread stop waiting for keyboard input a keep executing the following instructions even if he had read a null value and i need to do that without closing the thread or getting out the while(true). I have no idea on how to do that, so I need some advices.
Look into CSP programming and BlockingQueues. You could use a Queue in your loop that accepts "commands". One of the command is input from the socket, the other command is input from the keyboard.
private final BlockingQueue<Command> inputChannel = new SynchronousQueue<>();
public void run() {
while (true) {
Command request = this.inputChannel.take();
if (request instanceof Command.KeyboardInput) {
...
} else {
...
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With