在我看来,一旦线程开始通过System.console().readLine()
或System.in.read()
读取输入,宇宙中绝对没有办法在功能上中断读取,除了System.exit()
或提供输入
interrupt()
阅读线程什么也没做。即使stop()
它也什么都不做。在close()
期间System.in
System.in.read()
{{1}}在读取完成后通过提供输入做任何事情。 read方法不会使用任何超时参数,也不会自行超时。
根本没有办法“解锁”等待永远不会出现的控制台输入的线程吗?
答案 0 :(得分:0)
我基于此article
创建了一个变体//TOUCH LISTENER
theCanvas.addEventListener("touchstart", startT, false);
theCanvas.addEventListener("touchend", endT, false);
theCanvas.addEventListener("touchmove", moveT, false);
//MOUSE LISTENER
theCanvas.addEventListener("mousedown", start);
theCanvas.addEventListener("mouseup", end);
theCanvas.addEventListener("mousemove", move, false);
//MOUSE EVENTS
function start(event) {
if (event.button === MAIN_MOUSE_BUTTON) {
shouldDraw = true;
setLineProperties(theContext);
theContext.beginPath();
let elementRect = event.target.getBoundingClientRect();
theContext.moveTo(event.clientX - elementRect.left, event.clientY - elementRect.top);
console.log(PenType(mode));
}
}
function end(event) {
if (event.button === MAIN_MOUSE_BUTTON) {
shouldDraw = false;
}
}
function move(event) {
if (shouldDraw) {
let elementRect = event.target.getBoundingClientRect();
theContext.lineTo(event.clientX - elementRect.left, event.clientY - elementRect.top);
theContext.stroke()
}
}
//TOUCH EVENTS
function startT(event) {
event.preventDefault();
shouldDraw = true;
setLineProperties(theContext);
theContext.beginPath();
let elementRect = event.target.getBoundingClientRect();
theContext.moveTo(event.clientX - elementRect.left, event.clientY - elementRect.top);
console.log(PenType(mode));
}
function endT(event) {
if (event.touches.length == 1) {
event.preventDefault();
shouldDraw = false;
}
}
function moveT(event) {
if (shouldDraw) {
event.preventDefault();
let elementRect = event.target.getBoundingClientRect();
theContext.lineTo(event.clientX - elementRect.left, event.clientY - elementRect.top);
theContext.stroke()
}
}
主程序的一部分
let myDate = getDateSomewhere();
Date.parse(myDate);
您可以通过在另一个线程中调用
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;
public class ConsoleInputReadTask implements Callable<String> {
private boolean cont = true;
public void stop() {
cont = false;
}
public String call() throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("ConsoleInputReadTask run() called.");
String input;
try {
// wait until we have data to complete a readLine()
while (!br.ready()) {
Thread.sleep(200);
if (!cont) {
System.out.println("ConsoleInputReadTask() stopped");
return null;
}
}
input = br.readLine();
} catch (InterruptedException e) {
System.out.println("ConsoleInputReadTask() cancelled");
return null;
}
return input;
}
}
来停止 private ConsoleInputReadTask consoleInputReadTask;
public String readLine() throws InterruptedException {
ExecutorService ex = Executors.newSingleThreadExecutor();
String input = null;
try {
Future<String> result = ex.submit(consoleInputReadTask);
try {
input = result.get();
} catch (ExecutionException e) {
e.getCause().printStackTrace();
}
} finally {
ex.shutdownNow();
}
return input;
}
public void test() {
consoleInputReadTask = new ConsoleInputReadTask();
while ((line = readLine()) != null) {
// ...
}