Results 1 to 10 of 10
Thread: Interrupt Thread
- 09-28-2011, 01:28 AM #1
Interrupting a Thread
I'm trying to setup a interruption, but it seems to be ignoring it. Take a look at my code
button code:
thread run():Java Code:Thread mainBot = new Thread(); mainBot.interrupt();
Java Code:@Override public void run() { Thread mainBot = new Thread(); while (activeRun == 1) { if (mainBot.isInterrupted()) { activeRun = 0; } else { mainRun(); } } }Last edited by TyCox94; 09-28-2011 at 01:39 AM.
- 09-28-2011, 02:48 AM #2
Re: Interrupt Thread
What is the code you posted supposed to do when it is executed?
What does it do when it is executed?
Add some println statements to show where the execution goes.
- 09-28-2011, 03:03 AM #3
Re: Interrupt Thread
Ok so I've been working on it. I now trying a different approach. How do i change a public variable in a thread that is already running? Take a look at the code...
My stop button
I need it to change a public variable (allDone) in mainBot Thread to 1. This will notify my while loop code to stop and return;Java Code:mainBot dt1=new mainBot(1); dt1.allDone = 1;
Running Output: Works FineJava Code:while (active == 1) { mainRun(); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(mainBot.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(allDone); if(allDone == 1) { System.out.println("yes"); return; } else { System.out.println(allDone); } System.out.println(allDone); }
After Stopped Output: Returns the same value.... not goodRun
0
0
0
I need it to output after stopped...Run
0
0
0
Run
1
yes
- 09-28-2011, 03:09 AM #4
Re: Interrupt Thread
What do the printed numbers mean? You need to label your output so you know exactly which println printed it:
System.out.println("first aD=" + allDone);
...
System.out.println("last aD=" + allDone);
Where does it print "Run"?
What variable are you trying to change?How do i change a public variable in a thread that is already running?
You need to post a complete program that executes and shows your problem. These short bits are pretty much useless.
- 09-28-2011, 03:14 AM #5
Re: Interrupt Thread
xD ok sorry.
Here is my mainBot.java thread:
Start Button:Java Code:import java.util.logging.Level; import java.util.logging.Logger; public class mainBot extends Thread { public int allDone; public int active = 1; String msg; @Override public void run() { while (active == 1) { mainRun(); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(mainBot.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(allDone); if(allDone == 1) { System.out.println("yes"); return; } else { System.out.println(allDone); } System.out.println(allDone); } } public void mainRun() { System.out.println(msg); } mainBot(String mg) { msg=mg; } mainBot(int i) { allDone = i; } }
My stop button is still the same code:Java Code:System.out.println(0); mainBot dt1=new mainBot("Run"); System.out.println(3); dt1.start();
Java Code:mainBot dt1=new mainBot(1); dt1.allDone = 1;
- 09-28-2011, 03:17 AM #6
Re: Interrupt Thread
The code you posted does not execute.
You need to post a complete program that executes and shows your problem
- 09-28-2011, 03:21 AM #7
Re: Interrupt Thread
Would you like my NetBeans Project?
NetBeans Project http://www.mediafire.com/?weece91oexd949jLast edited by TyCox94; 09-28-2011 at 03:27 AM.
- 09-28-2011, 03:34 AM #8
Re: Interrupt Thread
You need to add lots more printlns to see what the code is doing. In every method and every constructor.
- 09-28-2011, 04:08 AM #9
Re: Interrupt Thread
Do public variables in a active thread become static once first loaded?
- 09-28-2011, 04:13 AM #10
Similar Threads
-
interrupt to thread
By naik_amit in forum Threads and SynchronizationReplies: 2Last Post: 10-26-2010, 07:04 AM -
interrupt sleeping thread by mouse action
By BigBear in forum AWT / SwingReplies: 5Last Post: 08-12-2010, 10:10 PM -
interrupt a jdbc connection thread
By ko_aung in forum Threads and SynchronizationReplies: 5Last Post: 04-12-2010, 08:04 PM -
How to interrupt threads blocked in a RMI operation
By sky in forum Advanced JavaReplies: 2Last Post: 03-19-2010, 08:24 AM -
Anyone know how to interrupt a process forcibly?
By 2potatocakes in forum Advanced JavaReplies: 3Last Post: 04-05-2009, 06:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks