Question on interrupting threads

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 20
Reputation: toucan is an unknown quantity at this point 
Solved Threads: 1
toucan toucan is offline Offline
Newbie Poster

Question on interrupting threads

 
0
  #1
Aug 13th, 2009
I'm trying to run a thread until a mouse event occurs, which will trigger an interrupt to the thread. The problem is that I can't get the thread to stop running in an asynchronous way (i.e. to listen is a mouse event occurred). Instead, what happens is that the thread never exits the while loop. This is what I have so far:

  1. private class aListener extends MouseAdapter {
  2. public void mousePressed(MouseEvent e) {
  3. if(condition){
  4. aThread.run();
  5. }
  6. else{
  7. aThread.interrupt();
  8. }
  9. }
  10. }
  11.  
  12. ...
  13.  
  14. private class workThread extends Thread {
  15. private volatile Thread blinker;
  16.  
  17. public void run() {
  18. blinker = new Thread();
  19. blinker.run();
  20. Thread thisThread = Thread.currentThread();
  21. System.out.print(Thread.currentThread());
  22.  
  23. while (true){
  24. try {
  25. doSomething();
  26. Thread.sleep(1000);
  27. } catch (InterruptedException e) {
  28. blinker = null;
  29. return;
  30. }
  31. }
  32. }
  33. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Question on interrupting threads

 
1
  #2
Aug 13th, 2009
bool gFlag = true;

Event sets flag to false, and causes thread to fall out of loop!
Simple but effective!


// while (true){

while ( gFlag ){

Don't sleep your thread so long.

If you need 1000ms timing then

  1. int nSnooze = 1000;
  2.  
  3. while ( gFlag )
  4. {
  5. Sleep(25);
  6. nSnooze -= 25;
  7. if (nSnooze > 0)
  8. {
  9. continue;
  10. }
  11.  
  12. nSnooze = 1000;
  13. DoSomething();
  14. }
Last edited by wildgoose; Aug 13th, 2009 at 5:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: toucan is an unknown quantity at this point 
Solved Threads: 1
toucan toucan is offline Offline
Newbie Poster

Re: Question on interrupting threads

 
0
  #3
Aug 13th, 2009
I created the boolean flag, but the problem is that it doesn't ever change. I want the WorkThread to run concurrently, but for some reason, the MouseListener doesn't react when the WorkThread is running. It's as if the WorkThread is monopolizing the program's resources, like it's not a separate thread at all, just an infinite loop in the main method.

I didn't mean for the thread to sleep for so long. I figure 10 ms should be enough, but I slowed the thread down for debugging.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Question on interrupting threads

 
0
  #4
Aug 13th, 2009
gFlag is global.

In your event handler for mouse, set gFlag=false;

Thread falls out of while loop upon gFlag being false then exits.

There will be micro-delay between the flag being set and the thread reading the flag and falling out of its loop.

Just looked at your code again. Mouse event spawns the new thread?
Last edited by wildgoose; Aug 13th, 2009 at 6:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: toucan is an unknown quantity at this point 
Solved Threads: 1
toucan toucan is offline Offline
Newbie Poster

Re: Question on interrupting threads

 
0
  #5
Aug 13th, 2009
Yes, the event handler spawns the WorkThread (I did not make this clear: aThread is an instance of WorkThread). When the WorkThread is running, it continuously updates the GUI, but it does not allow me to interact with JButtons, although they have listeners. Even if I try to quit, the program is not responding and I have to terminate it in the Task Manager.

My code now looks like this:

  1.  
  2. class Prog1() {
  3.  
  4. private boolean bFlag;
  5. private WorkThread aThread;
  6.  
  7. public Prog1(args){
  8. ...
  9. addMouseListener(new MouseEventListener());
  10. aThread = new WorkThread();
  11. bFlag = false;
  12. }
  13.  
  14. private class MouseEventListener extends MouseAdapter {
  15. public void mousePressed(MouseEvent e) {
  16. if(condition){
  17. bFlag = true;
  18. aThread.run();
  19. }
  20. else{
  21. // this does not interrupt the thread, since
  22. // I can't detect new mouse events in the WorkThread
  23. bFlag = false;
  24. aThread.interrupt();
  25. }
  26. }
  27. }
  28.  
  29. ...
  30.  
  31. private class workThread extends Thread {
  32. private volatile Thread blinker;
  33.  
  34. public void run() {
  35. while (bFlag){
  36. try {
  37. doSomething();
  38. Thread.sleep(10);
  39. } catch (InterruptedException e) {
  40. return;
  41. }
  42. }
  43. }
  44. }
  45. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Question on interrupting threads

 
0
  #6
Aug 13th, 2009
Where does <condition> come from? If its never cleared then the false logic won't be executed!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: toucan is an unknown quantity at this point 
Solved Threads: 1
toucan toucan is offline Offline
Newbie Poster

Re: Question on interrupting threads

 
0
  #7
Aug 13th, 2009
The condition is a boolean called isSelected. When the user clicks on a image displayed on the GUI, he selects an OrderedPair (another class I wrote) object. The GUI is then supposed to animate the selected object until the user clicks on a location to which to move it. So basically, the condition variable called isSelected is used to alternate behavior: the user selects an OrderedPair (the if(condition) part), then moves the OrderedPair to a new location (the else statement).

I know that there is no faulty logic, since the program ran fine before I added the WorkThread. The WorkThread is only used to animate the selected OrderedPair. I can select and move OrderedPairs, but I have to keep track of which OrderedPair I selected earlier. I know I can display the selected OrderedPair statically by swapping with a different image, but I thought it would look better with an animation and it is about time I stop avoiding writing programs with multiple threads.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,638
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Question on interrupting threads

 
0
  #8
Aug 13th, 2009
I'm not sure that this is the issue, but just so you know, the while loop condition is only going to be checked after the method call that is inside the while loop completes. You probably knew that. Other than that, I'm not sure I understand what problem you're having. What you need to do is have a different thread (probably the main thread) detect the mouse event, then set a condition that lets the other thread know it's time to stop execution. The other thread should periodically check to see if it is time to stop execution.
Out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: toucan is an unknown quantity at this point 
Solved Threads: 1
toucan toucan is offline Offline
Newbie Poster

Re: Question on interrupting threads

 
0
  #9
Aug 13th, 2009
In a nutshell, my problem is:
I can't get out of the while loop in the WorkThread.

To elaborate, the program runs fine without the WorkThread class, but once the WorkThread starts running, I can't interact with the GUI and trigger mouse events that should interrupt the WorkThread, even if it goes through multiple iterations of the while loop.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,638
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Question on interrupting threads

 
0
  #10
Aug 13th, 2009
"I can't interact with the GUI and trigger mouse events that should interrupt the WorkThread, even if it goes through multiple iterations of the while loop."

So you're saying that once the WorkThread starts running, your program will not trigger mouse events? Or that your program triggers mouse events, but these mouse events do not interrupt the WorkThread like they're supposed to?
Out.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC