943,584 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1088
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 13th, 2009
0

Question on interrupting threads

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
toucan is offline Offline
20 posts
since Nov 2007
Aug 13th, 2009
1

Re: Question on interrupting threads

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

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 13th, 2009
0

Re: Question on interrupting threads

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
toucan is offline Offline
20 posts
since Nov 2007
Aug 13th, 2009
0

Re: Question on interrupting threads

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 13th, 2009
0

Re: Question on interrupting threads

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:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
toucan is offline Offline
20 posts
since Nov 2007
Aug 13th, 2009
0

Re: Question on interrupting threads

Where does <condition> come from? If its never cleared then the false logic won't be executed!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 13th, 2009
0

Re: Question on interrupting threads

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
toucan is offline Offline
20 posts
since Nov 2007
Aug 13th, 2009
0

Re: Question on interrupting threads

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Aug 13th, 2009
0

Re: Question on interrupting threads

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
toucan is offline Offline
20 posts
since Nov 2007
Aug 13th, 2009
0

Re: Question on interrupting threads

"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?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Is there any way around "cannot be resolved" warnings/errors
Next Thread in Java Forum Timeline: Java Progr Class, Need help quickly, PLEASE!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC