What does synchronized and LOCK do

Alan Ramirez
1 min readSep 21, 2020

--

The synchronized(){} method belongs to the category of multithreading concepts. It is used to stop threads from simultaneously accessing in any way the code that this method encapsulates.

It takes one parameter which is by convention named LOCK and of type Any() in kotlin.

The way it works is just like a talking stick does. whichever thread holds the LOCK gets to access the stuff inside the synchronized method, all other threads must wait in line (will be paused) until they receive the LOCK. No worries you do not have to handle the passing of the LOCK itself between the threads, they will just get in line and obtain it once the thread in front of them is done executing the code inside the synchronized method.

--

--