📄 spoolingtest.java
字号:
import javax.swing.*;
import java.math.*;
public class SpoolingTest{
private static Output output1 , output2;
private static Spooling spo;
private static List list = new List();//存储井
private static List wait = new List();//等待队列
private static String[] file1 = new String[50];//输出进程一的文件
private static String s ="";
private static String[] file2 = new String[50];//输出进程二的文件
private static int num1 , num2;//输出进程的文件数
private String input1 , input2;
public SpoolingTest()
{
output1 = new Output( "输出进程1" );
output2 = new Output( "输出进程2" );
spo = new Spooling( "Spooling进程" );
input1 = JOptionPane.showInputDialog( "Input the times of user1's output file");
input2 = JOptionPane.showInputDialog( "Input the times of user1's output file");
num1 = Integer.parseInt( input1 );
num2 = Integer.parseInt( input2 );
}
public static boolean input( String s ) //将s插入存储井中,成功插入返回true,存储井满返回false.
{
if( list.getSize() <= 10 ) //存储井可放10个文件
{
list.insertAtBack( s );
return true;
}
else
return false;
}
public void run()
{
int i = 0;//输出进程一的文件计数器
int j = 0;//输出进程二的文件计数器
String first;//记录等待队列的第一个文件
System.out.println( "程序启动:" );
System.out.println( "\n" );
while( !(list.getSize() ==0 && i == num1 && j == num2) )
// 当存储井为空,计数器i,j分别等于输出进程一二的文件数,结束运行
{
double choose = Math.random(); //随机决定要运行进程
if( choose <= 0.45 )//运行输出进程一
{
if( i == num1 )
continue;
output1.setStatus( 1 );
spo.setStatus( 0 );
output2.setStatus( 0 );
System.out.println( output1.getName() + "运行. " + output2.getName() + "," + spo.getName() + "等待.");
System.out.println( "\n");
file1[i] = output1.produce();//产生输出进程一的一个文件
if( input( file1[i] ) ) //判断能否插入存储井
{
System.out.println("生成" + file1[i]+"放入存储井中.");
System.out.println( "\n");
}
else
{
wait.insertAtBack( file1[i] ); //存储井满,将该文件插入等待队列
System.out.println( "存储井满," + output1.getName() + "把第"+i+"个"+file1[i]+"插入等待队列.");
System.out.println( "\n");
output1.setStatus(0);
}
i++;//文件数计数器加一
}
if( choose > 0.45 && choose <= 0.9 )//运行输出进程二
{
if( j == num2 )
continue;
output2.setStatus( 1 );
spo.setStatus( 0 );
output1.setStatus( 0 );
System.out.println( output2.getName() + "运行. " + output1.getName() + "," + spo.getName() + "等待.");
System.out.println( "\n");
file2[j] = output2.produce();//产生输出进程一的一个文件
if( input( file2[j] ) ) //判断能否插入存储井
{
System.out.println("生成" + file2[j] + "放入存储井中.");
System.out.println( "\n");
}
else
{
wait.insertAtBack( file2[j] );//将该文件插入等待队列
System.out.println( "存储井满," + output2.getName() + "把第"+j+"个"+file2[j]+"插入等待队列.");
System.out.println( "\n");
output2.setStatus(0);
}
j++;//文件数计数器加一
}
if( choose > 0.9 )//运行Spooling进程
{
System.out.println( "Spooling进程运行." );
System.out.println( "" );
output2.setStatus( 0 );
output1.setStatus( 0 );
if( list.getSize() == 0 )
{
System.out.println("存储井为空!");
System.out.println( "\n");
spo.setStatus( -1 );//当存储井为空时,状态为-1
continue;
}
else
{
System.out.println( "向I/O设备输出" + list.removeFromFront() );
System.out.println( "\n" );
if( !( wait.isEmpty() ) )//判断是等待队列中是否有要插入存储井的文件
{
first = wait.removeFromFront();
System.out.println( "将等待队列中的第一个" + first + "放入存储井" );
list.insertAtBack( first );
}
else
System.out.println( "没有等待进入存储井的的文件!" );
System.out.println( "\n" );
}
}
}
System.out.println( "运行结束!!!" );
}
public static void main( String args[] )
{
SpoolingTest app = new SpoolingTest();
app.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -