不同监视对象在同步中的目的
创始人
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有作弊吗,wp... 透视详情!wpk有作弊吗,wpk刷入池率脚本-确实有辅助插件(哔哩哔哩)1、任何透视是真的假的的玩家...
透视辅助!如何下载wpk透视版... 透视辅助!如何下载wpk透视版,wpk刷入池率脚本-一直真的有辅助软件(哔哩哔哩)1、下载好正确养号...
透视开挂!hhpoker有没有... 透视开挂!hhpoker有没有辅助,德州hhpoker脚本-一贯一直都是有辅助攻略(哔哩哔哩)1、在...
透视开挂!wpk模拟器,wpk... 透视开挂!wpk模拟器,wpk软件是正规的吗-都是一直都是有辅助教程(哔哩哔哩)1、不需要AI权限,...
透视详细!wpk显示有作弊,w... 透视详细!wpk显示有作弊,wpk软件是正规的吗-确实真的是有辅助工具(哔哩哔哩)破解侠是真的助透视...
透视关于!wpk系统是否存在作... 透视关于!wpk系统是否存在作弊行为,wpk透视是真的假的-都是有辅助技巧(哔哩哔哩)透视关于!wp...
透视揭幕!wpk透视插件,wp... 透视揭幕!wpk透视插件,wpk显示有作弊-切实真的是有辅助攻略(哔哩哔哩)1、下载好透视辅助下载之...
透视解密!hhpoker有辅助... 透视解密!hhpoker有辅助吗,hhpoker脚本下载-都是真的有辅助神器(哔哩哔哩)1、每一步都...
透视解谜!we poker辅助... 透视解谜!we poker辅助器下载,wejoker透视方法-果然是有辅助神器(哔哩哔哩)1、破解器...
透视曝光!wepoker高级辅... 透视曝光!wepoker高级辅助,wepoker辅助是真的吗-原来是有辅助软件(哔哩哔哩)1、脚本辅...