跳到主要内容

Best Practices of Java Core Multi-Threading Programming

· 阅读需 9 分钟

Introduction​

Java's multi-threading capabilities are powerful, but they require careful handling to avoid common pitfalls such as race conditions, deadlocks, and excessive synchronization. This article provides a deep dive into best practices for Java core multi-threading programming, including detailed textual diagrams, explanations, and real-life production sample code.


Thread Safety​

Use Synchronization Wisely​

Synchronization ensures that only one thread can execute a synchronized block or method at a time. However, excessive synchronization can lead to performance bottlenecks.

+-------------------+ +-------------------+
| | | |
| Thread 1 |<----->| Shared Resource|
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Thread 2 |<----->| Shared Resource|
| | | |
+-------------------+ +-------------------+
class Counter {
private int count = 0;

public synchronized void increment() {
count++;
}

public synchronized int getCount() {
return count;
}
}

public class Main {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();

Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

t1.start();
t2.start();

t1.join();
t2.join();

System.out.println("Count: " + counter.getCount()); // Count: 2000
}
}

Avoid Excessive Synchronization​

Excessive synchronization can lead to performance issues. Use finer-grained locks or non-blocking algorithms where possible.

class FineGrainedCounter {
private int count = 0;
private final Object lock = new Object();

public void increment() {
synchronized (lock) {
count++;
}
}

public int getCount() {
synchronized (lock) {
return count;
}
}
}

Use Atomic Classes​

Atomic classes provide thread-safe operations on single variables without the need for explicit synchronization.

import java.util.concurrent.atomic.AtomicInteger;

class AtomicCounter {
private AtomicInteger count = new AtomicInteger(0);

public void increment() {
count.incrementAndGet();
}

public int getCount() {
return count.get();
}
}

public class Main {
public static void main(String[] args) throws InterruptedException {
AtomicCounter counter = new AtomicCounter();

Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

t1.start();
t2.start();

t1.join();
t2.join();

System.out.println("Count: " + counter.getCount()); // Count: 2000
}
}

Thread Communication​

Use wait() and notify()​

Threads can communicate using wait(), notify(), and notifyAll() methods.

+-------------------+ +-------------------+
| | | |
| Producer |<----->| Consumer |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Shared Buffer |<----->| Shared Buffer |
| | | |
+-------------------+ +-------------------+
class SharedBuffer {
private int[] buffer = new int[10];
private int count = 0;

public synchronized void produce(int value) throws InterruptedException {
while (count == buffer.length) {
wait();
}
buffer[count++] = value;
notifyAll();
}

public synchronized int consume() throws InterruptedException {
while (count == 0) {
wait();
}
int value = buffer[--count];
notifyAll();
return value;
}
}

class Producer implements Runnable {
private SharedBuffer buffer;

public Producer(SharedBuffer buffer) {
this.buffer = buffer;
}

@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
buffer.produce(i);
System.out.println("Produced: " + i);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class Consumer implements Runnable {
private SharedBuffer buffer;

public Consumer(SharedBuffer buffer) {
this.buffer = buffer;
}

@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
int value = buffer.consume();
System.out.println("Consumed: " + value);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public class Main {
public static void main(String[] args) {
SharedBuffer buffer = new SharedBuffer();
new Thread(new Producer(buffer)).start();
new Thread(new Consumer(buffer)).start();
}
}

Avoid Busy Waiting​

Busy waiting is inefficient and should be avoided. Use wait() and notify() instead.

class BusyWaitExample {
private boolean flag = false;

public synchronized void setFlag(boolean flag) {
this.flag = flag;
notifyAll();
}

public synchronized void waitForFlag() throws InterruptedException {
while (!flag) {
wait();
}
}
}

Thread Pooling​

Use ExecutorService​

Thread pooling manages a pool of worker threads to execute tasks concurrently.

+-------------------+ +-------------------+
| | | |
| Task 1 |<----->| Thread Pool |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Task 2 |<----->| Worker Threads |
| | | |
+-------------------+ +-------------------+
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);

for (int i = 0; i < 10; i++) {
executor.submit(() -> {
System.out.println("Thread " + Thread.currentThread().getName() + " is running");
});
}

executor.shutdown();
}
}

Custom Thread Pools​

Custom thread pools can be created using ThreadPoolExecutor.

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);

for (int i = 0; i < 10; i++) {
executor.submit(() -> {
System.out.println("Thread " + Thread.currentThread().getName() + " is running");
});
}

executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Deadlock Prevention​

Lock Ordering​

Ensure that locks are acquired in a consistent order to prevent deadlocks.

+-------------------+ +-------------------+
| | | |
| Thread 1 |<----->| Resource A |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Thread 2 |<----->| Resource B |
| | | |
+-------------------+ +-------------------+
public class Main {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();

Thread t1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (lock2) {
System.out.println("Thread 1: Holding lock 1 & 2...");
}
}
});

Thread t2 = new Thread(() -> {
synchronized (lock1) { // Acquire locks in the same order
System.out.println("Thread 2: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 2...");
synchronized (lock2) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
});

t1.start();
t2.start();
}
}

Timeout Mechanisms​

Use timeouts to avoid waiting indefinitely for a lock.

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
public static void main(String[] args) {
Lock lock1 = new ReentrantLock();
Lock lock2 = new ReentrantLock();

Thread t1 = new Thread(() -> {
try {
if (lock1.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
if (lock2.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 1: Holding lock 1 & 2...");
lock2.unlock();
}
lock1.unlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});

Thread t2 = new Thread(() -> {
try {
if (lock1.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 2: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
if (lock2.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 2: Holding lock 1 & 2...");
lock2.unlock();
}
lock1.unlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});

t1.start();
t2.start();
}
}

Concurrency Utilities​

Use Concurrent Collections​

Concurrent collections provide thread-safe data structures.

import java.util.concurrent.ConcurrentHashMap;

public class Main {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
map.put("Key" + i, i);
}
});

Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
map.put("Key" + i, i);
}
});

t1.start();
t2.start();

try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Map size: " + map.size()); // Map size: 1000
}
}

Fork/Join Framework​

The Fork/Join framework is designed for tasks that can be recursively split into smaller tasks.

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

class SumTask extends RecursiveTask<Integer> {
private static final int THRESHOLD = 10;
private int[] array;
private int start;
private int end;

public SumTask(int[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}

@Override
protected Integer compute() {
if (end - start <= THRESHOLD) {
int sum = 0;
for (int i = start; i < end; i++) {
sum += array[i];
}
return sum;
} else {
int mid = (start + end) / 2;
SumTask leftTask = new SumTask(array, start, mid);
SumTask rightTask = new SumTask(array, mid, end);
leftTask.fork();
int rightResult = rightTask.compute();
int leftResult = leftTask.join();
return leftResult + rightResult;
}
}
}

public class Main {
public static void main(String[] args) {
int[] array = new int[100];
for (int i = 0; i < array.length; i++) {
array[i] = i + 1;
}

ForkJoinPool pool = new ForkJoinPool();
int sum = pool.invoke(new SumTask(array, 0, array.length));
System.out.println("Sum: " + sum); // Sum: 5050
}
}

Thread Local Variables​

ThreadLocal variables allow each thread to have its own copy of a variable.

public class Main {
private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

public static void main(String[] args) {
Thread t1 = new Thread(() -> {
threadLocal.set(1);
System.out.println("Thread 1: " + threadLocal.get());
});

Thread t2 = new Thread(() -> {
threadLocal.set(2);
System.out.println("Thread 2: " + threadLocal.get());
});

t1.start();
t2.start();
}
}

Example: Concurrent Web Server​

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class RequestHandler implements Runnable {
private Socket clientSocket;

public RequestHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}

@Override
public void run() {
try {
// Handle the client request
System.out.println("Handling request from " + clientSocket.getInetAddress());
// Simulate processing time
Thread.sleep(1000);
clientSocket.getOutputStream().write("HTTP/1.1 200 OK\r\n\r\nHello, World!".getBytes());
clientSocket.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

public class WebServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
ExecutorService executor = Executors.newFixedThreadPool(10);

while (true) {
Socket clientSocket = serverSocket.accept();
executor.submit(new RequestHandler(clientSocket));
}
}
}

Conclusion​

Java's multi-threading capabilities are powerful, but they require careful handling to avoid common pitfalls such as race conditions, deadlocks, and excessive synchronization. By following the best practices outlined in this article, you can write robust, high-performance Java applications that leverage the full potential of multi-threading. The provided real-life production sample code illustrates how these concepts can be applied in practical scenarios.


References​

  1. Java Concurrency in Practice
  2. Oracle Java Documentation
  3. Java Memory Model
  4. Fork/Join Framework
  5. Concurrent Collections

By mastering these concepts and practices, you can build robust, high-performance Java applications that leverage the full potential of multi-threading.