【问题标题】:My stop button is pausing all the thread instead of pausing specific thread我的停止按钮正在暂停所有线程而不是暂停特定线程
【发布时间】: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 按钮时,它 使所有线程进入睡眠模式。

请让我知道我在这里做错了什么。

  1. 我应该如何终止特定线程而不是将其置于睡眠模式?

谢谢

【问题讨论】:

    标签: java multithreading javafx


    【解决方案1】:
    1. 您的任务实施执行什么样的工作?请提供更多代码或信息。
    2. 使用Future怎么样?您可以通过 ExecutorService submit(task) 方法调用而不是 execute(task) 来获取它,后者不提供任务完成合约。 Future 可能随时取消底层任务。我认为这是您正在寻找的东西。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2019-08-09
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多