Results 1 to 8 of 8
  1. #1
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default I need to manually terminate my threads.

    Hi guys. I have a problem with my multithreaded application. I have this method in a thread that interrupts all the current threads running. It does this by storing an ArrayList of these threads.

    Here's the code:
    Java Code:
    public synchronized void foundNumber() {
            for(int i = 0; i < threads.size(); i++) {
                threads.get(i).interrupt();
                System.out.println(threads.get(i).getName() + " has been interupted.");
                found = true;
            }
    If it's any help here is the whole class:
    Java Code:
    import java.util.ArrayList;
    
    public class Monitor extends Thread {
        private ArrayList<Thread> threads;
        boolean found;
        
        Monitor() {
            threads = new ArrayList<>();
            found = false;        
        }
        
        @Override
        public void run(){
            while(true) {
                if(found == true) {    
                    
                }
            }
        }
        
        public void addThread(Thread thread) {
            threads.add(thread);
        }
        
        public synchronized void foundNumber() {
            for(int i = 0; i < threads.size(); i++) {
                threads.get(i).interrupt();
                System.out.println(threads.get(i).getName() + " has been interupted.");
                found = true;
            }
        }
    }
    What my application does is generate a random number, and the threads have to find them by using a for loop. Once it finds them, it prints a message saying it has done so. It is then suppose to call foundNumber() to interrupt the rest of the threads. When my threads are interrupted I want to be terminated, but it doesn't seem to be working. I'm new to threads, so I'd appreciate some help.

    Here's the code for the other class.
    Java Code:
    public class FinderThread implements Runnable {
        private int target;
        private int start;
        private int end;
        private Monitor monitor;
        
        FinderThread(int target, int start, int end, Monitor monitor){
            this.target = target;
            this.start = start;
            this.end = end;
            this.monitor = monitor;
        }
        
        @Override
        public synchronized void run(){
            while(Thread.interrupted() == false) {
                int count = 0;
                for(int i = start; i <= end; i++) {
                    if(i == target) {
                        System.out.println("The target number " 
                                           + target
                                           + " has been found by " 
                                           + Thread.currentThread().getName()
                                           + ".");
                        monitor.foundNumber();
                    }
                    count++;
                    if(count % 10 == 0)
                        try{
                            this.wait(1);
                        }
                        catch(InterruptedException e) {
                            System.err.println(e);
                        }
                }
            }
        }
        
    }
    Here's the weird stack trace:
    Java Code:
    The number is 664
    The target number 52 has been found by Thread-2.
    The target number 52 has been found by Thread-1.
    java.lang.InterruptedException
    The target number 52 has been found by Thread-3.
    The target number 52 has been found by Thread-4.
    Thread-1 has been interupted.
    java.lang.InterruptedException
    Thread-2 has been interupted.
    Thread-3 has been interupted.
    Thread-4 has been interupted.
    Thread-1 has been interupted.
    java.lang.InterruptedException
    Thread-2 has been interupted.
    Thread-3 has been interupted.
    Thread-4 has been interupted.
    Thread-1 has been interupted.
    java.lang.InterruptedException
    Thread-2 has been interupted.
    Thread-3 has been interupted.
    Thread-4 has been interupted.
    Thread-1 has been interupted.
    Thread-2 has been interupted.
    java.lang.InterruptedException
    Thread-3 has been interupted.
    Thread-4 has been interupted.
    java.lang.InterruptedException
    java.lang.InterruptedException
    java.lang.InterruptedException
    This keeps going on forever.

    Here's the main class too if you're interested.
    Java Code:
    import java.util.ArrayList;
    
    public class Main
    {
        public static void main(String args[])
        {        
            Monitor monitor = new Monitor();
            
            Thread finder1 = new Thread(new FinderThread(52, 0, 999, monitor));
            Thread finder2 = new Thread(new FinderThread(52, 0, 999, monitor));
            Thread finder3 = new Thread(new FinderThread(52, 0, 999, monitor));
            Thread finder4 = new Thread(new FinderThread(52, 0, 999, monitor));
            
            monitor.addThread(finder1);
            monitor.addThread(finder2);
            monitor.addThread(finder3);
            monitor.addThread(finder4);
                  
            int target = (int) (Math.random() * 1000);
            System.out.println("The number is " + target);
            finder1.start();
            finder2.start();
            finder3.start();
            finder4.start();
        }
    
    }
    So I basically want to know how to have all of my threads terminated when they're all interrupted, and why this is happening. Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: I need to manually terminate my threads.

    Can you better explain what you are trying to do and what the output is supposed to be? You
    define some target but it doesn't appear to be used. What is the output supposed to be. Are all threads
    supposed to quit when only one is interrupted? Please explain in detail.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: I need to manually terminate my threads.

    Quote Originally Posted by jim829 View Post
    Can you better explain what you are trying to do and what the output is supposed to be? You
    define some target but it doesn't appear to be used. What is the output supposed to be. Are all threads
    supposed to quit when only one is interrupted? Please explain in detail.

    Regards,
    Jim
    All of the threads are supposed to quit when a single thread finds the target number. The output is supposed to be saying that a thread has found the target number. Then the rest of the threads will output a message saying that they have been interrupted, and will be terminated.

  4. #4
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: I need to manually terminate my threads.

    Quote Originally Posted by coolman50544 View Post
    All of the threads are supposed to quit when a single thread finds the target number. The output is supposed to be saying that a thread has found the target number. Then the rest of the threads will output a message saying that they have been interrupted, and will be terminated.
    On another note, only one thread is supposed to find the number. The other threads should be terminated before they even have a chance to find the number.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: I need to manually terminate my threads.

    Ok, the main problem is your count and wait(1) code. I would get rid of all of that. What happens is that your interruption is caught by the try catch block surrounding the wait. But that also clears the interrupted exception so your loop does not terminate.

    Also your Monitor class does not need to extend Thread since you don't use it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: I need to manually terminate my threads.

    Quote Originally Posted by jim829 View Post
    Ok, the main problem is your count and wait(1) code. I would get rid of all of that. What happens is that your interruption is caught by the try catch block surrounding the wait. But that also clears the interrupted exception so your loop does not terminate.

    Also your Monitor class does not need to extend Thread since you don't use it.

    Regards,
    Jim
    I have been trying various things, but it still doen't work after removing that.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: I need to manually terminate my threads.

    Quote Originally Posted by coolman50544 View Post
    I have been trying various things, but it still doen't work after removing that.
    Ok, I think part of the problem is your run method is synchronized on this which is an instance of FinderThread. Since there
    are four instances, there is nothing to restrict simultaneous access to the same execution context. For example, if three threads are all
    executing code inside the "i == target" block, then they will all report having found the number even if they are interrupted or
    if found is set to true. On the other hand, if you synchronize all the run methods on the same object, then the threads will not run
    concurrently (which is sort of self defeating). So you need to narrow the scope of the your synchronizing statements inside
    the run method. And while you are synchronizing on monitor (which you pass to each class), use the found boolean to see if it
    is already true before taking any further action.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: I need to manually terminate my threads.

    Quote Originally Posted by jim829 View Post
    Ok, I think part of the problem is your run method is synchronized on this which is an instance of FinderThread. Since there
    are four instances, there is nothing to restrict simultaneous access to the same execution context. For example, if three threads are all
    executing code inside the "i == target" block, then they will all report having found the number even if they are interrupted or
    if found is set to true. On the other hand, if you synchronize all the run methods on the same object, then the threads will not run
    concurrently (which is sort of self defeating). So you need to narrow the scope of the your synchronizing statements inside
    the run method. And while you are synchronizing on monitor (which you pass to each class), use the found boolean to see if it
    is already true before taking any further action.

    Regards,
    Jim
    Thanks, Jim. I already fixed the problem before you posted that helpful thread, but thanks for the info. :D

Similar Threads

  1. Why are the threads start and terminate randomly?
    By ggyyree in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-14-2010, 04:11 PM
  2. how can terminate the string
    By abhay23k in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-10-2010, 08:25 PM
  3. event that terminate function
    By itaipee in forum AWT / Swing
    Replies: 6
    Last Post: 12-08-2009, 03:15 PM
  4. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  5. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 01:57 AM

Posting Permissions

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