不同监视对象在同步中的目的
创始人
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());
    }
}

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

相关内容

热门资讯

曝光辅助!开心泉州免费辅助器,... 曝光辅助!开心泉州免费辅助器,新广西老友辅助(太夸张了辅助开挂软件);相信小伙伴都知道这个开心泉州免...
原来有透视"给大家普... 原来有透视"给大家普及一下wepoker开挂透视方法"开挂(透视)辅助平台(我来教大家必赢方法)原来...
原来有开挂!欢乐达人辅助工具,... 原来有开挂!欢乐达人辅助工具,牛魔王辅助器(太嚣张了开挂辅助工具);欢乐达人辅助工具是一项欢乐达人辅...
必看透视"wpk辅助... 必看透视"wpk辅助kaq665"开挂(透视)辅助下载(一分钟了解黑科技教程)必看透视"wpk辅助k...
正品开挂!随意玩正版透视,随意... 正品开挂!随意玩正版透视,随意玩辅助器视频(太实锤了辅助开挂工具);亲,有的,ai轻松简单,又可以获...
正版透视"we po... 正版透视"we poker透视"开挂(透视)辅助下载(我来教大家必赢方法)正版透视"we poker...
了解开挂!甘肃白银麻将辅助软件... 了解开挂!甘肃白银麻将辅助软件,辽宁心悦游戏辅助器(太嚣张了开挂辅助软件);甘肃白银麻将辅助软件中的...
推荐透视"微扑克发牌... 推荐透视"微扑克发牌真的很假"开挂(透视)辅助软件(揭秘线上教程)【福星临门,好运相随】;推荐透视"...
分析开挂!微乐透视辅助功能插件... 分析开挂!微乐透视辅助功能插件下载,微乐陕西小程序破解版下载(太坑了辅助开挂平台);小薇(透视辅助)...
详细透视"德州局we... 详细透视"德州局wepoker辅助挂安全吗"开挂(透视)辅助神器(盘点十款微扑克教程)《详细透视"德...