Solved

volatile keyword in java 1.3

Posted on 2008-10-02
6
568 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
Apps blocked by Java 9 79
xampp tool 12 48
arguments to jar 5 26
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

785 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