Results 1 to 12 of 12
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default sleep() and stop()

    Java Code:
    import java.lang.Runnable;
    class Counter implements Runnable {
         public int count = 1;
          public void run()
          {
              while (count < 3)
              {
                  try
                  {
                  	System.out.println(count);
                    Thread.sleep(2000);
                  } 
                  catch (InterruptedException e)
                  {
                  	System.out.println("InterruptedException"); 
                  }
                count++;
            }
            System.out.println("+");  
        }
      } 
    
    public class B
    {
    	public static void main(String args[]) throws Exception
    	{
    		Thread   t = new Thread(new Counter(),"CounterThread");
    		t.start();
    		Thread.sleep(2000);
    		t.stop();
    
    	}
    }

    Hello
    I do not know exactly how stop() works?
    Does this function stops "CounterThread" after t.stop(); command.
    I run this program and saw that t.stop(); command stops "CounterThread".
    with thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please read the Thread API: Thread (Java Platform SE 6)

    There you'll see that stop() has been deprecated and why it has been deprecated. In other words, just don't use it.

  3. #3
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I saw that link and i found its way for replacing Thread.stop()
    You can avoid the use of Thread.stop by replacing the applet's stop and run methods with:
    private volatile Thread blinker;

    public void stop() {
    blinker = null;
    }

    public void run() {
    Thread thisThread = Thread.currentThread();
    while (blinker == thisThread) {
    try {
    thisThread.sleep(interval);
    } catch (InterruptedException e){
    }
    repaint();
    }
    }
    but, i do not know that what do i replace instead of t.stop() in my code?
    please guide me.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What do you want to achieve with your code?

    A thread dies when its run method completes. If you want to stop a thread under certain conditions just make sure that the run method completes immediately after those conditions are met.

  5. #5
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I saw this text:

    Why is Thread.stop deprecated?
    Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.

    Thus, I should replace the better code instead of Thread.stop(). My question in the first post is a practice for me to understand how a thread dies, but your question from me was nice for me:


    What do you want to achieve with your code?
    actually, why should a thread die? Why Thread.stop() created in java and why use of that is unsafe?


    With thanks

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Many methods were created in many classes that were later realized to be not as great as they were originally thought to be. Most of these methods can be used correctly but doing so requires soo much extra care and effort that it is best not to have them at all.

    The reason why you should not use the stop method is explained in the passage you quoted above.

  7. #7
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I thank you about your guidance.
    I have a code about replacing Thread.stop(),but this code can not stop the thread. why?
    Java Code:
    import java.lang.Runnable;
    class Counter implements Runnable {
    
         public int count = 1;
         private volatile boolean stop = true;
          public void run()
          {
              while (stop && count < 3)
              {
                  try
                  {
                  	System.out.println(count+" "+stop);
                    Thread.sleep(2000);
                  } 
                  catch (InterruptedException e)
                  {
                  	System.out.println("InterruptedException"); 
                  }
                count++;
            }
            System.out.println("+");  
            if(!stop)
            {
            	System.out.println("Detected Stop");
            }
    
        }
        public void requestStop()
        {
        	stop=false;
        	System.out.println("stop= "+stop);
        }
      } 
    
    public class B
    {
    	public static void main(String args[]) throws Exception
    	{
    		Counter  r = new Counter();
    		Thread   t = new Thread(new Counter(),"CounterThread");
    		t.start();
    		Thread.sleep(1000);
    		//t.stop();//Counter Thread dead!
                              r.requestStop();
    	}
    }

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    with "class Counter extends Thread {" instead of "class Counter implements Runnable {"
    my problem solved,but what do i do about "class Counter implements Runnable {".

    Java Code:
    class Counter extends Thread {
          public int count = 1;
          private volatile boolean stop = true;
          public void run()
          {
              while (stop && count < 3)
              {
                  try
                  {
                  	System.out.println(count+" "+stop);
                    Thread.sleep(2000);
                  } 
                  catch (InterruptedException e)
                  {
                  	System.out.println("InterruptedException"); 
                  }
                  count++;
            }
            System.out.println("+");  
            if(stop==false)
            {
            	System.out.println("Detected Stop");
            }
        }
        public void requestStop()
        {
        	stop=false;
        	System.out.println("stop= "+stop);
        }
      } 
    
    public class B
    {
    	public static void main(String args[]) throws Exception
    	{
    		Counter  r = new Counter();
    		r.start();
    		Thread.sleep(1000);
                              r.requestStop();
    	}
    }

  9. #9
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    my problem did not solve. please help me until i can solve this problem.
    with thanks

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    I thank you about your guidance.
    I have a code about replacing Thread.stop(),but this code can not stop the thread. why?
    Java Code:
    import java.lang.Runnable;
    class Counter implements Runnable {
    
         public int count = 1;
         private volatile boolean stop = true;
          public void run()
          {
              while (stop && count < 3)
              {
                  try
                  {
                  	System.out.println(count+" "+stop);
                    Thread.sleep(2000);
                  } 
                  catch (InterruptedException e)
                  {
                  	System.out.println("InterruptedException"); 
                  }
                count++;
            }
            System.out.println("+");  
            if(!stop)
            {
            	System.out.println("Detected Stop");
            }
    
        }
        public void requestStop()
        {
        	stop=false;
        	System.out.println("stop= "+stop);
        }
      } 
    
    public class B
    {
    	public static void main(String args[]) throws Exception
    	{
    		Counter  r = new Counter();
    		Thread   t = new Thread(new Counter(),"CounterThread");
    		t.start();
    		Thread.sleep(1000);
    		//t.stop();//Counter Thread dead!
                              r.requestStop();
    	}
    }



    You need to use the same Counter object for the thread and the request to stop. So it's not this:
    Java Code:
       public static void main(String args[]) throws Exception
       {
          Counter  r = new Counter();
          Thread   t = new Thread(new Counter(),"CounterThread"); // you've a new Counter here
          t.start();
          Thread.sleep(1000);
          r.requestStop();
       }

    but instead this:
    Java Code:
       public static void main(String args[]) throws Exception
       {
          Counter  r = new Counter();
          Thread   t = new Thread(r,"CounterThread"); // *** this is better ***
          t.start();
          Thread.sleep(1000);
          r.requestStop();
       }
    Otherwise you will be changing the stop variable of an object different from the one being threaded, and setting it to true will have no effect.

  11. #11
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Thank you very much. I underestand it.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. How to stop Thread
    By ersachinjain in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-30-2009, 07:11 PM
  2. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 10:58 PM
  3. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  4. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 08:44 PM
  5. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 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
  •