Questions tagged [synchronized]

A block or method is said to be 'synchronized' if entry to it is controlled by the Java `synchronized` keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.

A block or method is said to be 'synchronized' if entry to it is controlled by the Java synchronized keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.

1865 questions
1120
votes
17 answers

What does 'synchronized' mean?

I have some questions regarding the usage and significance of the synchronized keyword. What is the significance of the synchronized keyword? When should methods be synchronized? What does it mean programmatically and logically?
Johanna
  • 27,036
  • 42
  • 89
  • 117
421
votes
23 answers

Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Can any one tell me the advantage of synchronized method over synchronized block with an example?
Warrior
  • 39,156
  • 44
  • 139
  • 214
407
votes
22 answers

Avoid synchronized(this) in Java?

Whenever a question pops up on SO about Java synchronization, some people are very eager to point out that synchronized(this) should be avoided. Instead, they claim, a lock on a private reference is to be preferred. Some of the given reasons…
eljenso
  • 16,789
  • 6
  • 57
  • 63
302
votes
4 answers

Difference between volatile and synchronized in Java

I am wondering at the difference between declaring a variable as volatile and always accessing the variable in a synchronized(this) block in Java? According to this article http://www.javamex.com/tutorials/synchronization_volatile.shtml there is a…
Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105
227
votes
11 answers

Java synchronized method lock on object, or method?

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized…
wuntee
  • 12,170
  • 26
  • 77
  • 106
223
votes
2 answers

What is the reason why “synchronized” is not allowed in Java 8 interface methods?

In Java 8, I can easily write: interface Interface1 { default void method1() { synchronized (this) { // Something } } static void method2() { synchronized (Interface1.class) { //…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
220
votes
12 answers

If I synchronized two methods on the same class, can they run simultaneously?

If I synchronized two methods on the same class, can they run simultaneously on the same object? For example: class A { public synchronized void methodA() { //method A } public synchronized void methodB() { // method B …
Shelef
  • 3,707
  • 6
  • 23
  • 28
150
votes
8 answers

Java synchronized static methods: lock on object or class

The Java documentation says: It is not possible for two invocations of synchronized methods on the same object to interleave. What does this mean for a static method? Since a static method has no associated object, will the synchronized…
jbu
  • 15,831
  • 29
  • 82
  • 105
119
votes
4 answers

Java Synchronized Block for .class

What does this java code mean? Will it gain lock on all objects of MyClass? synchronized(MyClass.class) { //is all objects of MyClass are thread-safe now ?? } And how the above code differs from this one: synchronized(this) { //is all objects…
user249739
109
votes
8 answers

Synchronization of non-final field

A warning is showing every time I synchronize on a non-final class field. Here is the code: public class X { private Object o; public void setO(Object o) { this.o = o; } public void x() { …
divz
  • 7,847
  • 23
  • 55
  • 78
77
votes
11 answers

In Java critical sections, what should I synchronize on?

In Java, the idiomatic way to declare critical sections in the code is the following: private void doSomething() { // thread-safe code synchronized(this) { // thread-unsafe code } // thread-safe code } Almost all blocks synchronize on …
lindelof
  • 34,556
  • 31
  • 99
  • 140
77
votes
2 answers

Side effects of throwing an exception inside a synchronized clause?

Are there any unclear side effects to throwing an exception from within a synchronized clause? What happens to the lock? private void doSomething() throws Exception {...} synchronized (lock) { doSomething(); }
Yossale
  • 14,165
  • 22
  • 82
  • 109
74
votes
4 answers

Should getters and setters be synchronized?

private double value; public synchronized void setValue(double value) { this.value = value; } public double getValue() { return this.value; } In the above example is there any point in making the getter synchronized?
DD.
  • 21,498
  • 52
  • 157
  • 246
69
votes
9 answers

Why can't Java constructors be synchronized?

According to the Java Language Specification, constructors cannot be marked synchronized because other threads cannot see the object being created until the thread creating it has finished it. This seems a bit odd, because I can indeed have another…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
60
votes
20 answers

Synchronizing on String objects in Java

I have a webapp that I am in the middle of doing some load/performance testing on, particularily on a feature where we expect a few hundred users to be accessing the same page and hitting refresh about every 10 seconds on this page. One area of…
matt b
  • 138,234
  • 66
  • 282
  • 345
1
2 3
99 100