4 Replies Latest reply: Nov 9, 2006 5:06 AM by 807607 RSS

    How to join threads in a static context?

    807607
      Hi,

      I have come across one situation where i have to join two threads in a staitc context.Is it possible?if yes..how?
      for eg two threads r running..A,B
      i want A to continue after only B has over.

      so i called B.join()...but it is giving error.."non staitc method can not be refered from staic context"

      how to handle the situation?

      Thnkingu
      vej.
        • 1. Re: How to join threads in a static context?
          800322
          so i called B.join()...but it is giving error.."non
          staitc method can not be refered from staic
          context"

          how to handle the situation?
          I think in the very beginning you should find out what "static" means, because it has nothing at all to do with threads. You seem to be calling an instance method on B, but B is a class reference. No instance, no instance methods. It's as simple as that.
          • 2. Re: How to join threads in a static context?
            807607
            Hi..thanku for responding..this is the situation i come across...plz once have a look...

            i used : Thread.join()..and it is giving error i.e.nonstatic method sholud not be used from static context.
               public class IntMessDialog implements Runnable{
                    
                    public void run(){    
                           
                                JOptionPane.showInternalMessageDialog((                        VisitorImportPage.this.
                                    getApplication().getDesktop().getPanel().getParent()),
                                    NLSTranslator.translate(
                                    "messages.ImportBegin"),
                                    NLSTranslator.translate(
                                    "messages.ImportBeginTitle"),
                                    JOptionPane.INFORMATION_MESSAGE);
                               
                                
                           }      
                      }
            public class ImportWorker implements Runnable {
                
            public void run() {                    
                                 ......................................
            
                                      ...........................
            
                 
                             if (intMessDialog == null) {
            
                               intMessDialog  = new IntMessDialog();
                            }                
                              new Thread(intMessDialog).start();        
            
                               
                                   Thread.join();                     S.O.P("this statement sholud be printed only after pressing OK..in the internal dialog..so iwant to join those two threads");  
                            }
                        
            
                    }
            • 3. Re: How to join threads in a static context?
              800322
              Thread.join()..
              So which part of "don't call instance methods on classes" didn't you understand? :)

              You need an actual instance of a thread that you can join.
              • 4. Re: How to join threads in a static context?
                807607
                If you're going to use something like join on a Thread you start you need to keep a reference to the Thread object in order to call join on it.

                Incidentally there's rarely any point in starting a thread then immediately calling join on it. Might as well run the code on the original thread.