设备分配策略(Java版)附测试数据

package deceive_allocation;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author lele
 * @title 设备分配流程
 * @date 2020-11-2
 */
public class DeceiveAllocationFlow {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        //初始化设备信息

        /*测试数据一:
        CHCT channel1 = new CHCT("channel1", "闲");
        CHCT channel2 = new CHCT("channel2", "闲");
        CHCT channel3 = new CHCT("channel3", "闲");
        List<CHCT> channelsOfController1 = new ArrayList<>();
        channelsOfController1.add(channel1);
        channelsOfController1.add(channel2);
        COCT controller1 = new COCT("controller1", "闲", channelsOfController1);
        List<CHCT> channelsOfController2 = new ArrayList<>();
        channelsOfController2.add(channel2);
        COCT controller2 = new COCT("controller2", "闲", channelsOfController2);
        List<CHCT> channelsOfController3 = new ArrayList<>();
        channelsOfController3.add(channel3);
        COCT controller3 = new COCT("controller3", "闲", channelsOfController3);
        List<COCT> controllerOfDeceive1 = new ArrayList<>();
        controllerOfDeceive1.add(controller1);
        controllerOfDeceive1.add(controller2);
        DCT deceive1 = new DCT("deceive1", "闲", controllerOfDeceive1);
        List<COCT> controllerOfDeceive2 = new ArrayList<>();
        controllerOfDeceive2.add(controller2);
        controllerOfDeceive2.add(controller3);
        DCT deceive2 = new DCT("deceive2", "闲", controllerOfDeceive2);
        List<COCT> controllerOfDeceive3 = new ArrayList<>();
        controllerOfDeceive3.add(controller3);
        DCT deceive3 = new DCT("deceive3", "闲", controllerOfDeceive3);

        //将设备添加到系统设备表
        SDT sdt = new SDT();
        sdt.dcts.add(deceive1);
        sdt.dcts.add(deceive2);
        sdt.dcts.add(deceive3);
        //打印设备信息
        sdt.printDeceiveInfo();

        process1 deceive1
        process2 deceive1
        process3 deceive3
        process4 deceive2
        */

        CHCT channel1 = new CHCT("channel1", "闲");
        CHCT channel2 = new CHCT("channel2", "闲");
        CHCT channel3 = new CHCT("channel3", "闲");
        List<CHCT> channelsOfController1 = new ArrayList<>();
        channelsOfController1.add(channel1);
        channelsOfController1.add(channel2);
        COCT controller1 = new COCT("controller1", "闲", channelsOfController1);
        List<CHCT> channelsOfController2 = new ArrayList<>();
        channelsOfController2.add(channel2);
        channelsOfController2.add(channel3);
        COCT controller2 = new COCT("controller2", "闲", channelsOfController2);
        List<COCT> controllerOfDeceive1 = new ArrayList<>();
        controllerOfDeceive1.add(controller1);
        controllerOfDeceive1.add(controller2);
        DCT deceive1 = new DCT("deceive1", "闲", controllerOfDeceive1);
        List<COCT> controllerOfDeceive2 = new ArrayList<>();
        controllerOfDeceive2.add(controller2);
        DCT deceive2 = new DCT("deceive2", "闲", controllerOfDeceive2);
        List<COCT> controllerOfDeceive3 = new ArrayList<>();
        controllerOfDeceive3.add(controller2);
        DCT deceive3 = new DCT("deceive3", "闲", controllerOfDeceive3);


        /*
        测试数据
        process2 deceive2
        process3 deceive3
         */
        //将设备添加到系统设备表
        SDT sdt = new SDT();
        sdt.dcts.add(deceive1);
        sdt.dcts.add(deceive2);
        sdt.dcts.add(deceive3);
        //打印设备信息
        sdt.printDeceiveInfo();
        Scanner in = new Scanner(System.in);
        sdt.executeWaitQueueProcess();
        while (true) {
    
    
            Process process = new Process();
            process.dctId = process.coctId = process.chctId = -1;
            process.identifier = in.next();//输入进程标识
            process.deceiveIdentifier = in.next();//输入进程请求的设备标识
            sdt.requestDeceive(process);//请求设备
        }
    }
}

class Process {
    
    
    String identifier;
    Integer dctId;
    Integer coctId;
    Integer chctId;
    String deceiveIdentifier;
}

//系统设备表:记录系统中全部设备的信息
class SDT {
    
    
    volatile List<DCT> dcts = new ArrayList<>();

    void printDeceiveInfo() {
    
    
        for (DCT dct : dcts) {
    
    
            for (COCT coct : dct.cocts) {
    
    
                for (CHCT chct : coct.chcts) {
    
    
                    System.out.println(" 设备标识:" + dct.identifier + " 状态:" + dct.state +
                            " 控制器标识:" + coct.identifier + " 状态:" + coct.state + " 通道标识:" + chct.identifier + " 状态:" + chct.state);
                }
            }
        }
    }

