题解 | #多线程#

多线程

http://www.nowcoder.com/practice/cd99fbc6154d4074b4da0e74224a1582

import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Main {

    private volatile int count = 1;
    private Lock lock = new ReentrantLock();
    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();
    private Condition conditionC = lock.newCondition();
    private Condition conditionD = lock.newCondition();

    // 主要:所有的都要用 while
    public void addA() {
        lock.lock();
        try {
            // 如果当前不是 1 ,则要阻塞等待
            while (count != 1) {
                conditionA.await();
            }
            count++;
            System.out.print("A");
            conditionB.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void addB() {
        lock.lock();
        try {
            // 如果当前不是 2 ,就要阻塞等待
            while (count != 2) {
                conditionB.await();
            }
            count++;
            System.out.print("B");
            conditionC.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void addC() {
        lock.lock();
        try {
            // 如果当前不是 3,则阻塞等待
            while (count != 3) {
                conditionC.await();
            }
            count++;
            System.out.print("C");
            conditionD.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void addD() {
        lock.lock();
        try {
            // 当前不是 4 ,则阻塞等待
            while (count != 4) {
                conditionD.await();
            }
            System.out.print("D");
            count = 1;
            conditionA.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public static void main(String[] args) throws InterruptedException {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            Main hj = new Main();
            CountDownLatch countDownLatch = new CountDownLatch(4);

            // 线程1
            new Thread(() -> {
                // A 线程负责add 10 次
                for (int i = 0; i < n; i++) {
                    hj.addA();
                }
                countDownLatch.countDown();
            }, "A").start();

            // 线程2
            new Thread(() -> {
                // A 线程负责add 10 次
                for (int i = 0; i < n; i++) {
                    hj.addB();
                }
                countDownLatch.countDown();
            }, "B").start();

            // 线程3
            new Thread(() -> {
                // A 线程负责add 10 次
                for (int i = 0; i < n; i++) {
                    hj.addC();
                }
                countDownLatch.countDown();
            }, "C").start();

            // 线程4
            new Thread(() -> {
                // A 线程负责add 10 次
                for (int i = 0; i < n; i++) {
                    hj.addD();
                }
                countDownLatch.countDown();
                System.out.println();
            }, "D").start();
            countDownLatch.await();
        }
    }


}

 
 

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务