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

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

相关内容

热门资讯

透视黑科技!wpk控制牌是真的... 透视黑科技!wpk控制牌是真的吗,wpk有作弊吗(透视)原先真的有挂(详细教程)1、每一步都需要思考...
透视肯定!约局吧辅助器,pok... 透视肯定!约局吧辅助器,pokemmo脚本最新版,切实真的是有挂(可靠教程)1、pokemmo脚本最...
透视辅助!佛手大菠萝有挂吗,w... 透视辅助!佛手大菠萝有挂吗,wpk辅助器(透视)果然真的是有挂(2025新版教程)小薇(透视辅助)致...
透视线上!pokermaste... 透视线上!pokermaster脚本,来玩app 德州 辅助,都是存在有挂(新2025教程)1、透视...
透视app!如何下载wepok... 透视app!如何下载wepoker安装包,wepoker有没有插件(透视)往昔存在有挂(曝光教程)1...
透视教程!菠萝辅助器免费版的特... 透视教程!菠萝辅助器免费版的特点,德州圈脚本,切实真的有挂(揭秘攻略);1、菠萝辅助器免费版的特点a...
透视挂透视!wepoker透视... 透视挂透视!wepoker透视器免费,wepoker好友房开挂(透视)往昔有挂(AI教程)1、wep...
透视模拟器!pokerrrr2... 透视模拟器!pokerrrr2辅助,newpoker怎么安装脚本,确实是真的有挂(总结教程)1、全新...
透视私人局!扑克之星辅助,德州... 透视私人局!扑克之星辅助,德州hhpoker脚本(透视)确实真的有挂(攻略教程)1、在德州hhpok...
透视计算!pokermaste... 透视计算!pokermaster修改器,拱趴大菠萝挂哪里,一直存在有挂(分享教程);1、完成拱趴大菠...