调度3个元素的数组避免死锁

Scheduling array of 3 elements avoiding deadlock

给定一个总共有 3 个 ["0","1","2"] 字符串元素的数组,所有元素最初都呈现为可用,可以分配的最大元素数为 2,在 2 个元素具有已分配(预定),无法分配更多元素。基于这个前提,会出现下面的场景。

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

场景 1:进程 P1 请求一个元素,并被分配元素“0”(元素“0”随后变得不可分配给任何其他进程,因为它由进程 P1 持有),然后进程 P2 请求一个元素并被分配元素“1” - 现在数组已达到其最大调度容量。任何进程 (P1/P2) 都可以在任何给定时间放弃它的元素。如果进程 P2 放弃它的元素“1”,那么进程 P3 请求一个元素,它应该是 given/assigned 元素“2”(因为元素 2 从未被分配)。任何进程 (P1/P3) 都可以在任何给定时间放弃它的元素。如果进程 P1 放弃其元素“0”,则可以分配的下一个可用元素是元素“1”(因为数组中的两个元素(“1”、“0”)可用。整个循环可能发生在 P( n) 进程,前提是保持不超过两个的规则。

如何始终分配最多 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" */
}

所以这就是我要做的:构造一个 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;

}

如有任何问题,请告诉我!