JCole
JCole

Reputation: 37

Updating thread at the right moment

I've been having problems with getting my thread to work 100% correctly as currently it is returning my graph already sorted even when I update the thread inside my sorting algorithm while it is still sorting. Maybe there is a piece of code that I may be missing because I feel that I just about have it now.

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;




    }

This is what I did to achieve what I wanted.

  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();
           }
        }

Upvotes: 1

Views: 68

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

Looks like you're overcomplicating this by quite a degree! You've got a SwingWorker, which executes on a background thread; and then that's kicking off another thread of its own.

I think you want your ActionListener to start a SwingWorker to do the sorting, and that can use SwingUtilities.invokeLater() to schedule a repaint whenever it feels like it. Or else do your updates in the process() method, and call publish() to trigger that to do a repaint.

Upvotes: 2

Related Questions