Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2008, 11:28 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
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:



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()) {
               
                }
        
           }
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-28-2008, 11:54 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-29-2008, 12:02 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-29-2008, 08:33 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
Default
There better ways to wait for a condition to be set that spinning in a loop. ???
Could you give a example, norm
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-29-2008, 12:05 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 66
Rep Power: 0
ProjectKaiser is on a distinguished road
Default
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:
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
Code:
            midipedalflag=false;  // the flag set by midipedal
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-29-2008, 01:42 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
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
Quote:
Multipedal should do:
Code:
midipedalflag.notify();
The thread issuing the notify must have the lock on the monitor(midipedalflag):
Code:
sychronized(midipedaflag) {
  midipedalflag.notify();
}

Last edited by Norm; 08-29-2008 at 01:50 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-29-2008, 02:36 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 66
Rep Power: 0
ProjectKaiser is on a distinguished road
Default
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:

Code:
sychronized(midipedaflag) {
  midipedalflag= true;
  midipedalflag.notify();
}

Last edited by ProjectKaiser; 08-29-2008 at 02:39 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-29-2008, 02:39 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 66
Rep Power: 0
ProjectKaiser is on a distinguished road
Default
Again - the biggest problem in this example is that some events can be missed. It might not be critical though.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-29-2008, 10:38 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
Default
The complete code concerning the midiflag is the following. There are two classes envolved


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.

Code:
public void keyTyped(KeyEvent evt) {
         
         char ch = evt.getKeyChar();  // The character typed.
         
         if (ch == ' ' || (setm.getPedal()))  {
             midipedalflag=true;
             System.out.println(" midipedal trigger "); 
         } 
      }
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-29-2008, 10:46 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 08-29-2008, 10:47 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 66
Rep Power: 0
ProjectKaiser is on a distinguished road
Default
So as you see, thread can miss keyTyped event if it happens between thread start and before "midipedalflag=false" thread statement.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 08-29-2008, 11:18 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
Default
PK your code does not compile

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!
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 08-29-2008, 11:21 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
Default
okay since I am testing the midi pedal only I should strip of the key event!
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 08-29-2008, 11:22 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 08-30-2008, 01:21 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 08-30-2008, 05:19 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 08-30-2008, 05:56 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 08-30-2008, 07:05 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 342
Rep Power: 2
willemjav is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 08-30-2008, 09:14 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Your first posted code does NOT show anything in the loop.
Quote:
do { // and space bar

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

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 09-10-2008, 07:11 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
Rep Power: 2
danielstoner is on a distinguished road
Default
Use your own flag or use Thread.interrupt(). Read more here: Dealing with InterruptedException
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 08:24 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org