?? e_3.java
字號:
/**
* @author AP0336128
*內容:操作系統實驗3:模擬時間片輪轉調度程序
*
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class E_3 extends Applet implements Runnable
{
private static final long serialVersionUID = 1L;
static int sum,sum1,sum2;
static JProgressBar pb,pb1,pb2;
Thread p0,p1,p2;
TextField text;
Box baseBox,boxH,boxH2,boxH3;
Label label1,label2,label3,label4;
public void init()
{
label1=new Label("進程1(0-20)");
label2=new Label("進程2(0-30)");
label3=new Label("進程3(0-50)");
label4=new Label("時間片輪轉調度程序 時間片:10");
baseBox=Box.createVerticalBox();
boxH=Box.createHorizontalBox();
boxH2=Box.createHorizontalBox();
boxH3=Box.createHorizontalBox();
text=new TextField(30);
pb=new JProgressBar(0,20);
pb1=new JProgressBar(0,30);
pb2=new JProgressBar(0,50);
pb.setBackground(Color.WHITE);
pb.setStringPainted(true);
pb1.setBackground(Color.WHITE);
pb1.setStringPainted(true);
pb2.setBackground(Color.WHITE);
pb2.setStringPainted(true);
p0=new Thread(this);
p1=new Thread(this);
p2=new Thread(this);
boxH.add(label1);
boxH.add(pb);
boxH2.add(label2);
boxH2.add(pb1);
boxH3.add(label3);
boxH3.add(pb2);
baseBox.add(boxH);
baseBox.add(Box.createVerticalStrut(20));
baseBox.add(boxH2);
baseBox.add(Box.createVerticalStrut(20));
baseBox.add(boxH3);
baseBox.add(Box.createVerticalStrut(20));
baseBox.add(label4);
baseBox.add(text);
add(baseBox);
}
public void start()
{
p0.start(); p1.start();p2.start();
}
public synchronized void run()
{
try{
while(true)
{
if(Thread.currentThread()==p0)
{
sum=sum+1;
Thread.sleep(500);
setpb(sum,pb);
if(sum%10==0)
{
try{
wait();
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
notifyAll();
if(sum>=20)
{
p0.interrupt();
}
text.setText("進程1 計數 "+sum);
}
else if(Thread.currentThread()==p1)
{
sum1=sum1+1;
Thread.sleep(500);
setpb(sum1,pb1);
if(sum1%10==0)
{
try{
wait();
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
notifyAll();
text.setText("進程2 計數 "+sum1);
}
else if(Thread.currentThread()==p2)
{
sum2=sum2+1;
Thread.sleep(500);
setpb(sum2,pb2);
if(sum2%10==0)
{
try{
wait();
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
notifyAll();
text.setText("進程3 計數 "+sum2);
}
}
}catch (InterruptedException e) {}
}
public synchronized void setpb(int sum3,JProgressBar c)
{
c.setValue(sum3);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -