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

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

相关内容

热门资讯

详细透视!we-poker靠谱... 详细透视!we-poker靠谱吗,微乐陕西三代自建房挂,绝活儿教程(有挂秘诀)-哔哩哔哩1、每一步都...
总结透视!xpoker辅助控制... 总结透视!xpoker辅助控制,贵州微乐自建房辅助软件下载,法子教程(竟然有挂)-哔哩哔哩1、玩家可...
详情透视!wepoker手机版... 详情透视!wepoker手机版辅助,手机微信小程序游戏破解器下载,窍门教程(的确有挂)-哔哩哔哩1、...
辅助透视!德普之星辅助工具如何... 辅助透视!德普之星辅助工具如何打开,微信小程序微乐房间有挂吗,指引教程(有挂方法)-哔哩哔哩小薇(辅...
详情透视!wepoker免费脚... 详情透视!wepoker免费脚本,微信小程序游戏破解微乐游戏,方案教程(有挂详情)-哔哩哔哩1、上手...
专业透视!wpk插件辅助,微乐... 专业透视!wpk插件辅助,微乐广西小程序,资料教程(证实有挂)-哔哩哔哩1、操作简单,无需wpk插件...
分享透视!wpk透视是真的假的... 分享透视!wpk透视是真的假的,微信小程序游戏破解器,阶段教程(有挂秘籍)-哔哩哔哩一、wpk透视是...
曝光透视!如何下载德普之星辅助... 曝光透视!如何下载德普之星辅助软件,微信小程序修改器,举措教程(有挂教程)-哔哩哔哩进入游戏-大厅左...
关于透视!hhpoker德州牛... 关于透视!hhpoker德州牛仔视频,微信小程序微乐辅助器免费下载,经验教程(讲解有挂)-哔哩哔哩1...
解谜透视!德州局透视脚本,微信... 解谜透视!德州局透视脚本,微信小程序微乐破解器2025,举措教程(有挂教程)-哔哩哔哩1、德州局透视...