我想在按下按钮时运行一个线程
public void ButtonClick(){
Thread thread = new Thread(){
public void run(){
Log.i("Test", "I'm in thread");
}
};
thread.start();
}
我的问题是:我想多次点击这个按钮。打印“I'm in thread”后是否还有多个线程存在?还是每次run函数结束,线程就被销毁?
如果我创建了多个同时运行的线程,我该如何以干净的方式关闭它们?
感谢您的帮助!
最佳答案
Are several thread still existing after the message "I'm in thread" is printed?
没有。它们中的每一个都会被自动销毁。
In case I create several threads which are running at the same time, how can I close them in a clean way?
无需停止线程,一旦完成任务(执行run),它们就会自动销毁。
要处理并发性和安全性,您应该查看 java.util.concurrent
这是在 Java 中处理并发的实用框架。
关于java - 新线程多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34331843/