第一题:编写代码,使用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();
}
}