Solved

volatile keyword in java 1.3

Posted on 2008-10-02
6
572 Views
Last Modified: 2012-05-05
Hi,

Does the volatile keyword make a primitive safe to read/write from multiple threads without guarding it? I don't know where I read that but - something like:

public class Test
{
    public volatile m_flag;

    new Thread() {
        public void run() {
            while(...) {
                m_flag = false;
                print(m_flag);
            }
        }
    }.start();

    new Thread() {
        public void run() {
            while(...) {
                m_flag = true;
                print(m_flag);
            }
        }
    }.start();
}

I mean is setting and getting m_flag atomic now, so no thread is reading or writing it at the same time? I can put a guard object around it but it seems annoying to do for a simple boolean primitive,

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:mnrz
ID: 22627536
no I think  this is not safe, you had better to change it into this line:

private volatile java.util.concurrent.atomic.AtomicBoolean m_flag = false;

atomic types make your variable safe in read/write concurrently
0
 
LVL 8

Expert Comment

by:mnrz
ID: 22627545

private volatile java.util.concurrent.atomic.AtomicBoolean m_flag = false;

Open in new window

0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22627896
mnrz: java.util.concurrent is not available to 1.3 as far as I know. Has this changed?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
mbodewes earned 500 total points
ID: 22627973
No, this does not work, the change may happen in between the setting and the printing. So the print statement may print about anything. There is nothing locking the volatile variable between these two states. You might want to synchronize the code parts setting and printing the variable instead.
0
 
LVL 8

Expert Comment

by:mnrz
ID: 22631793
yes in version 1.3 you need to synchronize it
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 22650055
ok thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question