• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to let user abort loop  RSS feed

 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want it to be possible to abort a process while it�s running.
The process is started when the user pushes a button.
Then some more methods is called, based on the current environment, before code execution goes into a loop.
What i want to do is to let the user be able to push a button while in the loop, that aborts the loop before it is finished.
I set up a test for one more boolean value in the loop, then set this value to false when the user pushes the abort button.
The problem is that the user can�t push the button before the loop has finished.
I have tried some thread code, but i am not sure how to do this, as I am not very experienced with thread-programming.
I hope that someone will help me out
 
John Rushington
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about you try this:
1. Make an object as an instance variable for your class. (Object checkObject = new Object() );
2. Right before your loop, save the thread that will be executing your loop (which may be the main() thread or another thread) in another instance variable (Thread threadExecutingLoop = Thread.currentThread(); ).
3. put the code
at every place in the loop that you want to possibly stop the code at.
4. when the button is pushed, do a threadExecutingLoop.interrupt() call. The result of this call should be to force the next encountered wait( someTimeoutPeriod ) call to throw an InterruptedException and so return from that method.
I don't think that the loop code has to be in a thread itself, because if you have a User Interface, (as I understand it) then you already have at least two threads: one to handle events from the UI, and the main thread that your program is executing in. It is important that some timeout is specified, because if you have a wait() or a wait(0) call, then the loop will block until the button is pushed, instead of the way I showed, where it blocks for only about 30 milliseconds (could probably make it less, definitely could make it more), during which time an exception can be checked for, and after which it resumes with the code (in your case a loop).
See if this works; I am interested in the result.
[ July 26, 2002: Message edited by: John Rushington ]
 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get it to work when I followed all four of your steps. I think the problem is that when I call wait(long), the button still can't be pushed because it's in the same thread (the loop is initially started by pushing another button).
So what I did was to made the object Runnable (implement Runnable, implement run()). Then I created a new Thread, and gave it the runnable object as parameter to the Thread constructor. Then i called start() on the thread object. In the run method of the Runnable object i placed the loop. Within the loop I placed the wait(long) code (point 3 above). Then in the abort button's actionPerformed method I called interrupt() on the threadobject (point 4 above). Now I am able to abort the loop!
Thanx for your help!
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dont need wait and interrupt. Just make a variable that is checked every loop

now in your button routine you should simple call the function stopMe(). that will cause the loop to end. Is this what you are looking for?
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by CL Gilbert:
You dont need wait and interrupt. Just make a variable that is checked every loop

This will work only if the variable dontstop is declared volatile.
Peter Haggar
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Haggar:

This will work only if the variable dontstop is declared volatile.
Peter Haggar

I think I understand why you would say that, though in my experience I never needed it. Though I should use something.
Personally I use synchronize on my stopper method, so I really havent found a use for volatile. Actually I found that I didn't use synchronize on one of my methods either, its been about 2 years too. It worked, but im putting it in there for good measure anyway...
[ July 31, 2002: Message edited by: CL Gilbert ]
 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementation with setting and checking a boolean variable looks quite similar to what I first tried, but didn't succeed in getting to work.
Our code seems to deviate in one important(?) aspect. While your loop is inside the main method, mine was started by an actionhandler (actionPerformed) for a JButton. Then it seems I can't push another JButton before the handling for the first is finished. Because swing executes within a single-thread. Then, the way I implement it, I can't use an implementation which sets and checks a boolean variable. Have I understood this correctly?
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by CL Gilbert:

I think I understand why you would say that, though in my experience I never needed it. Though I should use something.

To guarantee that it works, you need to declare the variable volatile, or access if from a synchronized method/block. If you do neither, it might work, but it is not guaranteed to work on all VM implementations.
Peter Haggar
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do your work in an action handler if its going to take any amount of time. The swing thread is required to update the graphics and process the rest of the swing GUIs stuff. So if you are working in the swing thread it will not have time to do anything else.
Basically you have another thread.
when you action handler is fired, you set a variable, the wakeup that thread. when you action handler is fired again, you do the same thing, but change the value of the variable. You can not have the while loop within the swing thread. understand? Your first way was correct, but you need to tweek it a bit.
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody,
After so many solutions to that particular problem, I am putting my views please comment on it. I think the easiest way is to make out a seperate thread for the looping process. The thread is started when the user clicks the push button, and is stopped by a flag in the run method, which can be set by the button assigned for stopping the thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!