多线程模拟生成武器独一无二标识,限定可以生成的数量,生成同时放到同一list中

多线程模拟限定武器生成唯一标识,限定数量

模拟业务逻辑:多个用户的线程同时获取限定数量的武器,它们的标识(weaponId)都是独一无二的,获取的同时随机生成(不是一开始随机生成固定的,再随机获取,而是获取的时候才生成),生成的同时添加进同一list中,且数量有限,用锁机制达成不能超额获取的效果,若主线程任务执行结束时,获取weaponid的所有子线程必须同时结束

package cn.itsp.threaduse;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

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


    //注意:锁是在同一个对象的上,不然没有意义,上锁的对象只实例化一次
    CreateWeaponId createWeaponId1 = new CreateWeaponId();
    //一个简单的控制开关,输入S关闭所有代理createWeaponId1这个对象的线程
    CreateWeaponIdConsole createWeaponIdConsole = new CreateWeaponIdConsole(createWeaponId1);

    Thread thread2 = new Thread(createWeaponId1);
    Thread thread = new Thread(createWeaponId1);
    Thread thread1 = new Thread(createWeaponId1);

    Thread thread3 = new Thread(createWeaponIdConsole);

    thread.setDaemon(true);//设置为守护线程,主线程任务执行完成不再继续执行,而是同时结束,所有子线程都为守护线程才能成功
    thread1.setDaemon(true);
    thread2.setDaemon(true);
    thread3.setDaemon(true);
    thread.start();
    thread1.start();
    thread2.start();
    thread3.start();


    System.out.println("主线程继续执行" + Thread.currentThread().getName());
    for (int i = 0; i < 60; i++) {
        System.out.println("主线程 i=" + i);
        try {
            Thread.sleep(500);
            //thread.interrupt();//唤醒,中断线程休眠状态
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

//        Thread.yield();//线程礼让,让出CPU,让出的时间不确定,礼让不一定成功,cpu资源紧张时容易成功
//     	 thread1.join();//线程插队,插队一旦成功,先执行完插入线程(子线程)的所有任务

    Thread.sleep(500);

}
}


class CreateWeaponId implements Runnable {

//限定武器id数量
private static int allIdsCnt = 999;
private boolean loop = true;

@Override
public void run() {


    ArrayList weaponIdList = new ArrayList<>();

    while (loop) {

        generateId(weaponIdList);


    }

}

private /*synchronized*/ void generateId(ArrayList weaponIdList) {    //同步方法,上锁,在同一时刻,只能有一个线程来执行该方法

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        System.out.println(Thread.currentThread().getName() + "被唤醒【interrupt】了");
    }

    synchronized (this) {   //在操作临界资源的代码块上加锁,提高效率
        if (allIdsCnt <= 0) {
            System.out.println(allIdsCnt);
            setLoop(false);
        } else {
            String weaponId = duplicateOrNot(weaponIdList);
            System.out.println(Thread.currentThread().getName() + "获取成功: " + weaponId);
            System.out.println("剩余数量:" + (allIdsCnt));
        }
    }


}

private String duplicateOrNot(ArrayList weaponIdList) {
    String weaponId = getWeaponId();
    if (!weaponIdList.contains(weaponId)) {
        weaponIdList.add(weaponId);
        allIdsCnt--;
    } else {
        System.out.println("ID重复,正在重新生成");
        duplicateOrNot(weaponIdList);
        if (allIdsCnt < 0) {
            System.out.println("警告!获取越界,边界资源可能没有加锁");
            System.exit(0);
        }
    }
    return weaponId;
}

private String getWeaponId() {
    Random random = new Random();
    int z = random.nextInt(9);
    int x = random.nextInt(9);
    int c = random.nextInt(9);
    int v = random.nextInt(9);

    return ("id-" + z + x + c + v);
}

public void setLoop(boolean loop) {
    this.loop = loop;
}


}

class CreateWeaponIdConsole implements Runnable {
private final CreateWeaponId createWeaponId;

private final Scanner scanner = new Scanner(System.in);

public CreateWeaponIdConsole(CreateWeaponId createWeaponId) {
    this.createWeaponId = createWeaponId;
}

@Override
public void run() {

    while (true) {
        System.out.println("请输入S停止子线程");
        String key = scanner.next().toUpperCase();
        if (key.equals("S")) {
            createWeaponId.setLoop(false);
            break;
        }
    }

}
}

你可能感兴趣的:(线程,java,开发语言,面向对象编程)