多线程【一】线程自定义顺序执行

第一题:编写代码,使用3个线程,1个线程打印A,一个线程打印B,一个线程打印C,同时执行连续打印n次"ABC";

 

第二题:编写代码,使用m个线程,M小于62,str[m] 为62进制对应的值,第1个线程打印str[1],第二个线程打印str[2],第三个线程打印str[3],第m个线程打印str[m],同时执行连续打印n次"str[1]至str[m]"的值;

 

第一题:


import lombok.SneakyThrows;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Multithread {
    private ReentrantLock lock = new ReentrantLock();
    private Condition ca = lock.newCondition();
    private Condition cb = lock.newCondition();
    private Condition cc = lock.newCondition();

    private int current = 1;

    public Multithread() {
    }

    public void printA() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            lock.lock();
            if (current != 1) {
                ca.await();
            }
            System.out.print("A");
            current = 2;
            cb.signal();
            lock.unlock();
        }
    }

    public void printB() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            lock.lock();
            if (current != 2) {
                cb.await();
            }
            System.out.print("B");
            current = 3;
            cc.signal();
            lock.unlock();
        }
    }

    public void printC() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            lock.lock();
            if (current != 3) {
                cc.await();
            }
            System.out.print("C");
            System.out.println("");
            current = 1;
            ca.signal();
            lock.unlock();
        }
    }

    public static void main(String[] args) {

        //lock.lock();
        final Multithread exam = new Multithread();
        Thread printA = new Thread() {
            @SneakyThrows
            public void run() {
                exam.printA();
            }
        };

        Thread printB = new Thread() {
            @SneakyThrows
            public void run() {
                exam.printB();
            }
        };

        Thread printC = new Thread() {
            @SneakyThrows
            public void run() {
                exam.printC();
            }
        };

        printA.start();
        printB.start();
        printC.start();
    }

}

第二题:


import lombok.SneakyThrows;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class MultithreadN {
    private ReentrantLock lock = new ReentrantLock();
    private Condition[] conditions;

    private static final char[] DIGITS_BASE = {
            '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x',
            'y', 'z', '[', ']'
    };

    private int current = 0;
    private int amount = 6;

    private int printAmount = 20;

    public MultithreadN(int amount, int printAmount) {
        this.amount = amount;
        this.printAmount = printAmount;
        conditions = new Condition[amount];
        for (int i = 0; i < amount; i++) {
            conditions[i] = lock.newCondition();
        }
    }

    public void print(int index) throws InterruptedException {
        for (int i = 0; i < printAmount; i++) {
            lock.lock();
            if (current != index) {
                conditions[index].await();
            }
            System.out.print(DIGITS_BASE[index]);
            current = (current + 1) % amount;
            if (current == 0) {
                System.out.println();
            }

            conditions[current].signal();
            lock.unlock();
        }
    }

    public void run() {
        for (int i = 0; i < this.amount; i++) {
            int finalI = i;
            Thread print = new Thread() {
                @SneakyThrows
                public void run() {
                    print(finalI);
                }
            };
            print.start();
        }
    }

    public static void main(String[] args) {

        final MultithreadN exam = new MultithreadN(16, 30);

        exam.run();
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值