GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java synchronized - ensuring thread safety in Java Java synchronizedlast modified July 16, 2024 In this article we show how to use the synchronized keyword to ensure thread safety in Java.
The We can control the scope of synchronization by applying synchronized to methods or code blocks.
When applied to a method, it ensures that only one thread can execute the method
at a time. Other threads trying to access the
When applied to a code block, it creates a Locking Mechanism
The
Acquiring and releasing locks can introduce some overhead. The
Use CasesUse synchronized when you have multiple threads accessing and modifying the same data concurrently. This prevents race conditions where the outcome depends on the unpredictable timing of thread execution. It is useful for scenarios where multiple threads need to update a shared counter, modify a data structure, or access a critical section of code that relies on consistent state. Synchronized methodThe following example uses a synchronized method. Main.java
import java.util.Scanner;
class Task {
synchronized void process(int n) {
System.out.println(Thread.currentThread().getName());
for (int i = 1; i <= 10; i++) {
System.out.printf("%d ", n + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println();
}
}
void main() {
// only one object
final Task obj = new Task();
for (int i = 1; i < 5; i++) {
int e = i;
String name = String.format("VirThread-%d", e);
Thread.ofVirtual().name(name).start(() -> obj.process(e * 10));
}
waitForKeyPress();
}
void waitForKeyPress() {
try (var scanner = new Scanner(System.in)) {
scanner.nextLine();
}
}
The example creates five virtual threads that all take the for (int i = 1; i < 5; i++) {
int e = i;
String name = String.format("VirThread-%d", e);
Thread.ofVirtual().name(name).start(() -> obj.process(e * 10));
}
In a for loop, we create and launch five virtual threads. void waitForKeyPress() {
try (var scanner = new Scanner(System.in)) {
scanner.nextLine();
}
}
We use scanner to prevent the main thread from finishing. There are tools such
us $ java Main.java VirThread-1 11 12 13 14 15 16 17 18 19 20 VirThread-4 41 42 43 44 45 46 47 48 49 50 VirThread-3 31 32 33 34 35 36 37 38 39 40 VirThread-2 21 22 23 24 25 26 27 28 29 30 Synchronized counterThe following example creates a synchronized counter. Main.java
class Counter {
private int counter = 0;
public synchronized void inc() {
counter++;
}
public synchronized void dec() {
counter--;
}
public int getCounter() {
return counter;
}
}
class Task extends Thread {
private final String name;
private final Counter counter;
public Task(Counter counter, String name) {
this.counter = counter;
this.name = name;
}
public void run() {
for (int i = 0; i <= 1000; i++) {
if (name.contains("inc"))
counter.inc();
else
counter.dec();
}
}
}
void main() {
final var counter = new Counter();
var taskInc = new Task(counter, "Thread-inc");
var taskDec = new Task(counter, "Thread-dec");
taskInc.start();
taskDec.start();
try {
taskInc.join();
taskDec.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(counter.getCounter());
}
We have two tasks in the example. One increments the counter 1000 times and the other one decrements it 1000 times. So the final output must be zero. class Counter {
private int counter = 0;
public synchronized void inc() {
counter++;
}
public synchronized void dec() {
counter--;
}
public int getCounter() {
return counter;
}
}
In order for the example to work, we must use the try {
taskInc.join();
taskDec.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
The $ java Main.java 0 SourceJava Synchronized Methods - tutorial
In this article we have worked with the AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |