Java 进程加锁 在多线程编程中,锁是一种重要的同步机制,用于保护共享资源免受并发访问的影响。在 Java 中,可以使用锁来控制对共享资源的访问,并确保多个线程之间的互斥性。
          Java 进程加锁
在多线程编程中,锁是一种重要的同步机制,用于保护共享资源免受并发访问的影响。在 Java 中,可以使用锁来控制对共享资源的访问,并确保多个线程之间的互斥性。
Java 提供了多种锁机制,包括 synchronized 关键字、ReentrantLock 类等。这些锁机制都可以用于进程内的线程间同步,确保线程之间的互斥操作。
synchronized 关键字
synchronized 是 Java 中最基本的锁机制,它可以用来修饰方法、代码块或对象,确保同一时刻只有一个线程可以访问被修饰的资源。
1. 修饰方法
可以使用 synchronized 关键字修饰方法,以确保在同一时间只有一个线程可以执行该方法。
public class SynchronizedMethodExample {
    private int count = 0;
    public synchronized void increment() {
        count++;
    }
    public synchronized int getCount() {
        return count;
    }
    public static void main(String[] args) {
        SynchronizedMethodExample example = new SynchronizedMethodExample();
        // 创建多个线程,同时调用 increment 方法
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                example.increment();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                example.increment();
            }
        });
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Count: " + example.getCount()); // 输出结果为 2000
    }
}
在上面的示例中,increment 和 getCount 方法都被 synchronized 关键字修饰,确保了对 count 属性的访问是互斥的。当多个线程同时调用 increment 方法时,只有一个线程可以进入该方法,其他线程需要等待。
2. 修饰代码块
除了修饰方法,synchronized 关键字还可以修饰代码块。通过指定一个对象作为锁,可以确保在同一时间只有一个线程可以进入被修饰的代码块。
public class SynchronizedBlockExample {
    private int count = 0;
    private final Object lock = new Object();
    public void increment() {
        synchronized (lock) {
            count++;
        }
    }
    public int getCount() {
        return count;
    }
    public static void main(String[] args) {
        SynchronizedBlockExample example = new SynchronizedBlockExample();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                example.increment();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                example.increment();
            }
        });
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Count: " + example.getCount()); // 输出结果为 2000
    }
}
在上面的示例中,我们创建了一个 lock 对象作为锁,在 increment 方法中使用 synchronized 关键字修饰代码块,并传入 lock 对象作为锁。这样在同一时间只有一个线程可以进入 increment 方法。
ReentrantLock 类
除了 synchronized 关键字,Java 还提供了更灵活的锁机制,即 ReentrantLock 类。ReentrantLock 类实现了 Lock 接口,提供了更多的功能,如可重入性、公平锁、条件变量等。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockExample {
    private int count = 0;
    private Lock lock = new ReentrantLock();
    public void increment() {
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }
    public int getCount() {
        return count;
    }
    public static void main(String[] args) {
        ReentrantLockExample example = new ReentrantLockExample();
        Thread t1 = new Thread(() -> {
            for (int i = 0;