Results 1 to 20 of 20
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default how to stop a thread

    Threads are tricky! You start them easily.. but how do you stop them. Consider this example (it works more or the less) but each time I test the pedal a new thread is started.... so what are all these started threats doing:



    Java Code:
    class PedaltestThread extends Thread {
            private int cnt;  // The name of this thread.
            public PedaltestThread(int cnt) {  // the thread counter
            this.cnt = cnt;
            }
                public void run() {  
                    midipedalflag=false;  // the flag set by midipedal
                    do  {                 // and space bar
                           
                    } while (!midipedalflag);
                    midipedalflag=false;
                    midipedalTest.setSelected(false);
                    System.out.println(" pedal pushed " + cnt);
                }
     }
    
    
     private class EventHandler implements  ActionListener {
            
             public void actionPerformed(ActionEvent evt) {
               Object source = evt.getSource();
     
               if (source ==  midipedalTest)  {
                   
                    if (midipedalTest.isSelected()) {
                       
                       threadcount++; // thread counter
                       pedtest  = new PedaltestThread(threadcount);
                       pedtest.start();
                       
                        
                    }
                    if (!midipedalTest.isSelected()) {
                   
                    }
            
               }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    so what are all these started threats doing:
    Add some println() statements with appropriate contents to show what the threads are doing.
    You have one that shows when the loop has ended.

    The do {} while loops will continually test for the exit condition until it allows the flow to exit the loop. There better ways to wait for a condition to be set that spinning in a loop.

    When a thread ends the run() method is exited.
    Last edited by Norm; 08-29-2008 at 12:01 AM.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Okay so when exiting run() it is over.. I did not know that, thanks Norm. Probably the code works correctly (because the thread only waits for the midipedalflag to become true, set by the midi pedal). There are no threads hanging around.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    There better ways to wait for a condition to be set that spinning in a loop. ???
    Could you give a example, norm

  5. #5
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Java Code:
            Boolean midipedalflag;
    
                midipedalflag=false;  // the flag set by midipedal
                do {                 // and space bar
                    synchronized(midipedalflag){
                        midipedalflag.wait();                
                    }
                } while (!midipedalflag);
                midipedalflag=false;
    Multipedal should do:
    Java Code:
    midipedalflag.notify();
    Note:
    1) To be notifiable midipedalflag is Boolean, not boolean
    2) Seems you can miss some events. E.g. midipedal sets midipedalflag = true before you set it to false by line
    Java Code:
                midipedalflag=false;  // the flag set by midipedal

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    midipedalflag=false; // the flag set by midipedal
    do { // and space bar

    } while (!midipedalflag);
    midipedalflag=false;
    midipedalTest.setSelected(false);
    The only thing this code seems to be doing is to setSelected(false) when the midipedalflag is set to true.
    Why not get rid of this code and have the method that sets midipedalflag true do the setSelected(false) itself?


    BTW
    Multipedal should do:
    Code:
    midipedalflag.notify();
    The thread issuing the notify must have the lock on the monitor(midipedalflag):
    Java Code:
    sychronized(midipedaflag) {
      midipedalflag.notify();
    }
    Last edited by Norm; 08-29-2008 at 01:50 PM.

  7. #7
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The thread issuing the notify must have the lock on the monitor(midipedalflag)
    Correct, I missed that, should be something like this:

    Java Code:
    sychronized(midipedaflag) {
      midipedalflag= true;
      midipedalflag.notify();
    }
    Last edited by ProjectKaiser; 08-29-2008 at 02:39 PM.

  8. #8
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Again - the biggest problem in this example is that some events can be missed. It might not be critical though.

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    The complete code concerning the midiflag is the following. There are two classes envolved


    Java Code:
    public void midiInput(byte[] data){
    		
    		System.out.println("Input from: "+myInput.getName());
                            System.out.println("Array length " + data.length);
                            System.out.print("midi data  ");
    			for (int i = 0; i < data.length; i++) {
                            System.out.print("  >  " + (data[i]) + " / " + (data[i]));
    				//System.out.print(hexChars[(data[i] & 0xFF) / 16] );
    				//System.out.print(hexChars[(data[i] & 0xFF) % 16]+" willem ");
    				//if (data.length > 5 && (i+1) % 16 == 0) System.out.println("");
    			}
                            if ((data[0]==-112) && (data[1]==60) && (data[2]==0))   { // for the moment works on C, have no midi ped
                               midiped = true;              // midi pedal cc64 127, >64 or 0 pedal up
                               System.out.println("");
                               System.out.println("pedal up");
                            }  
                            else midiped = false;
    			System.out.println("");
                           
    		
                    }
    
    
    
    
    public boolean getPedal()   { / method/ of class midiforFbo1
               if(midiped)  return true;
               else return false;
           }
    This code sits in the class midiforFbo1 (its constructor sets either one of the midi drivers by the mmjflag).
    In the second class the object setm gets acces to the method of the midi flag.

    setm = new midiforFbo1(mmjflag);

    The following method sets the flag on the pedal or space key.

    Java Code:
    public void keyTyped(KeyEvent evt) {
             
             char ch = evt.getKeyChar();  // The character typed.
             
             if (ch == ' ' || (setm.getPedal()))  {
                 midipedalflag=true;
                 System.out.println(" midipedal trigger "); 
             } 
          }

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Java Code:
    public void setpedFlag(boolean pflg) {
              midipedalflag = pflg;
          }
          
          public boolean getpedFlag() {
              return midipedalflag; 
          }
    These two methods reside in the second class to be used again in a third class. Maybe it is all to complicated. Anyway the code subject to discussion should just test the working of the pedal before its use in the third class.

  11. #11
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    So as you see, thread can miss keyTyped event if it happens between thread start and before "midipedalflag=false" thread statement.

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    PK your code does not compile

    Java Code:
    Boolean midipedalflag;
    
                midipedalflag=false;  // the flag set by midipedal
                do {                 // and space bar
                    synchronized(midipedalflag){
                        midipedalflag.wait();                
                    }
                } while (!midipedalflag);
                midipedalflag=false;
    After all the loop of mine is not a very elegant way of doing things, I admit, but at least I ensure that, while spinning in that loop, there is not happening to much more. When the flag is set (or the box unchecked) the thread terminates and the info screen displays ¨pedal pushed¨ .... not much more should happen for the moment. Because that is all, waiting and notifying, is not really needed!? I tested the thing and saw no problem, Sure when more things are involved one should look for other ways!

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    okay since I am testing the midi pedal only I should strip of the key event!

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    The space key was used to get out of the loop incase midi wont work. But its better to uncheck the box called midipadal test to get out!

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    I don't know how anyone can help you when post so many small pieces of code. There is NO way to see how they are connected.
    Here is a do{} while loop, there there are a couple of methods
    and again some more methods.

    Posting a large program won't be any good either.

    Can you squeeze your problem down into a small, simple program that demonstrates your problem that you can post?

    It looks like you are trying to coordinate the program execution with user actions: Key events are used to set flags that are tested for in loops --- for what purpose?
    Last edited by Norm; 08-30-2008 at 01:25 AM.

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    It is a little frustrating I know, at first I try to reduce the problem to one or two simple methods, but than I find out that, that is not a appropriate presentation of the issue so I start to add more and more things (maybe my codes are to complicated and I should write them simpler).
    But Norm you actually did help me a lot by saying that when one exit a run() the thread is over. This information was vital for me. PK also helped me by pointing out that threads are tricky in sense of synchronization. I am aware of that problem now but believe that in that single case, synchronization is not a problem so I will leave it as it is.

    The good news is that I started studying kathy sierra`s book because I beleive I already know a loto of java on the one hand but I do have a some gramatical problems as well, and I beleive that that book will help me eventually.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    midipedalTest.setSelected(false);
    In the early part of the code you posted, the ONLY action taken when execution exited the loop was the above. Why bother with a loop if the code that sets the flag that allows the loop to exit could have executed the above statement. The flag and the loop are not needed.

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    So after the user selects the midi driver I wont here or him to test if midi is working. I test midi out by playing a single tone. Midi in only´s task is to read a midi pedal out. So the test is done by selecting a JCheckbox called midipedalTest. When the box is checked the test begins. When the pedal sets the flag true the test informs positive and the box gets unchecked. In case the pedal does not work (there is some problem with the driver, cables and or midi device) the test should be ended (threat should be stopped) by unchecking the box (I latter added the small code: infoPane.setText(" pedal pushed "); to indicate some more than just the checkbox). That is all there is to it? So I might not need a loop.... but how I get out of the run() method? The loop forces the run to run until 1) the pedal is pushed and works; 2) the box gets unchecked! When either one of these things is not done the threads continuos and no other thread can start. After getting out the loop the test could be done again by starting the thread new and the loop repeats....... the rest of the story has been told. Norm its not elegant but it works or not?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Your first posted code does NOT show anything in the loop.
    do { // and space bar

    } while (!midipedalflag);
    Did you remove code that was in that loop?
    Otherwise why have an empy loop?

    but how I get out of the run() method?
    If there is no code in the run() method, it will execute and exit very quickly.

  20. #20
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Use your own flag or use Thread.interrupt(). Read more here: Dealing with InterruptedException
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. How to stop entering Data
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 02:58 AM
  2. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM
  3. Applets (init, start, stop, destroy)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 10:57 AM
  4. how to stop refreshing page
    By cecily in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 01:25 AM
  5. stop button in the browser
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •