Actually I'm using threads to print two names simultaneously and I want to stop that execution after pressing the 'e' letter. But I don't want to press 'e' and then hit Enter. I just want to stop by pressing letter 'e'.
import java.io.*;
class multithreads{
public static void main(String[] args) {
thread4 t1=new thread4("firstname",500,500);
thread4 t2=new thread4("Secondname",1000,0);
thread4 t3=new thread4("exit",10,0);
}
}
class thread4 implements Runnable{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Thread t;
String name;
int msg='c';
static boolean over=true;
int t1;
int t2;
thread4(String s,int a,int b){
name=s;
t1=a;
t2=b;
t=new Thread(this,s);
t.start();
}
public void run(){
while(over){
if(name.equals("exit")){
try{
msg=in.read();
try{
t.sleep(t1);
}
catch(InterruptedException e){
System.out.println(e);
}
}catch(Exception eio){
System.out.println(eio);
}
if(msg=='E'){
over=false;
}else{
msg='c';
}
}else{
try{
t.sleep(t1);
}
catch(InterruptedException e){
System.out.println(e);
}
System.out.print(name);
for(int j=name.length();j>0;j--){
System.out.print("\b\b\b");
}
try{
t.sleep(t2);
}
catch(InterruptedException e){
System.out.println(e);
}
}
}
}
}