Is this valid?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Is this valid?

  1. #1
    Join Date
    May 2005
    Posts
    3

    Is this valid?

    Is it okay to replace the deprecated method:
    loadThread.stop();

    with

    loadThread = null;
    ?

  2. #2
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    No. Here's what the 1.4 API docs have to say about it.
    Code:
    What should I use instead of Thread.stop?
    
    Most uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. (This is the approach that JavaSoft's Tutorial has always recommended.) To ensure prompt communication of the stop-request, the variable must be volatile (or access to the variable must be synchronized).
    
    For example, suppose your applet contains the following start, stop and run methods:
    
        private Thread blinker;
    
        public void start() {
            blinker = new Thread(this);
            blinker.start();
        }
    
        public void stop() {
            blinker.stop();  // UNSAFE!
        }
    
        public void run() {
            Thread thisThread = Thread.currentThread();
            while (true) {
                try {
                    thisThread.sleep(interval);
                } catch (InterruptedException e){
                }
                repaint();
            }
        }
    
    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();
            }
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center