⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e_3.java

📁 进程调度,时间片轮转调度.解压密码:4
💻 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 + -