我在Class1[] QR;
Class2.java
其中Class1是一个不同的类,它扩展了Task,我正在那里做一些事情。
我在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 QueuyeReaderExecutorService;
@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
QueuyeReaderExecutorService = 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);
QueuyeReaderExecutorService.execute(QR[i]);
}
}
所以我在进度条旁边看到10个“停止”按钮。因此,一旦我启动应用程序,所有线程都会开始运行。
我的目标是在用户点击它时停止特定线程。为此我已经睡了5秒钟。但问题是,当我点击任何一个STOP按钮时,它 将所有线程置于睡眠模式。
请告诉我这里我做错了什么。
2)我应该如何终止特定线程而不是将其置于睡眠模式?
由于
答案 0 :(得分:0)