【Java多线程】4 线程同步


第四部分:线程同步

在多线程环境中,多个线程可能会同时访问共享资源,这会导致数据不一致和其他并发问题。为了保证数据的正确性和一致性,Java 提供了多种同步机制来控制线程对共享资源的访问。

一、共享资源和竞争条件

  • 共享资源:指多个线程可能同时访问的变量、对象或数据结构。
  • 竞争条件:当两个或多个线程在没有适当同步的情况下同时访问共享资源时,可能会导致数据不一致的问题。

二、同步方法和同步块

2.1 同步方法

在方法上使用 synchronized 关键字可以确保同一时刻只有一个线程能执行该方法。synchronized 可以用于实例方法或静态方法。

示例代码:

class Counter {
    private int count = 0;

    // 同步实例方法
    public synchronized void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

public class SyncMethodExample {
    public static void main(String[] args) {
        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();
        
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("最终计数器值:" + counter.getCount()); // 应该是2000
    }
}
2.2 同步块

同步块提供更细粒度的控制,可以选择在特定的代码块中使用 synchronized,而不是同步整个方法。使用 synchronized 块时,可以指定锁对象。

示例代码:

class Counter {
    private int count = 0;

    public void increment() {
        // 使用同步块
        synchronized (this) {
            count++;
        }
    }

    public int getCount() {
        return count;
    }
}

三、静态同步方法

如果将 synchronized 用于静态方法,锁定的是类的 Class 对象。这意味着该类的所有实例共享同一把锁。

示例代码:

class StaticCounter {
    private static int count = 0;

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

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

四、锁对象

可以使用任意对象作为锁。多个线程使用同一个对象作为锁时,访问该对象的同步代码块将会互斥。

示例代码:

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

    public void increment() {
        synchronized (lock) { // 使用 lock 对象作为锁
            count++;
        }
    }

    public int getCount() {
        return count;
    }
}

五、死锁

死锁指两个或多个线程在等待彼此持有的锁,导致无法继续执行的情况。防止死锁的一些策略包括:

  • 资源分配顺序:确保所有线程以相同的顺序获取锁。
  • 使用 tryLock():使用 Lock 类的 tryLock() 方法尝试获取锁并设置超时。
  • 避免嵌套锁:尽量减少在持有一个锁的情况下再去获取另一个锁。

示例代码

class A {
    synchronized void methodA(B b) {
        System.out.println("线程1:持有A锁...");
        try { Thread.sleep(100); } catch (InterruptedException e) {}
        System.out.println("线程1:等待B锁...");
        b.last();
    }

    synchronized void last() {
        System.out.println("A内部的last方法");
    }
}

class B {
    synchronized void methodB(A a) {
        System.out.println("线程2:持有B锁...");
        try { Thread.sleep(100); } catch (InterruptedException e) {}
        System.out.println("线程2:等待A锁...");
        a.last();
    }

    synchronized void last() {
        System.out.println("B内部的last方法");
    }
}

// 产生死锁的示例
public class DeadlockExample {
    public static void main(String[] args) {
        final A a = new A();
        final B b = new B();

        new Thread(() -> a.methodA(b)).start();
        new Thread(() -> b.methodB(a)).start();
    }
}

总结

通过同步方法和同步块,我们可以有效控制线程对共享资源的访问,从而避免数据不一致的问题。在多线程编程中,死锁是一个常见问题,了解死锁的原理和防止措施也非常重要。

下一步,我们将学习 Java 中的并发工具,包括 Lock 接口、SemaphoreCountDownLatchCyclicBarrier 等,并发工具的使用方法和适用场景。

点我跳转下一课^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天呐少爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值