Solved

volatile keyword in java 1.3

Posted on 2008-10-02
6
563 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now