4 Replies Latest reply: Jun 13, 2008 8:04 AM by 807591 RSS

    serialized working threads: join or syncronize?

    807591
      My problem is fairly simple: I'm using a GUI that calls a number of working threads (ex A, B, C, D, ...), one after another. The situation is that each working thread has to wait while the previously called threads finishes its job (the order is obviously A, B, C, D, ...).
      My question is which is the fastest way to solve this? I thought about two options:
      1. use join() and let B called from A; C from B and so on? BUT in this case will the GUI be also affected? Will it "freeze"?
      2. use some kind of manual syncronization: each thread is called normally with a global value that determines which thread should actually run? With a while loop at the start of the run() that only finishes when the global value is the one for the current thread. Like here penultimate post (by Shadowics). BUT in that case will they slow down each other, because each thread would run its own while loop.

      Or
      3. any other possibility please?

      Just to mention, all threads work with file IO and huge database, therefore speed it's crutial
        • 1. Re: serialized working threads: join or syncronize?
          807591
          If these background activities must execute one after the other, then why give each it's own thread? Why not simply run them one after the other on the same thread?
          • 2. Re: serialized working threads: join or syncronize?
            807591
            Good point.
            I forgot to say that these threads are activated by a single button; the button watches the sate of a checkbox-group, each checkbox triggering a thread. i guess I could watch the checkbox-group from inside one huge thread, but that would just look too ugly (the code is quite long already).
            • 3. Re: serialized working threads: join or syncronize?
              800268
              You could pass to the thread several worker classes (depending on which checks selected) and let the thread execute those in order:
              class Manager extends Thread {
                private final Client master;
                private final List<Worker> workers = new ArrayList<Worker>();
                private final Object initialWork;
              
                public Manager(Client client, List<Worker> workers, Object initalWork) {
                  this.workers.addAll(workers);
                  this.initialWork = initialWork;
                  this.client= client;
                }
              
                public void run() {
                   Object work = initialWork;
                   for(Worker worker : workers) {
                    work = worker.doWork(work);
                   }
                   
                   final Object completedWork = work;
                    EventQueue.invokeLater(new Runnable() {
                      public void run() {
                        client.workDone(completedWork);
                      }
                    });
                }
                
                public interface Client { void workDone(Object work); }
                public interface Worker { Object doWork(Object initialWork); }
              }
              class Adder implements Worker {
                public Object doWork(Object work) {
                  return ((Integer) work) + 1;
                }
              }
              
              public void actionPerformed(ActionEvent e) {
                 List<Worker> workers = new ArrayList<Worker>();
                if(check1.isSelected) workers.add(new Adder());
                if(check2.isSelected) workers.add(new Adder());
              
                new Manager(new Client() {
                  public void workDone(Object work) {
                    System.out.println(work + " checkboxes where selected");
                  }
                }, workers, 0).start();
              }
              • 4. Re: serialized working threads: join or syncronize?
                807591
                tootles wrote:
                Good point.
                I forgot to say that these threads are activated by a single button; the button watches the sate of a checkbox-group, each checkbox triggering a thread. i guess I could watch the checkbox-group from inside one huge thread, but that would just look too ugly (the code is quite long already).
                It's ugly only if you try to do everything in one method. I'd picture the run method looking like:
                public void run() {
                    if(option1.isSelected())  doOption1();
                    if(option2.isSelected())  doOption2();
                ...
                }