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

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

相关内容

热门资讯

曝光透视!红龙poker有辅助... 曝光透视!红龙poker有辅助吗!竟然真的是有辅助软件(有挂分析)-哔哩哔哩1、完成红龙poker有...
辅助透视!hhpoker脚本下... 辅助透视!hhpoker脚本下载,wepoker辅助器激活码,妙招教程(有挂教学)-哔哩哔哩1、下载...
曝光透视!sohoo poke... 曝光透视!sohoo poker辅助!竟然一直都是有辅助脚本(有挂存在)-哔哩哔哩1、全新机制【so...
总结透视!fishpoker透... 总结透视!fishpoker透视底牌,wepoker有辅助器吗,总结教程(有挂教程)-哔哩哔哩1、游...
解谜透视!哈糖大菠萝可以开挂吗... 解谜透视!哈糖大菠萝可以开挂吗!本来是真的有辅助技巧(新版有挂)-哔哩哔哩在进入哈糖大菠萝可以开挂吗...
了解透视!wepoker透视有... 了解透视!wepoker透视有没有,wejoker辅助软件视频,妙计教程(有挂秘笈)-哔哩哔哩1、该...
普及透视!pokerworld... 普及透视!pokerworld软件!其实一直总是有辅助攻略(有挂秘籍)-哔哩哔哩该软件可以轻松地帮助...
解迷透视!wepoker可以透... 解迷透视!wepoker可以透视码,pokemmo辅助官网,法门教程(有挂秘笈)-哔哩哔哩1、许多玩...
科普透视!newpoker脚本... 科普透视!newpoker脚本!切实真的是有辅助神器(有挂教程)-哔哩哔哩newpoker脚本能透视...
辅助透视!智星德州插件2024... 辅助透视!智星德州插件2024最新版,hhpoker辅助靠谱吗,课程教程(证实有挂)-哔哩哔哩1、玩...