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

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

相关内容

热门资讯

开挂辅助工具"wep... 开挂辅助工具"wepokerplus辅助"开挂(透视)辅助平台(详细教程);打开点击测试直接进入微信...
开挂辅助软件"德州圈... 大家好,今天小编来为大家解答德州圈脚本这个问题咨询软件客服可以免费测试直接加微信(136704302...
开挂辅助平台"wep... 【亲,wepoker破解游戏盒子 这款游戏可以开挂的,确实是有挂的,很多玩家在这款wepoker破解...
开挂辅助插件"We ... 开挂辅助插件"We poker辅助器下载"开挂(透视)辅助脚本(存在有挂)>>您好:软件加薇1367...
开挂辅助下载"wep... wepoker私人局可以透视是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
开挂辅助软件"wep... 开挂辅助软件"wepoker代打辅助机器人"开挂(透视)辅助脚本(有挂教程)【无需打开直接搜索加薇1...
开挂辅助工具"wep... 开挂辅助工具"wepoker轻量版有透视吗"开挂(透视)辅助神器(真的有挂) 了解更多开挂安装加(1...
开挂辅助插件"we ... 开挂辅助插件"we poker免费辅助器"开挂(透视)辅助神器(今日头条)您好:we poker免费...
开挂辅助工具"hhp... 开挂辅助工具"hhpkoer辅助器视频"开挂(透视)辅助平台(有挂实锤) 【无需打开直接搜索加薇13...
开挂辅助工具"hhp... 开挂辅助工具"hhpoker德州辅助"开挂(透视)辅助平台(有挂猫腻)>>您好:软件加1367043...