【发布时间】:2014-04-01 17:40:08
【问题描述】:
我在Class2.java 中定义了Class1[] QR;
Class1 是一个不同的类,它扩展了Task<Integer>,我正在那里做一些事情。
我在 Class2 中定义了以下变量,如下所示:
public class Class2 implements Initializable {
private ProgressBar[] prog_QR;
private Label[] lab_QR;
public Button[] stop_QR;
public Class1[] QR;
public boolean RunningQRState;
ExecutorService QueueReaderExecutorService;
@Override
public void initialize(URL url, ResourceBundle rb)
{
QR = new ResultsReader[10];
prog_QR = new ProgressBar[10];
lab_QR = new Label[10];
stop_QR = new Button[10];
for(int i=0; i<10; i++)
{
prog_QR[i] = new ProgressBar();
stop_QR[i] = new Button();
stop_QR[i].setText("Stop");
lab_QR[i] = new Label("Thread " + i);
// more stuff
}
@FXML
private void act_Start(ActionEvent event)
{
if(RunningQRState)
{
RunningQRState = false;
}
else
{
RunningQRState = true;
// Set up thread pool
QueueReaderExecutorService = Executors.newFixedThreadPool(10);
for(int i=0; i<10; i++)
{
QR[i] = new Class1(i);
prog_QR[i].progressProperty().bind(QR[i].progressProperty());
lab_QR[i].textProperty().bind(QR[i].messageProperty());
//stop_QR[i].textProperty().bind(QR[i].messageProperty());// By AK
stop_QR[i].setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("Stop Button number Clicked");
try
{
Thread.sleep(5000);
}
catch (InterruptedException interrupted)
{
if (isCancelled())
{
System.out.println("QRC Cancelled");
}
}
}
private boolean isCancelled() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
//QR[i].setQDVC(this);
QueueReaderExecutorService.execute(QR[i]);
}
}
所以我在进度条旁边看到了 10 个“停止”按钮。所以,一旦我启动应用程序,所有线程都会开始运行。
我的目标是在用户单击特定线程时停止它。为此,我已经休眠了 5 秒。但问题是,当我点击任何一个 STOP 按钮时,它 使所有线程进入睡眠模式。
请让我知道我在这里做错了什么。
- 我应该如何终止特定线程而不是将其置于睡眠模式?
谢谢
【问题讨论】:
标签: java multithreading javafx