枚举类的使用:
package Juc;
import lombok.Getter;
public enum CountryEnum {
ONE(1,"齐国"),TWO(2,"楚国"),THREE(3,"燕国"),FOUR(4,"赵国"),FIVE(5,"魏国"),SIX(6,"韩国");
@Getter private Integer retCode;
@Getter private String retMessage;
public Integer getRetCode() {
return retCode;
}
public String getRetMessage() {
return retMessage;
}
CountryEnum(Integer retCode,String retMessage){
this.retCode=retCode;
this.retMessage=retMessage;
}
public static CountryEnum forEach_CountryEnum(int index){
CountryEnum[] MyEnum = CountryEnum.values();
for (CountryEnum element : MyEnum) {
if(index==element.getRetCode()){
return element;
}
}
return null;
}
}
CountDownLatch 计数器—>做减法
package Juc;
import java.util.concurrent.CountDownLatch;
class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 1; i <=6; i++) {
new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"灭亡!。");
countDownLatch.countDown();
},CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();
}
// while(Thread.activeCount()>2){ //默认2个线程,main线程和Gc线程。
// Thread.yield(); //礼让线程
// }
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"\t 最后秦国一统天下。");
}
}
做加法
package Juc;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier cyc = new CyclicBarrier(7,()-> {System.out.println("召唤神龙!");});
for (int i = 1; i <= 7; i++) {
final int temp=i;
new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"\t 收集到第"+ temp+"龙珠。");
try {
cyc.await();
} catch (Exception e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}