首页 > 解决方案 > 调度3个元素的数组避免死锁

问题描述

给定一个共有 3 个 ["0","1","2"] 字符串元素的数组,所有初始呈现为可用,在分配 2 个元素之后,可以分配的最大元素数为 2(计划) ,不能再分配更多元素。基于这个前提,可能会出现以下场景。

(注:从代码中易于理解)

场景 1:进程 P1 请求一个元素,并被分配元素“0”(元素“0”然后变得无法分配给任何其他进程,因为它由进程 P1 持有),然后进程 P2 请求一个元素并被分配元素“1” - 现在阵列已达到其最大调度容量。任何进程(P1/P2)都可以在任何给定时间放弃它的元素。如果进程 P2 放弃它的元素“1”,那么进程 P3 请求一个元素,它应该被给予/分配元素“2”(因为元素 2 从未被分配过)。任何进程(P1/P3)都可以在任何给定时间放弃它的元素。如果进程 P1 放弃其元素“0”,则可以分配的下一个可用元素是元素“1”(因为数组中有两个元素(“1”、“0”)可用。

如何始终分配 2 个元素的最大值,避免元素饥饿?(假设进程以任何顺序都可以放弃它们的元素)

public class Main {

public static void main(String[] args)
{
    myScheduler p = new myScheduler();

    /* available elements: "0","1","2" imagine them being in a queue */
    /* elements taken: non */
    String show = p.requestElement(); //Should return "0"

    /* available elements: "1","2" imagine them being in a queue */
    /* elements taken: "0" */
    System.out.println(show);

    show = p.requestElement(); //Should return "1"

    /* available elements: "2" queue cannot be empty */
    /* elements taken: "0","1" imagine them being a linked list (at most 2 elements can be taken) */
    System.out.println(show);

    show = p.requestElement(); //Should return "full"
    /* available elements: "2" queue cannot be empty */
    /* elements taken: "0","1" note: it didn't change (at most 2 elements can be taken) */
    System.out.println(show); 
    show = p.abandonElement("1"); //Should return "1"
    /* available elements: "2","1" imagine them being in a queue */
    /* elements taken: "0" */
    System.out.println(show);
    show = p.requestElement(); //Should return "2"
    /* available elements: "1" queue cannot be empty */
    /* elements taken: "0","2" (at most 2 elements can be taken) */
    System.out.println(show);
    show = p.requestElement(); //Should return "full"
    /* available elements: "1" queue cannot be empty */
    /* elements taken: "0","2" note: it did not change (at most 2 elements can be taken) */
    System.out.println(show);
    show = p.abandonElement("0"); //Should return "0"

    /* available elements: "1","0" */
    /* elements taken: "2" */
    System.out.println(show);
    show = p.requestElement(); //Should return "1"

    /* available elements: "0" */
    /* elements taken: "2","1" */
    System.out.println(show);
}
public class myScheduler {

private String [] array;
public myScheduler()
{
    array = {"0","1","2"};
}
public String requestElement(){
    /*This function returns the element given to this request, if array has already assigned 2 elements, return "full" */
}

public String abandonElement(String element){
    /*This function returns the element to be abandoned, if the element doesnt exist, return "not found" */
}

标签: javaarraysqueueiterationscheduling

解决方案


所以这就是我要做的:构造一个LinkedList<>()来跟踪最先预订的时间。(队列是 FIFO-先进先出)然后创建一个final ArrayList<>()来跟踪您可以预订的时间。(您只希望您的用户能够预订0, 1,2

这是我的代码:

//Add the three choices. Make the ArrayList final so nobody can change it
public static final ArrayList<String> array = new ArrayList<>(); {{
    array.add("0");    
    array.add("1");
    array.add("2");
}}
public static LinkedList<String> linked = new LinkedList<>(); {{
  linked.add("0");
  linked.add("1");
  linked.add("2");
 }}

public String requestElement(){
    /*This function returns the element given to this request, if array has already assigned 2 elements, return "full" */
   String result = "full";  //Assume the result is full
   if(linked.size()==1) {  //If only one time is available return full 
       return result;
   }
   result = linked.poll();    //Poll the first item on the queue
   return result;
}
public String abandonElement(String element){
    /*This function returns the element to be abandoned, if the element doesnt exist, return "not found" */
    String result = "Not Found";
    if(array.contains(element)) {   //If the arrayList contains the element, add it back to the LinkedList
        linked.add(element);
        result = element;
    }
    return result;

}

请让我知道,如果你有任何问题!


推荐阅读