在适当的时候更新线程

时间:2014-12-02 17:20:22

标签: java multithreading swing timer

我一直遇到让我的线程100%正确工作的问题,因为它正在返回已经排序的图表,即使我在排序算法中更新线程时仍在排序。也许有一段代码我可能会丢失,因为我觉得我现在就拥有它。

private class ButtonHandler implements ActionListener
{

   public void actionPerformed(ActionEvent e)
   {
     Object src = e.getSource();
     if (src == button1){

       int[] array2=array;
       for (int i = 1; i < array2.length; i++) {
            int thingToInsert = array2[i];

            int j = i - 1;
            while (j >= 0 && thingToInsert<array2[j]) {
            array2[j+1] = array2[j];
            j--;
            }

            array2[j+1] = thingToInsert;
            (new UpdateTextFieldThread()).execute();
        }

     }
     }

}

 private class UpdateTextFieldThread extends SwingWorker<Void, Integer> 
  {
     static final int THREAD_DELAY = 1000;
     protected Void doInBackground()
    {
         ExecutorService service = Executors.newSingleThreadExecuto();
         try {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                       Thread.sleep(THREAD_DELAY);
                       display.repaint();
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
               }
            };
            Future<?> f = service.submit(r);

            f.get(2, TimeUnit.MINUTES); 
            }
            catch (final InterruptedException e) {
                // The thread was interrupted during sleep, wait or join
            }
            catch (final TimeoutException e) {
                // Took too long!
            }
            catch (final ExecutionException e) {
                // An exception from within the Runnable task
            }
            finally {
                service.shutdown();
            }
        return null;




    }

这就是我为实现我想要的目标所做的。

  private class UpdateTextFieldThread extends SwingWorker<Void, Integer[]> 
  {
     static final int THREAD_DELAY = 1000;
     protected Void doInBackground()
    {
        if(base==1){ 
         array2=array;
                    try {

                         Integer[] array3=array;
                           for  (int i = 1; i < array3.length; i++) {
                            Thread.sleep(THREAD_DELAY);

                               int thingToInsert = array3[i];

                                int j = i - 1;
                                while (j >= 0 && thingToInsert<array3[j]) {
                                array3[j+1] = array3[j];
                                j--;
                                }

                                array3[j+1] = thingToInsert;
                            publish(array3);
                        }



                     } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                        }

        return null;
    }
        protected void process(java.util.List<Integer[]> list)
        {
                  array2=list.get(list.size()-1);

               display.repaint();
           }
        }

1 个答案:

答案 0 :(得分:2)

看起来你在相当程度上过度复杂了!你有一个SwingWorker,它在后台线程上执行;然后那就开始了另一个自己的线程。

我认为您希望ActionListener启动SwingWorker进行排序,并且可以使用SwingUtilities.invokeLater()安排重新绘制。或者使用process()方法进行更新,然后调用publish()来触发重新绘制。