调度 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"(可用。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<>()来跟踪哪些时间是先预订的。(队列是先进先出(然后创建一个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;
}

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

最新更新