    void executeWaitQueueProcess() {
    
    
        new Thread(() -> {
    
    
            while (true) {
    
    //轮训各个设备的等待队列
                for (DCT dct : dcts) {
    
    
                    if (!dct.waitQueue.isEmpty() && dct.state.equals("闲")) {
    
    //若该设备的等待队列中有请求的进程且当前设备状态为闲置时
                        Process poll = dct.waitQueue.poll();//该请求进程出栈
                        try {
    
    
                            System.out.println(poll.deceiveIdentifier + " 等待队列:" + poll.identifier + "已占用此设备...");
                            requestDeceive(poll);//继续分配控制器
                        } catch (InterruptedException e) {
    
    
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
        new Thread(() -> {
    
    
            while (true) {
    
    
                for (DCT dct : dcts) {
    
    //轮训各个设备的控制器
                    if (!dct.controllerWaitQueue.isEmpty()) {
    
    //若有进程已占用设备,但为占用控制器时
                        for (COCT coct : dct.cocts) {
    
    //遍历该设备所有的控制器
                            if (coct.state.equals("闲")) {
    
    //当有一个控制器是空闲时
                                Process poll = dct.controllerWaitQueue.poll();//则将等待进程出栈
                                try {
    
    
                                    System.out.println(poll.deceiveIdentifier + "设备已请求到控制器成功...");
                                    requestDeceive(poll);
                                    break;
                                } catch (InterruptedException e) {
    
    
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }).start();
        new Thread(() -> {
    
    
            while (true) {
    
    
                for (DCT dct : dcts) {
    
    
                    for (COCT coct : dct.cocts) {
    
    
                        if (!coct.channelWaitQueue.isEmpty()) {
    
    
                            for (CHCT chct : coct.chcts) {
    
    
                                if (chct.state.equals("闲")) {
    
    
                                    Process poll = coct.channelWaitQueue.poll();
                                    try {
    
    
                                        System.out.println(poll.deceiveIdentifier + "设备已请求到通道成功...");
                                        requestDeceive(poll);
                                        break;
                                    } catch (InterruptedException e) {
    
    
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }).start();
    }

    void requestDeceive(Process process) throws InterruptedException {
    
    
        new Thread(() -> {
    
    
            if (process.dctId == -1) {
    
    
                boolean flag = false;
                int i;
                for (i = 0; i < dcts.size(); i++) {
    
    
                    if (dcts.get(i).identifier.equals(process.deceiveIdentifier)) {
    
    
                        if (dcts.get(i).state.equals("闲")) {
    
    
                            flag = true;
                        } else {
    
    
                            flag = false;
                        }
                        break;
                    }
                }
                if (flag) {
    
    
                    dcts.get(i).state = "忙";
                    process.dctId = i;
                } else {
    
    
                    dcts.get(i).waitQueue.add(process);
                    System.out.println(process.identifier + ": 请求的设备:" + process.deceiveIdentifier + "已被占用,已加入请求队列等待...");
                    return;
                }
            }

            if (process.coctId == -1) {
    
    
                List<COCT> cocts = dcts.get(process.dctId).cocts;
                int i;
                for (i = 0; i < cocts.size(); i++) {
    
    
                    if (cocts.get(i).state.equals("闲")) {
    
    
                        break;
                    }
                }
                if (i < cocts.size()) {
    
    
                    cocts.get(i).state = "忙";
                    process.coctId = i;
                } else {
    
    
                    dcts.get(process.dctId).controllerWaitQueue.add(process);
                    System.out.println(process.deceiveIdentifier + ":设备所需的控制器已全部被占用,已加入请求队列等待...");
                    return;
                }
            }

            if (process.chctId == -1) {
    
    
                List<CHCT> chcts = dcts.get(process.dctId).cocts.get(process.coctId).chcts;
                int i;
                for (i = 0; i < chcts.size(); i++) {
    
    
                    if (chcts.get(i).state.equals("闲")) {
    
    
                        break;
                    }
                }
                if (i < chcts.size()) {
    
    
                    chcts.get(i).state = "忙";
                    process.chctId = i;
                } else {
    
    
                    dcts.get(process.dctId).cocts.get(process.chctId).channelWaitQueue.add(process);
                    System.out.println(process.deceiveIdentifier + ":设备所需的通道已全部被占用,已加入请求队列等待...");
                    return;
                }
            }

            //执行
            System.out.println(process.identifier + ":请求设备:" + process.deceiveIdentifier + "成功!");
            //打印状态信息
            printDeceiveInfo();
            try {
    
    
                Thread.sleep(8000 + new Random().nextInt(8000));
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println(process.identifier + ":使用设备:" + process.deceiveIdentifier + "完成!");
            dcts.get(process.dctId).state = "闲";
            dcts.get(process.dctId).cocts.get(process.coctId).state = "闲";
            dcts.get(process.dctId).cocts.get(process.coctId).chcts.get(process.chctId).state = "闲";
        }).start();
    }
}


//设备控制表:记录该设备的情况
class DCT {
    
    
    String identifier;//设备标识符
    String state;//设备状态
    volatile Queue<Process> waitQueue = new LinkedBlockingQueue<>();//进程设备请求队列
    List<COCT> cocts = new ArrayList<>();//控制器表

    volatile Queue<Process> controllerWaitQueue = new ConcurrentLinkedDeque();//控制器等待队列指针

    public DCT(String identifier, String state, List<COCT> cocts) {
    
    
        this.identifier = identifier;
        this.state = state;
        this.cocts = cocts;
    }
}

//控制器控制表:为每一个控制器都配置了用于记录本控制器情况的控制器控制表
class COCT {
    
    

    String identifier;//
    // 控制器标识符
    String state;//控制器状态
    List<CHCT> chcts = new ArrayList<>();//通道控制表
    volatile Queue<Process> channelWaitQueue = new ConcurrentLinkedDeque<>();//设备等待队列指针

    public COCT(String identifier, String state, List<CHCT> chcts) {
    
    
        this.identifier = identifier;
        this.state = state;
        this.chcts = chcts;
    }
}

//通道控制表:为每一个通道都配置了一张用于记录本通道情况的通道控制表
class CHCT {
    
    
    String identifier;//通道标识符
    String state;//通道状态

    public CHCT(String identifier, String state) {
    
    
        this.identifier = identifier;
        this.state = state;
    }
}









猜你喜欢

转载自blog.csdn.net/qq_44486437/article/details/109476689