File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Volatile - synchronized ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Volatile - synchronized ?" Watch "Volatile - synchronized ?" New topic

Volatile - synchronized ?

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1432
Can i specify volatile variable within synchronized method or synchronized block ? Volatile works only with multithreaded code(non thread-safe) ?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
volatile is a modifier that only applies for instance and class variables.

It makes that different Threads do not cache the value of the corresponding variable and hence always retrieve the latest, up-to-date, could-have-been-modified-by-another-thread value.

JDBCSupport - An easy to use, light-weight JDBC framework -
Monu Tripathi

Joined: Oct 12, 2008
Posts: 1369

If you are interested in understanding Synchronization and about volatile modifier and if you have time, consider reading Synchronization and Java Memory Model.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Probably best moved to the threads forum.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

One thing to note, there is only one non access modifier for local variables and that is final. There are no other non access modifiers for local variables.

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
I agree. Here's the link:
subject: Volatile - synchronized ?
It's not a secret anymore!