| | |
Question on interrupting threads
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
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)
private class aListener extends MouseAdapter { public void mousePressed(MouseEvent e) { if(condition){ aThread.run(); } else{ aThread.interrupt(); } } } ... private class workThread extends Thread { private volatile Thread blinker; public void run() { blinker = new Thread(); blinker.run(); Thread thisThread = Thread.currentThread(); System.out.print(Thread.currentThread()); while (true){ try { doSomething(); Thread.sleep(1000); } catch (InterruptedException e) { blinker = null; return; } } } }
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
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)
int nSnooze = 1000; while ( gFlag ) { Sleep(25); nSnooze -= 25; if (nSnooze > 0) { continue; } nSnooze = 1000; DoSomething(); }
Last edited by wildgoose; Aug 13th, 2009 at 5:56 pm.
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
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.
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.
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?
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.
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
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:
My code now looks like this:
Java Syntax (Toggle Plain Text)
class Prog1() { private boolean bFlag; private WorkThread aThread; public Prog1(args){ ... addMouseListener(new MouseEventListener()); aThread = new WorkThread(); bFlag = false; } private class MouseEventListener extends MouseAdapter { public void mousePressed(MouseEvent e) { if(condition){ bFlag = true; aThread.run(); } else{ // this does not interrupt the thread, since // I can't detect new mouse events in the WorkThread bFlag = false; aThread.interrupt(); } } } ... private class workThread extends Thread { private volatile Thread blinker; public void run() { while (bFlag){ try { doSomething(); Thread.sleep(10); } catch (InterruptedException e) { return; } } } } }
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Sep 2008
Posts: 1,638
Reputation:
Solved Threads: 206
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.
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Sep 2008
Posts: 1,638
Reputation:
Solved Threads: 206
"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?
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.
![]() |
Similar Threads
- Confused with threads-help with a homework (Java)
- Hotmail Login Problem! (Web Browsers)
- Threads in user space / kernel space (*nix Software)
- vBulletin mod_rewrite (PHP)
- Tell us about yourself! (Community Introductions)
- Adblaster2!! Wont go away! (Viruses, Spyware and other Nasties)
- message when i start up (Windows NT / 2000 / XP)
- Launching program from resources (C)
Other Threads in the Java Forum
- Previous Thread: Is there any way around "cannot be resolved" warnings/errors
- Next Thread: Java Progr Class, Need help quickly, PLEASE!!!
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint 2dgraphics android api apple applet application applications arguments array arrays automation bank binary bluetooth chat class classes client code collision component database db development draw eclipse eclipsedevelopment error event exception file fractal game givemetehcodez graphics gui helpwithhomework homework html ide image input integer integration j2me jarfile java javadesktopapplications javafx javaprojects jmf jni jpanel julia learningresources linux list loop map method methods mobile netbeans newbie number object oracle print problem program programming project projectideas recursion researchinmotion scanner screen server service set size sms socket sort sorting sql sqlserver state string swing swt tcp test text-file threads time tree web windows






