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
59
votes
12 answers

Java Multithreading concept and join() method

I'm confused in join() method used in Threads in Java. In the following code: // Using join() to wait for threads to finish. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { …
user2696258
  • 1,159
  • 2
  • 14
  • 26
59
votes
6 answers

Is ConcurrentHashMap totally safe?

this is a passage from JavaDoc regarding ConcurrentHashMap. It says retrieval operations generally do not block, so may overlap with update operations. Does this mean the get() method is not thread safe? "However, even though all operations are…
user697911
  • 10,043
  • 25
  • 95
  • 169
56
votes
5 answers

What is the difference between synchronized on lockObject and using this as the lock?

I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part. Assuming I have this code class Test { private int x=0; private Object lockObject = new Object(); public void…
GantengX
  • 1,471
  • 2
  • 16
  • 28
55
votes
5 answers

Is HttpSession thread safe, are set/get Attribute thread safe operations?

Also, does the object that is being set have to be thread safe in order to guarantee that we know what the state of the object stored in session is known. Also, I was reading on the web that some suggest using: synchronized(session) { …
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203
54
votes
3 answers

Happens-before relationships with volatile fields and synchronized blocks in Java - and their impact on non-volatile variables?

I am still pretty new to the concept of threading, and try to understand more about it. Recently, I came across a blog post on What Volatile Means in Java by Jeremy Manson, where he writes: When one thread writes to a volatile variable, and…
Christian
  • 6,070
  • 11
  • 53
  • 103
54
votes
2 answers

Overriding synchronized methods in Java

Let's say I have a synchronized method on some class: abstract class Foo { public synchronized void foo() { // synchronized! // ... }; } and I overrode it without using the synchronized modifier: class Bar extends Foo { …
Markus A.
  • 12,349
  • 8
  • 52
  • 116
53
votes
5 answers

why using volatile with synchronized block?

I saw some examples in java where they do synchronization on a block of code to change some variable while that variable was declared volatile originally .. I saw that in an example of singleton class where they declared the unique instance as…
50
votes
4 answers

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

In Java, if a synchronized method contains a call to a non-synchronized, can another method still access the non-synchronized method at the same time? Basically what I'm asking is everything in the synchronized method have a lock on it (including…
dido
  • 3,347
  • 11
  • 34
  • 42
49
votes
5 answers

synchronized block - lock more than one object

I'm modelling a game where multiple players (threads) move at the same time. The information of where a player is located at the moment is stored twice: the player has a variable "hostField" that references to a field on the board and every field…
speendo
  • 13,045
  • 22
  • 71
  • 107
47
votes
3 answers

Two threads executing synchronized block simultaneously

Below is the code where a Thread enters a synchronized block, waits for 5 seconds and then exits. I have started two Thread instances simultaneously. The expectation was one of the threads will own the lock on the synchronized object, and the other…
47
votes
2 answers

Does a ConcurrentHashMap need to be wrapped in a synchronized block?

Do all non-retreival operations on a ConcurrentHashMap (put(), remove() etc.) need to be wrapped in a synchronized(this) block? I understand that all of these operations are thread-safe, so is there any real benefit/need in doing so? The only…
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
45
votes
5 answers

java syntax: "synchronized (this)"

can you please explain to me this piece of java code? I cannot understand this syntax. synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } }
user822931
42
votes
1 answer

Static versus non-static lock object in synchronized block

Trying to visualize and understand synchronization. What are the differences between using a static lock object (code A) and a non-static lock object (code B) for a synchronized block? How does it differ in practical applications? What are the…
ADTC
  • 8,999
  • 5
  • 68
  • 93
41
votes
7 answers

How to differentiate when wait(long timeout) exit for notify or timeout?

Having this wait declaration: public final native void wait(long timeout) throws InterruptedException; It could exit by InterruptedException, or by timeout, or because Notify/NotifyAll method was called in another thread, Exception is easy to catch…
Hernán Eche
  • 6,529
  • 12
  • 51
  • 76
40
votes
5 answers

Why are synchronize expensive in Java?

I am really new to Java and I read that synchronized is "very expensive" in Java. All I want to know is what is expensive and how is it expensive? Thanks.
unj2
  • 52,135
  • 87
  • 247
  • 375