java:threads:synchronized-example
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:threads:synchronized-example [2024/01/17 00:31] – odefta | java:threads:synchronized-example [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 64: | Line 64: | ||
| **Synchronized Block on Class (Class object)**: | **Synchronized Block on Class (Class object)**: | ||
| - | |||
| - | The syncBlockOnClass method contains a synchronized block on the Class object of SynchronizedExample. | ||
| - | This synchronized block restricts execution to one thread across all instances for that specific block of code, useful for handling static data shared across instances. | ||
| - | |||
| - | |||
| - | Certainly! Here's the explanation again in English: | ||
| - | |||
| - | In the provided Java example, we have different ways of applying synchronization: | ||
| - | |||
| - | Synchronized Method on Instance (this): | ||
| - | |||
| - | The syncMethodOnInstance method is synchronized on the instance of the class (this). | ||
| - | This means that only one thread can execute this method on the same instance of SynchronizedExample at any given time. | ||
| - | Synchronized Block on Instance (this): | ||
| - | |||
| - | In the syncBlockOnInstance method, a specific block of code is synchronized on the current instance (this). | ||
| - | This allows for more granular control, as only the code within the synchronized block is subject to mutual exclusion, as opposed to the entire method. | ||
| - | Synchronized Method on Class (Class object): | ||
| - | |||
| - | The syncMethodOnClass method is a static method synchronized on the Class object of SynchronizedExample. | ||
| - | This means that only one thread can execute this method across all instances of the class at a time. It's useful for controlling access to static or class-level data. | ||
| - | Synchronized Block on Class (Class object): | ||
| The syncBlockOnClass method contains a synchronized block on the Class object of SynchronizedExample. | The syncBlockOnClass method contains a synchronized block on the Class object of SynchronizedExample. | ||
java/threads/synchronized-example.1705451512.txt.gz · Last modified: (external edit)
