« June 2006 »
SunMonTueWedThuFriSat
    
1
2
3
4
5
6
8
9
10
11
12
13
15
17
18
20
21
23
25
26
 
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 1012

Powered by Roller Weblogger.
« Previous day (Jun 28, 2006) | Main | Next day (Jun 30, 2006) »
20060630 Friday June 30, 2006

Is this MT-safe? I think not...

I came across this article recently: A Safe Way to Stop a Java Thread. The author suggests an alternative for the deprecated Thread.stop() method. The code looks like



public class MyThread extends Thread {

    private boolean threadDone = false;

    public void done() {
        threadDone = true;
    }

    public void run() {
        while (!threadDone) {
            // work here
            // modify common data
        }
    }

}


MyThread.done() is supposed to be called by another thread that wants to stop a thread (that is running MyThread.run()). But, there is a problem here: threadDone variable is read by one thread and written by another thread. To ensure prompt communication of the stop-request, the threadDone variable must be volatile (or access to the threadDone variable must be synchronized):

    volatile private boolean threadDone = false;

Please refer to this as well: Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?



( Jun 30 2006, 09:32:51 AM IST ) Permalink Comments [3] del.icio.us | furl | simpy | slashdot | technorati | digg

Copyright (C) 2005, A. Sundararajan's Weblog