Results 1 to 10 of 10
  1. #1
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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:
    Java Code:
            Thread mainBot = new Thread();
            mainBot.interrupt();
    thread run():
    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 02:39 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.

  3. #3
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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
    Java Code:
            mainBot dt1=new mainBot(1);
            dt1.allDone = 1;
    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:
            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);
            }
    Running Output: Works Fine
    Run
    0
    0
    0
    After Stopped Output: Returns the same value.... not good
    Run
    0
    0
    0
    I need it to output after stopped...
    Run
    1
    yes

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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"?

    How do i change a public variable in a thread that is already running?
    What variable are you trying to change?

    You need to post a complete program that executes and shows your problem. These short bits are pretty much useless.

  5. #5
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Interrupt Thread

    xD ok sorry.

    Here is my mainBot.java thread:
    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;
        }
    }
    Start Button:
    Java Code:
            System.out.println(0);
            mainBot dt1=new mainBot("Run");
            System.out.println(3);    
            dt1.start();
    My stop button is still the same code:
    Java Code:
            mainBot dt1=new mainBot(1);
            dt1.allDone = 1;

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Interrupt Thread

    The code you posted does not execute.
    You need to post a complete program that executes and shows your problem

  7. #7
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Interrupt Thread

    Would you like my NetBeans Project?
    NetBeans Project http://www.mediafire.com/?weece91oexd949j
    Last edited by TyCox94; 09-28-2011 at 04:27 AM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Interrupt Thread

    You need to add lots more printlns to see what the code is doing. In every method and every constructor.

  9. #9
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Interrupt Thread

    Do public variables in a active thread become static once first loaded?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

Similar Threads

  1. interrupt to thread
    By naik_amit in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-26-2010, 08:04 AM
  2. interrupt sleeping thread by mouse action
    By BigBear in forum AWT / Swing
    Replies: 5
    Last Post: 08-12-2010, 11:10 PM
  3. interrupt a jdbc connection thread
    By ko_aung in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-12-2010, 09:04 PM
  4. Replies: 2
    Last Post: 03-19-2010, 09:24 AM
  5. Anyone know how to interrupt a process forcibly?
    By 2potatocakes in forum Advanced Java
    Replies: 3
    Last Post: 04-05-2009, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •