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

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

相关内容

热门资讯

透视app!wepoker怎么... 透视app!wepoker怎么破解游戏(透视)一贯是真的有挂(详细辅助透牌教程);该软件可以轻松地帮...
必备科技!wepoker私人局... 必备科技!wepoker私人局透视插件,菠萝德州透视脚本,教你攻略(有挂透视)1、不需要AI权限,帮...
透视最新!有没有人wepoke... 透视最新!有没有人wepoker(透视)一直存在有挂(详细辅助细节揭秘);1、完成透视辅助安装,帮助...
重大通报!pokemmo脚本,... 重大通报!pokemmo脚本,hhpoker辅助器,解密教程(有挂透视);最新版2024是一款经典耐...
透视科技!wepoker透视脚... 透视科技!wepoker透视脚本是什么(透视)竟然有挂(详细辅助2025教程)1、完成wepoker...
终于清楚!wepoker免费脚... 终于清楚!wepoker免费脚本弱密码,wepokerplus作弊,靠谱教程(有挂攻略)1、不需要A...
透视ai代打!wepoker有... 透视ai代打!wepoker有没有挂(透视)总是是真的有挂(详细辅助力荐教程);1、玩家可以在软件透...
透视免费!来玩app 德州 辅... 透视免费!来玩app 德州 辅助(透视)原来是真的有挂(详细辅助介绍教程)1、游戏颠覆性的策略玩法,...
揭秘关于!哈糖大菠萝怎么挂,w... 揭秘关于!哈糖大菠萝怎么挂,werplan有挂吗,揭秘攻略(有挂攻略);玩家必备必赢加哟《13670...
透视透视!pokernow辅助... 透视透视!pokernow辅助控制(透视)其实真的有挂(详细辅助实用技巧)1、完成透视辅助安装,帮助...