不同监视对象在同步中的目的
创始人
2025-01-09 04:30:14
0

在同步中,不同的监视对象有不同的目的。以下是一些常见的使用场景及相应的代码示例:

  1. 同步线程间共享资源:使用相同的监视对象来同步多个线程访问共享资源,确保线程安全。
class SharedResource {
    private int count = 0;

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

public class Main {
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();

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

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

        thread1.start();
        thread2.start();

        // 等待两个线程执行完毕
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Count: " + resource.getCount());
    }
}
  1. 避免死锁:使用不同的监视对象来避免可能导致死锁的情况。
class Resource1 {
    public synchronized void method1(Resource2 resource2) {
        System.out.println("Resource1.method1");

        // 调用Resource2的方法
        resource2.method2();
    }

    public synchronized void method2() {
        System.out.println("Resource1.method2");
    }
}

class Resource2 {
    public synchronized void method1(Resource1 resource1) {
        System.out.println("Resource2.method1");

        // 调用Resource1的方法
        resource1.method2();
    }

    public synchronized void method2() {
        System.out.println("Resource2.method2");
    }
}

public class Main {
    public static void main(String[] args) {
        Resource1 resource1 = new Resource1();
        Resource2 resource2 = new Resource2();

        Thread thread1 = new Thread(() -> {
            resource1.method1(resource2);
        });

        Thread thread2 = new Thread(() -> {
            resource2.method1(resource1);
        });

        thread1.start();
        thread2.start();

        // 等待两个线程执行完毕
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  1. 并发容器的同步:使用并发容器时,可以使用容器自带的监视对象来同步对容器的访问。
import java.util.concurrent.CopyOnWriteArrayList;

public class Main {
    public static void main(String[] args) {
        CopyOnWriteArrayList list = new CopyOnWriteArrayList<>();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add("Thread 1");
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                list.add("Thread 2");
            }
        });

        thread1.start();
        thread2.start();

        // 等待两个线程执行完毕
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("List size: " + list.size());
    }
}

这些示例展示了不同监视对象在同步中的目的和用法。根据具体的场景和需求,可以选择适合的同步方式和监视对象。

相关内容

热门资讯

外挂烘培!德普之星怎么设置埋牌... 外挂烘培!德普之星怎么设置埋牌,拱趴大菠萝万能挂图解-好像一直都是有辅助神器(哔哩哔哩)1、拱趴大菠...
外挂举措!德州机器人代打脚本,... 外挂举措!德州机器人代打脚本,智星菠萝辅助-其实是真的有辅助app(哔哩哔哩)1、起透看视 德州机器...
外挂学习!智星菠萝可以辅助吗,... 外挂学习!智星菠萝可以辅助吗,德州透视是真的假的-竟然真的是有辅助工具(哔哩哔哩)1、让任何用户在无...
外挂演示!约局吧作必弊脚本,约... 外挂演示!约局吧作必弊脚本,约局吧怎么看有没有挂-真是是有辅助攻略(哔哩哔哩)1、约局吧怎么看有没有...
外挂阶段!德州透视是真的吗,p... 外挂阶段!德州透视是真的吗,pokermaster破解版-原来真的是有辅助攻略(哔哩哔哩)进入游戏-...
外挂模块!约局吧德州有挂吗,来... 外挂模块!约局吧德州有挂吗,来玩app 德州 辅助-果然是真的有辅助脚本(哔哩哔哩)1、下载好约局吧...
外挂手筋!德扑圈透视,智星德州... 外挂手筋!德扑圈透视,智星德州插件2024最新版-竟然真的是有辅助神器(哔哩哔哩)智星德州插件202...
外挂手段!wepoker透视有... 外挂手段!wepoker透视有吗,德普之星透视-果然真的有辅助方法(哔哩哔哩)1、完成wepoker...
外挂教程书!拱趴大菠萝挂怎么安... 外挂教程书!拱趴大菠萝挂怎么安装,竞技联盟破解版最新版-切实有辅助攻略(哔哩哔哩)1、拱趴大菠萝挂怎...
外挂步骤!哈糖大菠萝可以开挂吗... 外挂步骤!哈糖大菠萝可以开挂吗,德扑圈有透视吗-本来真的有辅助攻略(哔哩哔哩)亲,关键说明,哈糖大菠...