为什么释放按钮时工作线程会陷入循环?
Why does the worker thread get stuck in loop when the button is released?
我正在尝试在按住给定按钮的每一秒内在 android 中向 stdout 写入一条消息(我打算稍后在此处放置一个方法)。我在标准的 onTouch 方法中使用开关来检测按钮按下:
protected void setFab(FloatingActionButton fab) {
fab.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
System.out.println("Button pressed");
handleButtonDown();
return true;
}
case MotionEvent.ACTION_UP: {
System.out.println("Button released");
handleButtonUp();
return true;
}
default:
return true;
}
}
});
}
和全局两个全局布尔值:
private boolean captureThreadRunning = false;
private boolean cancelCaptureThread = false;
当释放按钮时停止循环:
public void handleButtonUp() {
cancelCaptureThread = true;
}
但是,当我启动工作线程时,它陷入了无限循环,即使在释放按钮时也是如此,因此应该更改全局布尔值:
public void handleButtonDown() {
System.out.println("Capture thread running: " + captureThreadRunning);
if (!captureThreadRunning) {
System.out.println("Thread starting");
startCaptureThread();
}
}
public void startCaptureThread() {
System.out.println("Thread started");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
captureThreadRunning = true;
System.out.println("Got to try");
while (!cancelCaptureThread) {
System.out.println("Success");
try {
System.out.println("Falling asleep");
Thread.sleep(1000);
System.out.println("Slept 1000");
} catch (InterruptedException e) {
throw new RuntimeException(
"Interrupted.", e);
}
}
} finally {
System.out.println("got to finally");
captureThreadRunning = false;
cancelCaptureThread = false;
}
}
});
thread.run();
}
不仅如此,UI 也会被冻结,这当然不应该,因为我在单独的线程中执行所有操作。当我释放按钮时,循环应该停止,因为布尔值被改变了。
我对这两个线程和 android 都很陌生,所以我想我只是遗漏了一些东西。
但是你确实是在你的主线程中执行你的代码。请注意,您正在调用
thread.run();
所以你的执行路径进入死循环。改成
thread.start();
那将开始一个新的 Thread
我正在尝试在按住给定按钮的每一秒内在 android 中向 stdout 写入一条消息(我打算稍后在此处放置一个方法)。我在标准的 onTouch 方法中使用开关来检测按钮按下:
protected void setFab(FloatingActionButton fab) {
fab.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
System.out.println("Button pressed");
handleButtonDown();
return true;
}
case MotionEvent.ACTION_UP: {
System.out.println("Button released");
handleButtonUp();
return true;
}
default:
return true;
}
}
});
}
和全局两个全局布尔值:
private boolean captureThreadRunning = false;
private boolean cancelCaptureThread = false;
当释放按钮时停止循环:
public void handleButtonUp() {
cancelCaptureThread = true;
}
但是,当我启动工作线程时,它陷入了无限循环,即使在释放按钮时也是如此,因此应该更改全局布尔值:
public void handleButtonDown() {
System.out.println("Capture thread running: " + captureThreadRunning);
if (!captureThreadRunning) {
System.out.println("Thread starting");
startCaptureThread();
}
}
public void startCaptureThread() {
System.out.println("Thread started");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
captureThreadRunning = true;
System.out.println("Got to try");
while (!cancelCaptureThread) {
System.out.println("Success");
try {
System.out.println("Falling asleep");
Thread.sleep(1000);
System.out.println("Slept 1000");
} catch (InterruptedException e) {
throw new RuntimeException(
"Interrupted.", e);
}
}
} finally {
System.out.println("got to finally");
captureThreadRunning = false;
cancelCaptureThread = false;
}
}
});
thread.run();
}
不仅如此,UI 也会被冻结,这当然不应该,因为我在单独的线程中执行所有操作。当我释放按钮时,循环应该停止,因为布尔值被改变了。 我对这两个线程和 android 都很陌生,所以我想我只是遗漏了一些东西。
但是你确实是在你的主线程中执行你的代码。请注意,您正在调用
thread.run();
所以你的执行路径进入死循环。改成
thread.start();
那将开始一个新的 Thread