I'm trying to understand what is the correct way to implement in java a program that continuously read messages from the console and according to the message it performs different operations.
At the moment I have created two threads one that takes care of reading messages from the console, the other to perform operations, these operations not in all cases must stop reading console input
this is the code of thread1 which is started from the main and then continue in its reading cycle from the console.
public class Thread1 implements Runnable{
public void run() {
String input;
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("Insert command:");
//edited part
try {
input = scanner.nextLine();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
scanner.close();
break;
}
String[] strings = input.split(" ");
switch (strings[0]) {
case "something":
//do something
break;
...
case "login":
//something...
//here I start second thread
Sender senderTask = new Sender(Thread.currentThread());
Thread senderThread = new Thread(senderTask);
senderThread.start();
break;
//this thread now does not end but waits for other input messages
default:
break;
}
}
}
}
the senderThread performs operations and at some point I don't want thread1 to continue reading console input, it is stuck on scanner.nextLine()
waiting for input. So I thought about stopping it using thread.stop()
is it correct? is there a better way?
this is the senderThread code
public class Sender implements Runnable {
private Thread thread1;
public Sender(Thread thread1) {
this.thread1 = thread1;
}
public void run() {
while(true) {
//in short here I am waiting for an udp package, if it arrives I don't want to read the messages from the console anymore
//so somehow I end thread1 and I have to try to close the scanner, is it right to do it this way?
//I know stop() is deprecated, is there something else that I can use?
//edited part
thread1.interrupt();
//here I create a new scanner because after I received the udp packet
//I want to read a message from the console that is different from those that go to the switch case of thread1
Scanner scanner = new Scanner(System.in);
String msg = scanner.nextLine();
if(msg.equals("y")){
//do something
break;
}
}
if(thread1.isAlive()==false) {
thread1.start();
}
}
}
at the end I start thread1 again which reads messages from the console