Java多线程练习

Java多线程练习

在main方法中使用三个线程,向一个List集合中按顺序添加1到10共十个数字,添加完成后在控制台打印这个List的值(应该是[1,2,3,4,5,6,7,8,9,10]),然后结束程序执行。

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest {
    private int flag = 1;
    private int count = 1;

    private final Lock lock = new ReentrantLock();
    private final Condition conditionA = lock.newCondition();
    private final Condition conditionB = lock.newCondition();
    private final Condition conditionC = lock.newCondition();
    volatile List<Integer> list = new ArrayList<>();

    public void addA() {
        if (count >= 9) return;
        lock.lock();
        try {
            if (flag != 1) {
                conditionA.await();
            }
            list.add(count);
            count++;
            flag = 2;
            // 唤醒B
            conditionB.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void addB() {
        if (count >= 9) return;
        lock.lock();
        try {
            if (flag != 2) {
                conditionB.await();
            }
            list.add(count);
            count++;
            flag = 3;
            // 唤醒C
            conditionC.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public void addC() {
        if (count >= 9) return;
        lock.lock();
        try {
            if (flag != 3) {
                conditionC.await();
            }
            list.add(count);
            count++;
            flag = 1;
            // 唤醒A
            conditionA.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public List<Integer> getList() {
        return list;
    }
}
public class Test3 {

    static Thread t1 ,t2,t3;

    public static void main(String[] args) {
        List<Thread> threadList = new ArrayList<>();
        ThreadTest threadTest = new ThreadTest();
        t1 = new Thread(() -> {
            for (int i = 1; i <= 4; i++) {
                threadTest.addA();
            }
        });

        t2 = new Thread(() -> {
            for (int i = 1; i <= 4; i++) {
                threadTest.addB();
            }
        });

        t3 = new Thread(() -> {
            for (int i = 1; i <= 4; i++) {
                threadTest.addC();
            }
        });
        //把Thread添加到集合
        threadList.add(t1);
        threadList.add(t2);
        threadList.add(t3);
        //启动线程
        t1.start();
        t2.start();
        t3.start();

        for(Thread item : threadList){
            try {
                //等待子线程执行后才执行主线程
                item.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        List<Integer> integers = threadTest.getList();
        Object[] array = integers.toArray();
        System.out.println(Arrays.toString(array));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值