synchronized indicates that only one thread can execute this block of code at a time.
In this example, without a synchronized block, multiple threads can draw graphics at the same time, and the results can be messy. Thus, synchronized ensures that only one thread can draw at a time.
lockCanvas() creates an area of ββthe surface you will write to. The reason it's called lockCanvas() is because until you call unlockCanvasAndPost() , no other code can call lockCanvas() and write to the surface until your code is complete.
In general, locks are important for understanding, especially when it comes to multithreaded programming. Blocking is a synchronization primitive that is used to protect against multiple threads / code accessing simultaneously. He gets this name because he behaves like a physical castle. Typically, one thread can get a lock, and until it releases the lock, no other thread can get a lock. One of the possible ways to use a lock is that improper use of this can lead to a "dead lock" situation, when threads remain waiting for a lock and are never released.
source share