JAVA多线程实现调度
教师评阅意见:
实验成绩:
签名:
年月日
一、实验目的 1、理解程序、线程和进程的概念; 2、理解多线程的概念; 3、掌握线程的各种状态; 4、熟练使用 Thread 类创建线程; 5、熟练使用线程各种方法; 6、掌握线程的调度及线程同步的实现原理。
二、实验内容及要求 进程调度是处理机管理的核心内容。本实验要求采用最高优先数
五、主要代码
import java.awt.Font; import java.awt.event.*;
import java.awt.*;
import javax.swing.*; public class DoubleBuffer extends Frame //主类继承 Frame 类 { private JButton button1;
gBuffer1.fillRect(440,100,60,270);
gBuffer1.setColor(Color.GREEN);
gBuffer1.fillRect(500,205,170,60);
paint(gBuffer1);
scr.drawImage(iBuffer1,0,0,this);
}
public static void main(String[] args)
{
DoubleBuffer DB=new DoubleBuffer();//创建主类的对象
DB.addWindowListener(new WindowAdapter()
//添加窗口关闭处理
函数
{
public void windowClosing(WindowEvent e)
iBuffer1=createImage(670,400); gBuffer1=iBuffer1.getGraphics();
} gBuffer1.setColor(Color.BLACK); gBuffer1.fillRect(100,100,340,60); gBuffer1.setColor(Color.BLACK); gBuffer1.fillRect(100,170,340,60); gBuffer1.setColor(Color.BLACK); gBuffer1.fillRect(100,240,340,60); gBuffer1.setColor(Color.BLACK); gBuffer1.fillRect(100,310,340,60); gBuffer1.setColor(Color.MAGENTA);
DB.x1=DB.x1+40; DB.x1++; //修改小圆的横坐标 try {
sleep(15); //线程休眠 15ms } catch(InterruptedException e){} }
}
if(s==2) {
if(DB.x2<440) {
DB.x2++; //修改小圆的横坐标 try {
三、实验设备
PC 机,windows7,eclipse。
四、设计方案 ㈠ 设计主要思想 (1)要模拟进程的调度算法,必须先体现处进程及系统资源。 (2)要体现先来先服务的算法,就必须表现出当有一个进程进
入 CPU 时其他进程不能进入,并在就绪队列中排队。本实验建立了 四个圆移动的线程表示作业调度,用圆在表示就绪队列的方框中停留 表示进程在就绪队列中排队。
{
System.exit(0);
}
});
}
}
class paintThread extends Thread//绘图线程类 {
DoubleBuffer DB; int s; public paintThread(DoubleBuffer DB,int s) //构造函数 {
this.DB=DB; this.s=s; } public void run()//重载 run()函数 { while(true)//线程中的无限循环 {
@Override public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub x1=100; y1=100;//小圆横、纵坐标 x2=100; y2=170;//小圆横、纵坐标 x3=100; y3=240;//小圆横、纵坐标 x4=100; y4=310;//小圆横、纵坐标 a=1; b=2; c=3; d=4;
sleep(13); //线程休眠 13ms
} catch(InterruptedException e){} } else if(DB.a==1&&DB.b==2&&DB.c==3&&DB.d==0&&DB.x2<670) { DB.y2=205; if(DB.x2==340)
DB.x2=DB.x2+40; DB.x2++; //修改小圆的横坐标 try {
scr.setColor(Color.BLUE); //设置小圆颜色 //Font font = new Font("宋体", Font.BOLD, 20); //scr.setFont(font); scr.drawString("线程二", 250, 205); scr.fillOval(x2,y2,60,60); //绘制小圆 scr.setColor(Color.YELLOW); //设置小圆颜色 //Font font = new Font("宋体", Font.BOLD, 20); //scr.setFont(font); scr.drawString("线程三", 250, 275); scr.fillOval(x3,y3,60,60); //绘制小圆 scr.setColor(Color.PINK); //设置小圆颜色 //Font font = new Font("宋体", Font.BOLD, 20); //scr.setFont(font); scr.drawString("线程四", 250, 345); scr.fillOval(x4,y4,60,60); //绘制小圆 //button1.repaint(); } public void update(Graphics scr) { if(iBuffer1==null) {
重庆交通大学 综合性设计性实验报告
实验项目名称: 进程调度(先来先服务)
实验项目性质: JAVA 多线程
实验所属课程: JAVA 程序设计
实验室(中心): 语 音 大 楼 8 楼 801
班
级: 软件专业 2012 级 2 班
姓
名: 尚亚*
学
号: 631206050216
指导教师:杨
实验完成时间: 2014 年 11 月 25 日
DB.repaint();//窗口重绘
if(s==1) {
if(DB.x1<440) {
DB.x1++; //修改小圆的横坐标 try {
sleep(20); //线程休眠 20ms } catch(InterruptedException e){} } else if(DB.a==1&&DB.b==0&&DB.x1<670) { DB.y1=205; if(DB.x1==440)
优先的调度算法(即把处理机分配给优先数最高的进程)和先来先服 务算法编写和调试一个简单的进程调度程序。通过本实验可以加深理 解有关进程控制块、进程队列的概念。并体会了优先数和先来先服务 调度算法的具体实施办法。
用 JAVA 语言编写和调试一个进程调度程序,以加深对进程的概 念及进程调度算法的理解。做一个能够直观体现多个进程时,CPU 是怎样调度就绪队列中的进程(按照先来先服务的原则)。
super("Java 实现调度模拟"); this.setResizable(false); //窗体不可最大化 this.setSize(750,470); //设置窗口的首选大小 this.setVisible(true); //显示窗口 button1= new JButton("运行"); pT1=new paintThread(this,1); pT2=new paintThread(this,2); pT3=new paintThread(this,3); pT4=new paintThread(this,4); button1.setLocation(100, 415); button1.setSize(90, 40); add(button1); button1.addActionListener(new ActionListener() {
private Image iBuffer,iBuffer1; private Graphics gBuffer1,gBuffer2,gBuffer3,gBuffer4,gBuffer5,gBuffer6; public paintThread pT1;//绘图线程 public paintThread pT2;//绘图线程 public paintThread pT3;//绘图线程 public paintThread pT4;//绘图线程 public int x1=100,y1=100;//小圆横、纵坐标 public int x2=100,y2=170;//小圆横、纵坐标 public int x3=100,y3=240;//小圆横、纵坐标 public int x4=100,y4=310;//小圆横、纵坐标 public int a=1; public int b=2; public int c=3; public int d=4; public DoubleBuffer()//构造函数 {