📄 queue.java
字号:
//
// Queue Class
//
// This is my Queue class. Its members are Proc objects.
public class Queue
{
private int queueSize; ////the sizeofthe queue
private proc theQueue[]; ////the array of proc
private int nextFree; ////the next free space
// Constructor for the Queue class.
// Its MUST receive as argument the maximum queue size.
Queue(int size) ////construcotr
{
queueSize = size;
nextFree = 0;
theQueue = new proc[size];
}
public proc [] get_thequeue()
{
return this.theQueue;
}
// Returns TRUE if the queue is full.
public boolean isFull() ////full()
{
return (queueSize == nextFree);
}
// returns TRUE if the queue is empty.
public boolean isEmpty() /////empty()
{
return (nextFree == 0);
}
// Returns the number of processes currently in the queue.
public int length() ////get the actual len of the array
{
return nextFree;
}
// Reads the time left in the i-th process in the queue
public int getTime(int i) ////get the ith element's left time
{
return theQueue[i].getTime();
}
// Reads the process name of the i-th process in the queue
public String getName(int i) ////get the ith element's name
{
return theQueue[i].getName();
}
// Reads the creationTime of the first process in the queue
public int getCreation() ////get the ith element's creation time
{
return theQueue[0].getCreation();
}
public int getlasttime()
{
return theQueue[0].getlasttime();
}
public void setlasttime(int time)
{
this.theQueue[0].setlasttime(time);
}
// Changes the time left for the process being served by the CPU
// which is always process 0.
public void setTime(int i, int time) ////set the ith element's time left
{
theQueue[i].setTime(time);
}
// This method adds a new process to the queue. It receives as
// arguments the desired process execution time.
// Processes are always added to the end of the queue.
//
public synchronized void enqueue(proc newProcess) ////enroll the queue
{ //synchronized
if (isFull()) {
System.out.println("Cannot add process. Queue is full");
}
else {
theQueue[nextFree] = newProcess;
nextFree++;
}
}
// This method removes the process at the beginning of the queue.
// All other processes are rotated towards the beginning of the queue.
public synchronized void dequeue() ////leave the queue
{ ////synchronzed
if (isEmpty()) {
System.out.println("Cannot delete process. Queue is empty");
}
else ////remove the first proc from the queue
{
for (int i = 1; i < nextFree; i++)
{
theQueue[i - 1] = theQueue[i];
}
nextFree--;
}
}
// This method rotates the queue, so that Queue[0] is always the
// element being serviced by the CPU.
// eg: proc0---proc1----....----proc(N-1)---procN
// After rotate()
// proc1---proc2----....----procN----proc0
public void rotate()
{
/* rotating an empty queue is harmless. (I think!) */
if (!isEmpty())
{
proc temp = theQueue[0];
for (int i = 1; i < nextFree; i++) {
theQueue[i-1] = theQueue[i];
}
theQueue[nextFree - 1] = temp;
}
}
// For debugging purposes:
public void printQueue()
{
for (int i = 0; i < nextFree; i++) {
System.out.print("Queue[" + i + "] ");
System.out.println(theQueue[i].getTime());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -