Questions tagged [synchronized-block]

85 questions
60
votes
2 answers

In java, return value within synchronized block seems like bad style. Does it really matter?

I have a Collections.synchronizedList of WeakReference, _components; I wrote something like the following, expecting the complier to complain: public boolean addComponent2(Component e) { synchronized (_components) { return…
Charlweed
  • 1,517
  • 2
  • 14
  • 22
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
38
votes
6 answers

What is the difference between a synchronized method and synchronized block in Java?

What is the difference between a synchronized method and synchronized block in Java ? I have been searching the answer on the Net, people seem to be so unsure about this one :-( My take would be there is no difference between the two, except that…
Geek
  • 23,089
  • 20
  • 71
  • 85
32
votes
3 answers

What is the purpose of passing parameter to synchronized block?

I know that When you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more…
John Rambo
  • 906
  • 1
  • 17
  • 37
21
votes
7 answers

It is better to have a synchronized block inside a try block or a try block inside a synchronized block?

For example, is this better? try { synchronized (bean) { // Write something } } catch (InterruptedException e) { // Write something } Or it's better this: synchronized (bean) { try { // Write…
user1883212
  • 7,539
  • 11
  • 46
  • 82
13
votes
8 answers

Synchronized block not working

This exercise is straight out of SCJP by Kathy Seirra and Bert Bates Synchronizing a Block of Code In this exercise we will attempt to synchronize a block of code. Within that block of code we will get the lock on an object, so that other threads…
atsurti
  • 294
  • 1
  • 4
  • 15
9
votes
2 answers

AtomicBoolean vs synchronized block

I was trying to cut thread contention in my code by replacing some synchronized blocks with AtomicBoolean. Here's an example with synchronized: public void toggleCondition() { synchronized (this.mutex) { if (this.toggled) { …
biasedbit
  • 2,860
  • 4
  • 30
  • 47
9
votes
5 answers

Rejecting class because it failed compile-time verification Android

One of my application suddenly fails on startup, with the following error message : java.lang.VerifyError: Rejecting class com.sample.BufferManagerImpl because it failed compile-time verification (declaration of …
XGouchet
  • 10,002
  • 10
  • 48
  • 83
5
votes
0 answers

RxJava - synchronized block - inner Source

I want to use synchronized block for source of flatMap. But I need to use this construct for processing (method processItem), not only when inner source is created. This Observable is called each 5 minutes (for example…
5
votes
5 answers

Is this ok? Synchronized( thread ), then thread=null in the synch block

I see this: // thread is a member of this class synchronized( this.thread ) { this.thread.running = false; this.thread.notifyAll(); // Wake up anything that was .waiting() on // the thread this.thread = null; // kill this thread…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
5
votes
1 answer

Concurrency in Java using synchronized blocks not giving expected results

Below is a trivial java program. It has a counter called "cnt" that is incremented and then added to a List called "monitor". "cnt" is incremented by multiple threads, and values are added to "monitor" by multiple threads. At the end of the method…
5
votes
4 answers

Does synchronized (this) lock only the synchronized block or all the "this" code?

public class ObjectCounter { private static long numOfInstances = 0; public ObjectCounter(){ synchronized(this){ numOfInstances++; } } **public static synchronized long getCount(){ return…
5
votes
2 answers

How do I exit a monitor in bytecode properly?

I was reading the JVM specification to try to figure out how to properly handle monitors. The example they give in the relevant section looks like this: 0 aload_1 // Push f 1 dup // Duplicate it on the stack 2 …
user11171
  • 3,821
  • 5
  • 26
  • 35
4
votes
2 answers

using this keyword with synchronized block in java

I have two thread that have access to an object. with synchronized(a), i supply lock on object a so now in each time on thread can access to object "a" and modify that.if execute this code we have 1 2. without synchronized block some times we get 2…
kankan256
  • 210
  • 1
  • 4
  • 18
4
votes
2 answers

Does Java `synchronized` block lock on Object reference, or value?

Given the output shown below: Path path1 = Paths.get("/Users/someone/foo"); Path path2 = Paths.get("/Users/someone/foo"); System.out.println(path1.toString() == path2.toString()); // outputs false …
1
2 3 4 5 6