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
38
votes
9 answers

Java synchronizing based on a parameter (named mutex/lock)

I'm looking for a way to synchronize a method based on the parameter it receives, something like this: public synchronized void doSomething(name){ //some code } I want the method doSomething to be synchronized based on the name parameter like…
Doua Beri
  • 10,612
  • 18
  • 89
  • 138
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
37
votes
6 answers

Should you synchronize the run method? Why or why not?

I have always thought that synchronizing the run method in a java class which implements Runnable is redundant. I am trying to figure out why people do this: public class ThreadedClass implements Runnable{ //other stuff public synchronized…
MHP
  • 385
  • 1
  • 4
  • 8
35
votes
0 answers

What is the synchronized method in Java

What is the main idea of synchronized method and synchronized block in Java? And why should we use them? Sample code would be nice. I have read the Java documentation about synchronized method, but I didn't get the idea. This is the Java…
Antwan
  • 3,837
  • 9
  • 41
  • 62
34
votes
6 answers

Does @synchronized guarantees for thread safety or not?

With reference to this answer, I am wondering is this correct? @synchronized does not make any code "thread-safe" As I tried to find any documentation or link to support this statement, for no success. Any comments and/or answers will be…
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
33
votes
4 answers

Do two synchronized methods execute simultaneously

I have 4 methods (m1, m2, m3 and m4) in a class. Method m1, m2 and m3 are synchronized methods. Also, I have 4 threads t1, t2, t3 and t4 respectively. If t1 access the m1 method (synchronized method), could t2 thread access m2 method (synchronized…
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
33
votes
3 answers

Is synchronized inherited in Java?

I have superclass Point and a synchronized method draw(). Will the subclasses of Point inherit synchronized if I override method draw() in them or I have to always write it?
Yoda
  • 17,363
  • 67
  • 204
  • 344
32
votes
3 answers

Synchronized method calls itself recursively. Is this broken?

The point of this question is to illustrate that Java is not working as I expected. How would you expect the following code to behave? public class SynchTester { private static SynchTester synchTester; public synchronized static SynchTester…
Thom
  • 14,013
  • 25
  • 105
  • 185
31
votes
1 answer

return from inside a @synchronized block in objective-c

May somebody tell me if it is ok to return from inside a @synchronized block? For example: - (id)methodThatReturnsSomething:(BOOL)bDoIt { @synchronized(self) { if(!bDoIt) return nil; ... } …
Vassilis
  • 2,878
  • 1
  • 28
  • 43
30
votes
2 answers

Learning Java, use of synchronized keyword

so i was testing with synchronized keyword. Here is an example that I tried: public class MyTest { static int i = 0; public static void main(String[] args) { new Thread(t1).start(); new Thread(t2).start(); } …
911TurboS
  • 527
  • 2
  • 7
  • 14
30
votes
5 answers

In what situations could an empty synchronized block achieve correct threading semantics?

I was looking through a Findbugs report on my code base and one of the patterns that was triggered was for an empty synchronzied block (i.e. synchronized (var) {}). The documentation says: Empty synchronized blocks are far more subtle and hard to…
sk.
  • 6,336
  • 5
  • 38
  • 46
29
votes
1 answer

Synchronizing on local variable

I noticed a weird construct in ConcurrentHashMap's compute and computeIfAbsent methods: Node r = new ReservationNode(); synchronized (r) { //... } What is the point of synchronizing on a local object considering that the JIT will most…
assylias
  • 321,522
  • 82
  • 660
  • 783
28
votes
6 answers

Cost of locking in .NET vs Java

I was playing with Disruptor framework and its port for .NET platform and found an interesting case. May be I completely miss something so I'm looking for help from almighty Community. long iterations = 500*1000*1000; long testValue…
Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44
28
votes
3 answers

Synchronized Vs Semaphore

While reading concurrency in Java, I have following doubts: Does Java provides lower level construct then synchronized for synchronization? In what circumstances will we use semaphore over synchronized (which provides monitor behaviour in Java)
Addict
  • 803
  • 2
  • 9
  • 16
28
votes
4 answers

Why can't an abstract method be synchronized?

I was reading a thread from CodeRanch saying that abstract methods could not be synchronized due to the fact that an abstract class cannot be instantiated, meaning no object to lock. This doesn't make sense since an abstract class is a definition…
ahodder
  • 11,353
  • 14
  • 71
  • 114