0

Possible Duplicate:
Difference between volatile and synchronized in JAVA (j2me)

I am a bit confused with the 2 java keywords synchronized and volatile.

To what i understand, since java is a multi-threaded language, and by using the keyword synchronized will force it to be executed in 1 thread. Am i correct ?

And volatile also does the same thing ?

Community
  • 1
  • 1
user1315906
  • 3,374
  • 8
  • 30
  • 43
  • 1
    "java is a multi-threaded language". If you mean Java is thread safe, that's not true. – auselen Dec 21 '12 at 23:12
  • According to http://javarevisited.blogspot.com/2011/04/synchronization-in-java-synchronized.html it says it's a multi-threaded language. – user1315906 Dec 21 '12 at 23:14
  • That page doesn't look creditable at all. Java supports threads, and I guess if you start more that one thread in your code you are creating a multi-threaded application. However Java as language doesn't have very novel constructs to run threads. – auselen Dec 21 '12 at 23:17

7 Answers7

6

Both volatile and synchronized guarantee visibility, but synchronized also provides atomicity:

  • if one thread thread reads a volatile variable, it has the guarantee to see the previous writes to that same variable, including if they were done by other threads
  • synchronized blocks give the same guarantee (provided the write and the read are done holding the same monitor) but also provides atomicity guarantees: all instructions within a synchronized block will look atomic from another thread synchronized on the same lock.
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
whoever
  • 61
  • 1
4

For long and double read and write operation are not atomic. volatile is a modifier you can put on a variable to make read and write operations atomic (including long and double)

The article Atomic Access got more information about it.

Basically it is for making sure that if one thread changed the value of the variable the rest of the threads will see that change and will not able to read it while its written.

Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
  • It says `volatile` is used to reduce memory consistency errors? what does that mean – user1315906 Dec 21 '12 at 23:19
  • @user1315906 Threads can cache values of variables. So even if one thread will update some value there is risk that other threads will use their cached value. To prevent caching of variable use `volatile`. – Pshemo Dec 21 '12 at 23:23
  • 4
    The main promise of volatile is not about atomicity but about visibility. – assylias Dec 21 '12 at 23:27
2

Java multi-threading involves two problems, ensuring that multiple operations can be done consistently, without mixing actions by different threads, and making a change in a variable's value available to threads other than the on doing the change.

In reality, a variable does not naturally exist at a single location in the hardware. There may be copies in the internal state of different threads, or in different hardware caches. Simply assigning to a variable automatically changes its value from the point of view of the thread doing the assignment.

If the variable is marked "volatile" other threads will get the changed value.

"synchronized" also ensures changes become visible. Specifically, any change done in one thread before the end of a synchronized block will be visible to reads done by another thread in a subsequent block synchronized on the same object.

In addition, blocks that are synchronized on the same object are forced to run sequentially, not in parallel. That allows one to do things like adding one to a variable, knowing that its value will not change between reading the old value and writing the new one. It also allows consistent changes to multiple variables.

The best way I know to learn what is needed to write solid concurrent code in Java is to read Java Concurrency in Practice

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
1

Essentially, volatile is used to indicate that a variable's value will be modified by different threads.

Synchronized is a keyword whose overall purpose is to only allow one thread at a time into a particular section of code thus allowing us to protect, for example, variables or data from being corrupted by simultaneous modifications from different threads.

turnt
  • 3,235
  • 5
  • 23
  • 39
1

It is as complicated as described lengthy in Java Memory Model.

auselen
  • 27,577
  • 7
  • 73
  • 114
1

Synchronized means that only one thread can access the method or code block at a given time.

Volatile handles the communication between threads. Here is a nice explanation: http://jeremymanson.blogspot.be/2008/11/what-volatile-means-in-java.html

asgoth
  • 35,552
  • 12
  • 89
  • 98
0

Volatile keyword will make every thread to read or write as an atomic operation in memory location. If you do not use volatile, that variable may be cached by threads, and threads may be reading/writing cached copy instead of actual copy.

Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
nullptr
  • 3,320
  • 7
  • 35
  • 68