Results 1 to 4 of 4

Thread: [RESOLVED] Unable to stop/suspend the thread.

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Resolved [RESOLVED] Unable to stop/suspend the thread.

    Hi all,

    I have use a Thread just to print some numbers as follows.

    Code:
        Thread tempTH = new Thread(new Runnable() {
    
            public void run(){          
                int counter = 1;
                while(counter != 0){ // Condition to change
                    try {
                        System.out.println(counter++);
                        Thread.sleep(1000);
                    } 
                    catch (InterruptedException ex){
                        System.out.println(ex);
                    }
                }
               tempTH.start();
            }
        });
    I want to stop this thread, and try to following

    Code:
    tempTH.stop();
    Actually I can;'t do that. stop() is not allowed to use. As you know in my IDE simply draw a line across the word stop() and notify that I can't do it.

    Why is that, any idea.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Unable to stop/suspend the thread.

    There is always a work around
    Code:
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Util {
    
        private static boolean running = true;
    
        public static void main(String[] args) {
    	try {
    	    Thread countThread = new Thread(new Runnable() {
    
    		public void run() {
    		    int counter = 1;
    		    while (counter != 0 && running) {
    			// Condition to change
    			try {
    			    System.out.println(counter++);
    			    Thread.sleep(1000);
    			} catch (InterruptedException ex) {
    			    System.out.println(ex);
    			}
    		    }
    		}
    	    });
    	    countThread.start();
    	    System.out.println("Press type anything and press enter to stop counting");
    	    System.in.read();
    	    running = false;
    	} catch (IOException ex) {
    	    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    	}
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Unable to stop/suspend the thread.

    Actually I have do something else. In every time(actually loop) I used the thread, synchronized it. Then i notify when I want to suspend or stop. It's work actually.

    Hope you got that what I say.

    Thanks.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Unable to stop/suspend the thread.

    Quote Originally Posted by ComputerJy
    There is always a work around
    Code:
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Util {
    
        private static boolean running = true;
    
        public static void main(String[] args) {
    	try {
    	    Thread countThread = new Thread(new Runnable() {
    
    		public void run() {
    		    int counter = 1;
    		    while (counter != 0 && running) {
    			// Condition to change
    			try {
    			    System.out.println(counter++);
    			    Thread.sleep(1000);
    			} catch (InterruptedException ex) {
    			    System.out.println(ex);
    			}
    		    }
    		}
    	    });
    	    countThread.start();
    	    System.out.println("Press type anything and press enter to stop counting");
    	    System.in.read();
    	    running = false;
    	} catch (IOException ex) {
    	    Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    	}
        }
    }

    I got what you have done here. But I have one question here.

    In my code I set the sleep time to 10 seconds (10000 milliseconds actually). If I suspends the thread, it is suspend after 10 seconds. Not suspended at the same time I call to suspend.

    Here is my code.

    Code:
        boolean threadSuspend = false;
        
        Thread processThread;
        int counter;
    
        public void init(){
            counter = 0;
            processThread = new Thread(this);
            processThread.start();
        }
    
        public void run(){
            while(true){
                try {
                    System.out.println(counter++);
                    Thread.sleep(10000);
                    synchronized(this){
                        while(threadSuspend){
                            try{
                                wait();                            
                            }
                            catch(Exception ex){
                                Logger.getLogger(Processing.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
               }
                catch (InterruptedException ex) {
                    Logger.getLogger(Processing.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    I've done this to suspend the thread.

    Code:
            synchronized(appProcess){
                appProcess.threadSuspend = true;
            }
    appProcess is the thread class actually.

    To resume the thread do following.

    Code:
            synchronized(appProcess){
                appProcess.threadSuspend = false;
                appProcess.notify();
            }
    Do you know why this is happened. Actually it suspend and resume the thread at the same if I have done it after 10 seconds. If I try to suspend or resume within the 10 second, it happened after reach to 10 second limit.

    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

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



Click Here to Expand Forum to Full Width