Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronized Methods in Java

Just wanted to check to make sure that I understand this. A synchronized method doesn't create a thread, right? It only makes sure that no other thread is invoking this method while one thread within the same process (ie the JVM) is using it, right?

like image 380
user919860 Avatar asked Sep 01 '11 15:09

user919860


2 Answers

A synchronized method doesn't create a thread, right?

Right.

It only makes sure that no other thread is invoking this method while one thread within the same process (ie the JVM) is using it, right?

Right.

For more information, read Synchronized Methods. I also recommend reading Java Concurrency in Practice.

like image 176
mre Avatar answered Nov 16 '22 03:11

mre


That's mostly correct. Calling a synchronized method does not spawn a new thread. It just makes other threads block when they try to call any other synchronized method for that instance of that object.

The key thing to remember is that all synchronized methods of a class use the same lock.

like image 38
Jon7 Avatar answered Nov 16 '22 04:11

Jon7