0

I created two swing worker thread class in my swing application. They are load thread, save thread

  1. Load thread is used to load the data from rest service
  2. Save thread is used to push the data to rest service.

My question is that,

  • How to execute my threads one by one when i create more instance for load thread?

  • Save thread should be run after completing process of existing load thread

Does any one guide me to get solution for this scenario?

Note: I am using Swing Worker class to call rest services.

Murali
  • 341
  • 2
  • 7
  • 24

2 Answers2

0

You should start your save thread in the done method of your load thread. You can pass your saveThread to the loadThread as constructor argument or define it as class member.

This should work for you;

   SaveThread mySaveThread = new SaveThread();
   LoadThread myLoadThread = new LoadThread();

   class LoadThread extends SwingWorker<String, Object> {
       @Override
       public String doInBackground() {
           //do your work 
           return "";
       }

       @Override
       protected void done() {
           try { 
               mySaveThread.execute();
           } catch (Exception ignore) {
           }
       }
   }

   myLoadThread .execute();
kerberos84
  • 300
  • 1
  • 10
  • I can't call save thread automatically because it will be call when user wants to save by clicking button – Murali Oct 29 '13 at 09:55
  • 1
    If you will call the save thread when user clicks the save button, then you make the button enabled in the done method of load thread above. – kerberos84 Oct 29 '13 at 10:52
0

If you want to load data from multiple threads, and save this data from one thread after ALL data will be loaded. You can try to use barriers. For example CountDownLatch can wait while all load threads finished their works.

alex2410
  • 10,904
  • 3
  • 25
  • 41