1 2 Previous Next 29 Replies Latest reply: Mar 28, 2008 4:50 AM by 807591 Go to original post RSS
      • 15. Re: How to find that a thread is alive or not
        796447
        SunFred wrote:
        ItsJava wrote:
        if(processThread.isAlive())
        return true;
        else
        return false;
        So if something is true, you return true, and if it is false, you return false... why not just return the something?
        Because he likes to scratch his belly button by first stretching his arm around his back to get to it.

        Plus, chalk up one more proof about people who put the letters "java" or "Java" in their screen names, having a certain relationship to their skillset.
        • 16. Re: How to find that a thread is alive or not
          807591
          ItsJava wrote:
          Hi all,

          I've create a thread to make a http request in every 10 second and get a string as the response from the server. Requesting process start on the window open. And also I can dispose the window, so my processing is not stop. That is exactly what I want to do.

          Say after disposing the window I open it again. So the same thread start again while the first one is running. It messup my work. So I think of it. Every window open try to check that thread is alive or not. If alive stop the thread and start is again.

          So I try this simply.
          public boolean ThreadIsAlive(){
          if(processThread.isAlive())
          return true;
          else
          return false;
          }
          If I found the thread true on the above code, I want to stop it. So I try this.
          public void stop(){
          processThread = null;
          }
          I think up to now what I have done is ok. Then on the ActionListner I do this.
          ActionListener actionListener = new ActionListener(){
          public void actionPerformed(ActionEvent e){
          if(appProcess.IsThreadAlive()){
          appProcess.stop();
          System.out.println("Main Dialog!");
          java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
          MainDialog dialog = new MainDialog(new javax.swing.JFrame(), true);
          dialog.setVisible(true);
          }
          });
          }
          }
          };
          No compile error. But I got a runtime exception.
          Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
          at mytrayicon.Processing.IsThreadAlive(Processing.java:82)
          at mytrayicon.PanelRadarMain$3.actionPerformed(PanelRadarMain.java:73)
          at java.awt.TrayIcon.processActionEvent(TrayIcon.java:750)
          at java.awt.TrayIcon.processEvent(TrayIcon.java:708)
          at java.awt.TrayIcon.dispatchEvent(TrayIcon.java:690)
          at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
          at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
          at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
          at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
          at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
          Can anybody help me to solve this.

          Thanks
          First of all, maybe this could help instead?
              public boolean ThreadIsAlive(){
                  if(processThread.isAlive())
                      return "true";
                  else
                      return "false";
              }
          Next, the variable processThread. Have you declared it in a visible scope? For example, to make it visibl, you can put is just after the class declaration brace. For instance,

          // SNIP--------------------
          public class ThreadKiller
          {
              private Thread processThread;
          // SNIP--------------------
          • 17. Re: How to find that a thread is alive or not
            807591
            masijade. wrote:
            ItsJava wrote:
            Reason is that I start the thread in formWindowOpened() method. Every time I open the window my thread is started.
            Then change that, and open it in the Main class, or something, before you ever open your first window.
            Yes, I hope it is better to do in my default constructor. I'll try it and let you know.
            • 18. Re: How to find that a thread is alive or not
              807591
              >
              First of all, maybe this could help instead?
              public boolean ThreadIsAlive(){
              if(processThread.isAlive())
              return "true";
              else
              return "false";
              }
              Next, the variable processThread. Have you declared it in a visible scope? For example, to make it visibl, you can put is just after the class declaration brace. For instance,

              // SNIP--------------------
              public class ThreadKiller
              {
              private Thread processThread;
              // SNIP--------------------
              Actually that what I have done in my application. Even though gives an error on ThreadIsAlive() method at runtime. Now I'll try to used a dummy bool variable to hold the thread state.
              • 19. Re: How to find that a thread is alive or not
                EJP
                Actually that what I have done in my application. Even though gives an error on ThreadIsAlive() method at runtime. Now I'll try to used a dummy bool variable to hold the thread state.
                It would be more to the point to find the bug and fix it rather than just setting about introducing another one.
                • 20. Re: How to find that a thread is alive or not
                  807591
                  ejp wrote:
                  Actually that what I have done in my application. Even though gives an error on ThreadIsAlive() method at runtime. Now I'll try to used a dummy bool variable to hold the thread state.
                  It would be more to the point to find the bug and fix it rather than just setting about introducing another one.
                  No sir, I already use such a variable. What I have done is, regularly check the bool value and depend on that execute the thread.
                  • 21. Re: How to find that a thread is alive or not
                    807591
                    ItsJava wrote:
                    >
                    First of all, maybe this could help instead?
                    public boolean ThreadIsAlive(){
                    if(processThread.isAlive())
                    return "true";
                    else
                    return "false";
                    }
                    Next, the variable processThread. Have you declared it in a visible scope? For example, to make it visibl, you can put is just after the class declaration brace. For instance,

                    // SNIP--------------------
                    public class ThreadKiller
                    {
                    private Thread processThread;
                    // SNIP--------------------
                    Actually that what I have done in my application. Even though gives an error on ThreadIsAlive() method at runtime. Now I'll try to used a dummy bool variable to hold the thread state.
                    Also, have you initialized the variable?

                    Something like, say,
                    // SNIP--------------------
                    public class ThreadKiller
                    {
                        private Thread processThread;
                    // SNIP--------------------
                        public ThreadKiller()
                        {
                            processThread = new Thread();
                    // SNIP--------------------
                    • 22. Re: How to find that a thread is alive or not
                      masijade
                      Jamwa wrote:
                      First of all, maybe this could help instead?
                      public boolean ThreadIsAlive(){
                      if(processThread.isAlive())
                      return "true";
                      else
                      return "false";
                      }
                      See reply #14.
                      As was said there, why not just
                          public boolean ThreadIsAlive(){
                              return processThread.isAlive();
                          }
                      And, why would you want to return Strings, when the method is declared boolean.
                      If the calling process wants a String they need only do
                      String.valueOf(whatever.ThreadIsAlive())
                      • 23. Re: How to find that a thread is alive or not
                        807591
                        Ok, now I can find the thread is alive is not.

                        After start the thread I check it as follows.

                        Here is the MainDialog class where I start the thread. On the next open of the dialog I want to check that thread is running, if so I want to stop it and start again.
                        public class MainDialog extends javax.swing.JDialog {
                         private void formWindowOpened(java.awt.event.WindowEvent evt) {
                                appProcess.init();        
                                if(appProcess.IsThreadAlive()){
                                    System.err.println("Running");
                                    appProcess.ThreadStop();
                                    appProcess.init();
                                }
                                else{
                                    System.err.println("Stoped");
                                }
                        
                        }
                        }
                        Here is my thread processing class.
                        public class Processing{
                        
                            boolean threadSuspend = false;
                            volatile Thread processThread;
                            int counter;
                        
                            public void init(){
                                counter = 0;
                                processThread = new Thread(this);
                                processThread.start();
                            }
                            
                            public void run(){
                                while(true){
                                    try {
                                        System.out.println(counter);
                                        Thread.sleep(5000);
                                        counter++;
                                        synchronized(this){
                                            while(threadSuspend){
                                                try{
                                                    wait();
                                                }
                                                catch(Exception ex){
                                                    Logger.getLogger(Processing.class.getName()).log(Level.SEVERE, null, ex);
                                                }
                                            }
                                        }
                                   }
                                    catch (InterruptedException ex) {
                                        Logger.getLogger(Processing.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                }
                            }
                        
                            public boolean IsThreadAlive(){
                                return processThread.isAlive();
                            }
                            
                            public void ThreadStop(){
                                processThread = null;
                            }
                        
                        }
                        Still what happened is the initial thread is not stopped. Run two threads if I have open the dialog two time.

                        Can you tell me why is that. I'm fully wired with this now.
                        • 24. Re: How to find that a thread is alive or not
                          EJP
                          public void ThreadStop(){
                          processThread = null;
                          }
                          That doesn't stop the thread.
                          Still what happened is the initial thread is not stopped.
                          That's because you don't have any code that stops it.
                          Can you tell me why is that.
                          You don't have any code that stops it.

                          You need to implement what I said in this thread on 26/03/2008 20:02 (reply 3 of 23). I don't see any sign of it here, or of the other suggestion either.
                          • 25. Re: How to find that a thread is alive or not
                            807591
                            You mean rather making process thread equals to null, to interrupt.

                            Something like this.
                                    appProcess.init();        
                                    if(appProcess.IsThreadAlive()){
                                        System.err.println("Running");
                                        appProcess.processThread.interrupt();
                                        appProcess.init();
                                    }
                                    else{
                                        System.err.println("Stoped");
                                    }
                            • 26. Re: How to find that a thread is alive or not
                              EJP
                              You were told that setting it to null doesn't stop it on 26/03/2008 20:00 (reply 2 of 25).

                              You were told about Thread.interrupt() on 26/03/2008 20:02 (reply 3 of 25).

                              You had written some code that used Thread.interrupt() on 26/03/2008 20:33 (reply 8 of 25).

                              Why setting it to null is still in your code two days later I have no idea: nr why this is still such a mystery to you.
                              • 27. Re: How to find that a thread is alive or not
                                807591
                                ejp wrote:
                                You were told that setting it to null doesn't stop it on 26/03/2008 20:00 (reply 2 of 25).
                                On my original post I told that, setting thread to null doesn't stop my thread. It's not work. After making null and try to call the the inti() again. It give the NulPointerException.
                                You were told about Thread.interrupt() on 26/03/2008 20:02 (reply 3 of 25).

                                You had written some code that used Thread.interrupt() on 26/03/2008 20:33 (reply 8 of 25).
                                Even I use the interrupt it's not detect that Thread is alive or not. May be I've check it in the wrong place.
                                I don't know why setting it to null is still in your code two days later, or why this is still such a mystery to you.
                                Actually make to null is not there. I just use it for test that working fine or not.
                                • 28. Re: How to find that a thread is alive or not
                                  EJP
                                  On my original post I told that, setting thread to null doesn't stop my thread. It's not work. After making null and try to call the the inti() again. It give the NulPointerException.
                                  In other words there is no point in setting it to null whatsoever; there never was; and it caused another problem. So why are you still doing it?
                                  Even I use the interrupt it's not detect that Thread is alive or not. May be I've check it in the wrong place.
                                  Maybe you've coded it wrong. Thread.interrupt() interrupts the thread. It doesn't change its isAlive() status unless the thread checks isInterrupted() and does something to cause itself to exit. You've been told to use that API several times. I gave you the code, contrary to my usual practice, on 26/03/2008 20:11 (reply 5 of 28). Two days ago.
                                  Actually make to null is not there.
                                  Good. So why are you still asking about setting it to null? We disposed of that question two days ago.

                                  This is all quite ridiculous. It is also somewhat depressing, not to mention irritating, that I gave you a correct answer two days ago and you don't show too many signs of having tried it. I don't know why I bothered and I also don't know why you asked.
                                  • 29. Re: How to find that a thread is alive or not
                                    807591
                                    I've done just about every possible permutation of thread programming over the years and I've never had occasion use isAlive(). When you've got one of these background threads it's alive once you've started it, and once you tell it to quit it generally doesn't matter how long it takes to die. If it does, call interrupt() and then join() to wait for it to wind up. You should never need to ask if it's alive. You should know because you start it and you stop it.

                                    If the thread need to change stuff on windows that may open or close then have these windows add and remove themselves from a list as they open and close. The background thread, when it has changes to make, should use EventQueue.invokeLater to shedule a task which runs through the list, updating the GUI (which guarantees that the list isn't updated while it's being scanned).
                                    1 2 Previous Next