一个容器,两个方法add,size。起两个线程,线程一添加10个元素到容器中,线程二监控容器元素个数,5个时线程二给出提示并结束


在这里插入图片描述

一、sync的wait和notify

public class T04 {

    private static List lists = new ArrayList();

    private static void add(Object o) {
        lists.add(o);
    }

    private static int size() {
        return lists.size();
    }

    public static void main(String[] args) {
        Object lock = new Object();

        new Thread(() -> {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t2 结束");
                lock.notify();
            }
        }, "t2").start();

        new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 10; i++) {
                    add(new Object());
                    System.out.println("add: " + i);

                    if (size() == 5) {
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "t1").start();
    }

}

二、CountDownLatch

public class T05 {
    private static List lists = new ArrayList();

    private static void add(Object o) {
        lists.add(o);
    }

    private static int size() {
        return lists.size();
    }

    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);

        new Thread(()->{
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2 结束");
            latch2.countDown();
        }, "t2").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                add(new Object());
                System.out.println("add: " + i);

                if (size() == 5) {
                    latch.countDown();
                    try {
                        latch2.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t1").start();
    }

}

三、LockSupport

public class T06 {
    private static List lists = new ArrayList();

    private static void add(Object o) {
        lists.add(o);
    }

    private static int size() {
        return lists.size();
    }

    private static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        t2 = new Thread(() -> {
            LockSupport.park();
            System.out.println("t2 结束");
            LockSupport.unpark(t1);
        }, "t2");

        t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                add(new Object());
                System.out.println("add: " + i);

                if (size() == 5) {
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
        }, "t1");

        t2.start();
        t1.start();
    }

}
在Java,可以通过使用两个线程来同调用同一个方法,并遍历出字符串的不同字符。下面是一个示例代码,演示了如何实现这个功能: ```java import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { String str = "Hello, World!"; Set<Character> uniqueChars = new HashSet<>(); Thread t1 = new Thread(() -> { for (char c : str.toCharArray()) { synchronized (uniqueChars) { if (!uniqueChars.contains(c)) { uniqueChars.add(c); System.out.println("Thread 1: Found unique character: " + c); } } } }); Thread t2 = new Thread(() -> { for (char c : str.toCharArray()) { synchronized (uniqueChars) { if (!uniqueChars.contains(c)) { uniqueChars.add(c); System.out.println("Thread 2: Found unique character: " + c); } } } }); t1.start(); t2.start(); } } ``` 在这个例子,我们创建了两个线程(t1和t2),它们同遍历给定的字符串(str)。每个线程都使用一个`Set`(uniqueChars)来存储已经遇到的字符。 在遍历过程,我们使用`synchronized`关键字来确保两个线程不会同修改`uniqueChars`集合。这样可以防止竞态条件(race condition)和数据不一致的问题。 每当一个线程发现一个不同的字符,它会将它添加到`uniqueChars`集合,并打印出相应的消息。 请注意,由于线程的执行顺序是不确定的,因此输出的顺序可能会有所不同。但是,最终的结果应该是包含了字符串所有不同字符的集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值