1. synchronized 关键字
示例代码
收起
java
class Counter {
    private int count = 0;
    // 同步方法
    public synchronized void increment() {
        count++;
    }
    public int getCount() {
        return count;
    }
}
public class SynchronizedExample {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        // 创建两个线程来增加计数器的值
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });
        thread1.start();
        thread2.start();
        // 等待两个线程执行完毕
        thread1.join();
        thread2.join();
        // 输出最终的计数器值
        System.out.println("Final count: " + counter.getCount());
    }
}代码解释
2. ReentrantLock
示例代码
收起
java
import java.util.concurrent.locks.ReentrantLock;
class CounterWithLock {
    private int count = 0;
    private final ReentrantLock lock = new ReentrantLock();
    public void increment() {
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }
    public int getCount() {
        return count;
    }
}
public class ReentrantLockExample {
    public static void main(String[] args) throws InterruptedException {
        CounterWithLock counter = new CounterWithLock();
        // 创建两个线程来增加计数器的值
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                counter.increment();
            }
        });
        thread1.start();
        thread2.start();
        // 等待两个线程执行完毕
        thread1.join();
        thread2.join();
        // 输出最终的计数器值
        System.out.println("Final count: " + counter.getCount());
    }
}代码解释
3. ReadWriteLock
示例代码
收起
java
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class Data {
    private int value;
    private final ReadWriteLock lock = new ReentrantReadWriteLock();
    public int read() {
        lock.readLock().lock();
        try {
            return value;
        } finally {
            lock.readLock().unlock();
        }
    }
    public void write(int newValue) {
        lock.writeLock().lock();
        try {
            value = newValue;
        } finally {
            lock.writeLock().unlock();
        }
    }
}
public class ReadWriteLockExample {
    public static void main(String[] args) {
        Data data = new Data();
        // 读线程
        Thread readThread1 = new Thread(() -> {
            System.out.println("Read value: " + data.read());
        });
        Thread readThread2 = new Thread(() -> {
            System.out.println("Read value: " + data.read());
        });
        // 写线程
        Thread writeThread = new Thread(() -> {
            data.write(100);
            System.out.println("Write operation completed.");
        });
        readThread1.start();
        readThread2.start();
        writeThread.start();
    }
} 
 
                     
            
        













 
                    

 
                 
                    