I have a program where I start a new thread to receive input, and then stop the thread after a while and see if i got any input. (Yes, I know stop is deprecated). The print inside run() prints whatever I input, however the one inside play() prints nothing. I’d like to know how I can make response consistent between threads. I’ve tried making response volatile. I’ve also tried having a normal string and changing it inside run(), but this didn’t work at all.
class myClass implements Runnable{
Scanner in = new Scanner(System.in);
StringBuilder response = new StringBuilder();
public void run(){
System.out.println("TYPE!");
response.append(in.nextLine());
System.out.println("You responded: " + response.toString());
}
String play (){
Thread t = new Thread(new qTime());
t.start();
try{ Thread.sleep(3000); } catch (Exception e) { System.out.println("Waiting Failed!"); }
t.stop();
System.out.println(response.toString())
}
}