使用java隊(duì)列算法,網(wǎng)站有第三方實(shí)現(xiàn)jar.
目前累計(jì)服務(wù)客戶近1000家,積累了豐富的產(chǎn)品開發(fā)及服務(wù)經(jīng)驗(yàn)。以網(wǎng)站設(shè)計(jì)水平和技術(shù)實(shí)力,樹立企業(yè)形象,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)、網(wǎng)絡(luò)營銷、VI設(shè)計(jì)、網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。創(chuàng)新互聯(lián)公司始終以務(wù)實(shí)、誠信為根本,不斷創(chuàng)新和提高建站品質(zhì),通過對(duì)領(lǐng)先技術(shù)的掌握、對(duì)創(chuàng)意設(shè)計(jì)的研究、對(duì)客戶形象的視覺傳遞、對(duì)應(yīng)用系統(tǒng)的結(jié)合,為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進(jìn)步。
你也可以自己簡(jiǎn)單實(shí)現(xiàn)一個(gè),可以同數(shù)組模擬,也可以用List模擬。
在java中有java.util.Queue接口,你可以詳細(xì)查看一下java的API相關(guān)的說明后在使用。
import java.util.Vector;
@SuppressWarnings({ "unchecked", "serial" })
public class Stat extends Vector {
public void push(Object x) {
super.add(x); // 向隊(duì)尾添加組件
}
public Object pop() { // 隊(duì)首元素出隊(duì)(從隊(duì)列刪除)
Object x = super.elementAt(0); // 返回指定索引處的組件
super.removeElementAt(0); // 刪除指定索引處的組件
return x;
}
public void remove() {
super.removeAllElements(); // removeAllElements()移除全部組件,并將其大小設(shè)置為零
}
public static void main(String[] args) throws java.io.IOException {
Stat s = new Stat();
s.push("123adfasf123");
s.push(123123132);
System.out.println(s.pop());
System.out.println(s.pop());
}
}
import?java.util.LinkedList;
public?class?Demo01?{
private?LinkedListObject?linkedList;
public?Demo01()?{
linkedList?=?new?LinkedListObject();
}
public?void?put(Object?object)?{
linkedList.add(object);
}
public?Object?get()?{
Object?object?=?null;
if?(linkedList.size()?!=?0)?{
object?=?linkedList.get(0);
linkedList.remove(0);
}
return?object;
}
public?boolean?isEmpty()?{
if?(linkedList.size()?!=?0)?{
return?true;
}?else?{
return?false;
}
}
public?static?void?main(String[]?args)?{
Demo01?demo01?=?new?Demo01();
demo01.put("1");
demo01.put("2");
System.out.println(demo01.get());
System.out.println(demo01.get());
System.out.println(demo01.isEmpty());
}
}
結(jié)果:
1
2
false
沒java的 發(fā)段源代碼給你 有興趣自己慢慢理解
#include time.h
#include dos.h
#include math.h
#include conio.h
#include stdio.h
#include stdlib.h
#include time.h
#include graphics.h
#define ESC 0x1b
#define ENTER 0x0d
#define TRUE 1
#define FALSE 0
/*每隔TIME秒就變換一次優(yōu)先級(jí)*/
#define TIME 5
/*數(shù)據(jù)結(jié)構(gòu)*/
/****************************************************************/
enum _Status/*進(jìn)程狀態(tài)枚舉*/
{
READY =0,/*就緒*/
RUN,/*執(zhí)行中*/
SUSPEND,/*掛起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*進(jìn)程結(jié)構(gòu)*/
{
int PID;/*進(jìn)程ID,ID為負(fù)數(shù)的進(jìn)程為系統(tǒng)后備隊(duì)列的作業(yè)*/
int Time;/*進(jìn)程運(yùn)行需要的時(shí)間*/
int Prior;/*進(jìn)程的優(yōu)先級(jí),越大越優(yōu)先*/
Status Sts;/*狀態(tài)*/
struct _Pcb *Next;/*指向本進(jìn)程隊(duì)列中下個(gè)進(jìn)程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道處理中的道結(jié)構(gòu)*/
{
PCB *pcb;/*該道當(dāng)前正在處理的進(jìn)程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系統(tǒng)相關(guān)全局變量*/
PCB *ProcQueue = NULL;/*進(jìn)程鏈表,按優(yōu)先級(jí)從大到小排列*/
Batch *BatchQueue = NULL;/*系統(tǒng)多道鏈表*/
/****************************************************************/
/*動(dòng)態(tài)優(yōu)先權(quán)搶占式調(diào)度算法及相關(guān)函數(shù)聲明*/
/****************************************************************/
int InitBatchs(int n);/*初始化多道系統(tǒng),n為道數(shù)*/
int InsertProc(int prior, int time);/*向進(jìn)程鏈表中按優(yōu)先級(jí)大小插入一個(gè)新進(jìn)程*/
int InsertIDLE();/*向進(jìn)程隊(duì)列中加入后備進(jìn)程,當(dāng)系統(tǒng)空閑時(shí)將被調(diào)入*/
int SortProcQueue();/*將進(jìn)程鏈表按優(yōu)先級(jí)從大到小排列*/
int AddBatch();/*增加系統(tǒng)道數(shù)*/
int DeleteBatch();/*減少系統(tǒng)道數(shù)*/
int UnSuspendProc(int id);/*解除ID為id的進(jìn)程的掛起狀態(tài)*/
int UpdateBatchs();/*多道系統(tǒng)根據(jù)進(jìn)程鏈表進(jìn)行更新,并將執(zhí)行完畢的進(jìn)程刪除*/
int PassSeconds(int n);/*系統(tǒng)經(jīng)過n秒后計(jì)算數(shù)據(jù)并進(jìn)行優(yōu)先級(jí)調(diào)度*/
/****************************************************************/
/*各函數(shù)的定義*/
/****************************************************************/
int InitBatchs(int n)
{
int i;
for (i=0; in; ++i)
{
AddBatch();
}
return (UpdateBatchs());
}
int InsertProc(int prior, int time)
{
static int sysid = 0;/*該系統(tǒng)已經(jīng)加入過多少進(jìn)程,此值將是新進(jìn)程的ID*/
PCB *last,*now,*pcb;
pcb = (PCB*)malloc(sizeof(PCB));
if (pcb == NULL) return FALSE;
pcb-Prior = prior;
pcb-Time = time;
pcb-PID = (++sysid);
pcb-Sts = READY;
if (ProcQueue == NULL)/*如果進(jìn)程隊(duì)列為空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb將排在隊(duì)頭*/
{
pcb-Next = ProcQueue;
ProcQueue = pcb;
return TRUE;
}
while ((now != NULL) (pcb-Prior now-Prior))/*尋找插入位置*/
{
last = now;
now = last-Next;
}
last-Next = pcb;
pcb-Next = now;
return TRUE;
}
int InsertIDLE()
{
PCB *now = ProcQueue;
PCB *idle = (PCB*)malloc(sizeof(PCB));
if (idle == NULL) return FALSE;
idle-PID = -1;
idle-Prior = -1;
idle-Sts = SUSPEND;
idle-Time = -1;
idle-Next = NULL;
if (ProcQueue == NULL)
{
ProcQueue = idle;
return TRUE;
}
while(now-Next != NULL)
{
now = now-Next;
}
now-Next = idle;
return TRUE;
}
int SortProcQueue()
{ /*冒泡排序*/
PCB *last, *now;
int b = FALSE;/*上次遍歷是否無交換產(chǎn)生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果鏈表中無進(jìn)程或只有一個(gè)進(jìn)程*/
return FALSE;
while (!b)
{
b = TRUE;
last=ProcQueue;
now=last-Next;
if (last-Prior now-Prior)
{
ProcQueue = now;
last-Next = now-Next;
now-Next = last;
b = FALSE;
last = ProcQueue;
now = last-Next;
}
while (now-Next!=NULL)
{
if ((now-Prior)(now-Next-Prior))
{
last-Next = now-Next;
now-Next = now-Next-Next;
last-Next-Next = now;
b = FALSE;
}
else
last = last-Next;
now = last-Next;
}
}
return TRUE;
}
int AddBatch()
{
Batch *bt = (Batch*)malloc(sizeof(Batch));
if (bt==NULL) return FALSE;
bt-Next = BatchQueue;
BatchQueue = bt;
bt-pcb = NULL;
return (InsertIDLE());
}
int DeleteBatch()
{
Batch *bt = BatchQueue;
PCB *last, *now;
if (BatchQueue==NULL || BatchQueue-Next==NULL)/*如果只剩最后一道則不刪除*/
return FALSE;
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果只有最后一個(gè)后備空閑進(jìn)程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一個(gè)進(jìn)程,該進(jìn)程必定是后備空閑進(jìn)程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后備進(jìn)程*/
return FALSE;/**/
free(now);
last-Next = NULL;
BatchQueue = BatchQueue-Next;
free(bt);
return TRUE;
}
int UnSuspendProc(int id)
{
PCB *now = ProcQueue;
if (ProcQueue==NULL) return FALSE;
while (now != NULL)
{
if (now-PID == id)
{
now-Sts = READY;
return TRUE;
}
}
return FALSE;
}
int UpdateBatchs()
{
Batch *bt = BatchQueue;
PCB *last = ProcQueue, *now;
while (bt != NULL)
{
bt-pcb = NULL;
bt = bt-Next;
}
if (ProcQueue == NULL) return TRUE;
while (last-Sts==RUN last-PID=0 last-Time=0)
{
ProcQueue = ProcQueue-Next;
free(last);
last = ProcQueue;
}
now = last-Next;
while (now != NULL)
{
if (now-Sts==RUN now-PID=0 now-Time=0)/*如果該進(jìn)程是運(yùn)行中的一般進(jìn)程并已執(zhí)行完畢*/
{
last-Next = now-Next;
free(now);
}
else
last = last-Next;
now = last-Next;
}
bt = BatchQueue;
now = ProcQueue;
while (bt != NULL now != NULL)
{
bt-pcb = now;
now-Sts = RUN;
bt = bt-Next;
now = now-Next;
}
while (now != NULL)
{
if (now-Sts == RUN)
{
now-Sts = SUSPEND;
}
now = now-Next;
}
return TRUE;
}
int PassSeconds(int n)
{
static int time = 0;
int i=0, ProcEnd = FALSE;
PCB *pcb = ProcQueue;
Batch *bt = BatchQueue;
if (bt == NULL) return FALSE;
time += n;
if (time=TIME)
{
i = time/TIME;/*經(jīng)過多少時(shí)間段*/
time = time%TIME;
}
while (bt != NULL)/*更新進(jìn)程運(yùn)行時(shí)間*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*進(jìn)程結(jié)束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新進(jìn)程優(yōu)先權(quán)(動(dòng)態(tài)優(yōu)先權(quán))*/
{
if (pcb-Sts == RUN pcb-PID=0)/*運(yùn)行的進(jìn)程優(yōu)先權(quán)降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*掛起的進(jìn)程優(yōu)先權(quán)升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果優(yōu)先級(jí)有變動(dòng)則重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道進(jìn)程*/
}
return TRUE;
}
/****************************************************************/
/*圖形界面相關(guān)函數(shù)*/
/****************************************************************/
/*表格的單位寬度和高度*/
#define WIDTH 64
#define HEIGHT 12
void *black=NULL;/*背景色方格,使用它擦出表格中的圖形*/
int InitGraph()/*初始化圖形界面*/
{
int GraphDriver; /* The Graphics device driver */
int GraphMode; /* The Graphics mode value */
int ErrorCode;
GraphDriver = DETECT; /* Request auto-detection */
initgraph( GraphDriver, GraphMode, "" );
ErrorCode = graphresult(); /* Read result of initialization*/
if( ErrorCode != grOk )
{ /* Error occured during init */
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
getch();
return FALSE;
}
cleardevice();
black = (void*)malloc(imagesize(1,1,WIDTH-1,HEIGHT-1));
getimage(1,1,WIDTH-1,HEIGHT-1,black);
DrawFrame();
DrawData();
return TRUE;
}
int DrawFrame()/*畫邊框和表頭*/
{
settextjustify(CENTER_TEXT, CENTER_TEXT);
gprintf(320, HEIGHT/2-1, "Multi-Batch System Emulation");
settextjustify(LEFT_TEXT, TOP_TEXT);
moveto(0,HEIGHT);
lineto(0,479);
lineto(639,479);
lineto(639,HEIGHT);
lineto(0,HEIGHT);
line(WIDTH*4,HEIGHT,WIDTH*4,479);
line(WIDTH*7,HEIGHT,WIDTH*7,479);
line(0,HEIGHT*2,639,HEIGHT*2);
line(0,HEIGHT*3,639,HEIGHT*3);
gprintf(HEIGHT*0+2,HEIGHT*1+2,"System Batchs");/*系統(tǒng)多道列表頭*/
gprintf(HEIGHT*0+2,HEIGHT*2+2,"Batch");
gprintf(WIDTH*1+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*2+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*3+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*4+2,HEIGHT*1+2,"Suspended Processes");/*掛起隊(duì)列列表頭*/
gprintf(WIDTH*4+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*5+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*6+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*7+2,HEIGHT*1+2,"Ready Processes");/*就緒隊(duì)列列表頭*/
gprintf(WIDTH*7+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*8+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*9+2,HEIGHT*2+2,"Prior");
}
int DrawData()/*繪制系統(tǒng)數(shù)據(jù)*/
{
int numRun=0, numSus=0, numRed=0;/*運(yùn)行掛起和就緒的進(jìn)程各有多少*/
PCB* now = ProcQueue;
int x=0, y=0;
while (now != NULL)
{
switch(now-Sts)
{
case RUN:
x = WIDTH*1;
y = HEIGHT*(3+(numRun++));
break;
case SUSPEND:
x = WIDTH*4;
y = HEIGHT*(3+(numSus++));
break;
case READY:
x = WIDTH*7;
y = HEIGHT*(3+(numRed++));
break;
}
if (now-Sts==RUN)/*該進(jìn)程為正在運(yùn)行的進(jìn)程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*該進(jìn)程不是后備進(jìn)程*/
{
putimage(x+1,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"%d",now-PID);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
gprintf(x+WIDTH+2,y+2,"%d",now-Time);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+WIDTH*2+2,y+2,"%d",now-Prior);
}
else
{
putimage(x+1,y+1,black,COPY_PUT);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"system idle process");
}
now = now-Next;
}
}
int DlgGetNum(char *buf,int l,int t,int r,int b,int gettime)
{
char ch;
int pos=0;
bar(l,t,r,b);
gprintf(l+10,t+5,"Add new Process");
if (gettime)
gprintf(l+10,t+20,"input the time:");
else
gprintf(l+10,t+20,"input the priority:");
while (1)
{
ch = getch();
if (ch == ENTER)/*如果輸入了回車鍵*/
{
if(pos!=0)/*如果位置不在第一位(回車鍵不準(zhǔn)第一個(gè)輸入)*/
{
buf[pos]='\0';
break;
}
}
else if (ch='0' ch='9')
{
buf[pos++]=ch;
buf[pos]='\0';
}
else if (ch == ESC)
{
return FALSE;
}
else/*其他按鍵均按BackSpace處理*/
{
--pos;
buf[pos]='\0';
}
if (pos0)
{ pos=0; buf[pos]='\0';}
else if (pos4)/*最多輸入4位數(shù)*/
{ pos=4; buf[pos]='\0';}
bar(l,t+35,r,t+47);
gprintf(l+50,t+35,buf);
}
return TRUE;
}
int NewProcDlg()
{
int l=200,t=150,r=380,b=200,pos=0,prior=0,time=0;
char buf[5],ch;
PCB *pcb;
void* bg = (void*)malloc(imagesize(l,t,r,b));
getimage(l,t,r,b,bg);
setfillstyle(1,2);
flushall();
/*******輸入優(yōu)先級(jí)**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******輸入時(shí)間**********/
pos=0;
buf[pos]='\0';
if (!DlgGetNum(buf,l,t,r,b,TRUE))
goto exit;
time = atoi(buf);
InsertProc(prior, time);
exit:
putimage(l,t,bg,COPY_PUT);
free(bg);
return TRUE;
}
int gprintf( int xloc, int yloc, char *fmt, ... )/*圖形系統(tǒng)中的格式輸出*/
{
va_list argptr; /* Argument list pointer */
char str[140]; /* Buffer to build sting into */
int cnt; /* Result of SPRINTF for return */
va_start( argptr, format ); /* Initialize va_ functions */
cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */
outtextxy( xloc, yloc, str ); /* Send string in graphics mode */
va_end( argptr ); /* Close va_ functions */
return( cnt ); /* Return the conversion count */
}
/****************************************************************/
/*main函數(shù)*/
int main()
{
clock_t start=0, end=0;
char kb;
InitBatchs(3);/*初始化為3道系統(tǒng)*/
InitGraph();
while (1)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 = 1)/*時(shí)間過了一秒*/
{
start = end = clock();
PassSeconds(1);
cleardevice();
DrawFrame();
DrawData();
}
}
kb = getch();
switch (kb)
{
case ESC:
closegraph();
return 0;
case 'w':
AddBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 's':
DeleteBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 'i':
NewProcDlg();
UpdateBatchs();
DrawFrame();
DrawData();
break;
}
}
return 0;
}