How to stop threads using the key combination?

Good time of day. I am new to JAVA and I would like to know how to stop threads using CTRL + C in a console application. He tried to do something( for example the book took, but there was no stopping through the combination, he tried to do, unsuccessfully).
I will be very glad for help
package lab2;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.An InputStreamReader is;

class Q {
 int n;
 boolean valueSet = false;

 synchronized int get() { 
while(!valueSet)
 try {
 wait () ;
 } catch(InterruptedException e) { 
 System.out.println("InterruptedException intercepted");
}
 System.out.println("Received:" + n) ; 
 valueSet = false;
 notify(); 
 return n;
}

 synchronized void put(int n) { 
while(valueSet)
 try { 
wait();
 } catch(InterruptedException e) { 
 System.out.println("InterruptedException intercepted");
}
 this.n = n; 
 valueSet = true;
 System.out.println("Sent:" + n); notify();
}

}

class Producer implements Runnable {
 boolean pushButton;
 Q Q; 

 Producer(Q Q) { 
 this.q = q;
 new Thread(this, "Supplier").start() ;
}

 public void run() { 
 int i = 0;
 while(pushButton = true) {
q.put(i++);
}

 BufferedReader br = new BufferedReader(new an InputStreamReader(System.in));
 try 
{
 int ascii = br.read();
 while (ascii != 10){
 pushButton = false; 
}

}
 catch (IOException e)
{
e.printStackTrace();
 } 
}

}

class Consumer implements Runnable {
 boolean pushButton;
 Q Q;
 Consumer(Q Q) { 
 this.q = q;
 new Thread(this, "Consumer").start();
}

public void run() {

 while(pushButton = true) {
 q. get();
}

 BufferedReader br = new BufferedReader(new an InputStreamReader(System.in));
 try 
{
 int ascii = br.read();
 while (ascii != 10){
 pushButton = false; 
}

}
 catch (IOException e)
{
e.printStackTrace();
 } 
}
}

class Lab2 { 
 public static void main(String[] args) 
{

 Q Q = new Q ();
 new Producer(q);
 new Consumer(q);

 } 

}
July 12th 19 at 17:06
1 answer
July 12th 19 at 17:08
On windows try Ctrl+D

Find more questions by tags Java