您的位置:首页 > 编程语言 > Java开发

操作系统模拟公交车司机与售票员进程之间的协同关系

2017-12-19 13:33 323 查看
使用java多线程编写代码:

一.实验目的:

用两个进程来模拟汽车司机与售票员之间的协同关系:一方面只有售票员把车门关好了司机才能关门,因此,售票员关好车门应通知司机开车;另一方面,只有当汽车已经停下,售票员才能开门上下客,故司机停车后应通知售票员。汽车当前正在始发站停车上客,试设必要的信号量并赋初值,写出它们的同步过程。

二.实验思路:

建立两个线程driver表示汽车司机,conductor表示售票员,两个线程同时进行。信号变量bus和seller分别代表汽车和售票员。初始值分别为0和1。线程driver中将bus=0,seller=1;线程seller中将bus=1,seller=0。在t从0到10000时间内两个进程交替执行,从而模拟司机与售票员的协同关系。



三.实验结果:

The bus is starting from the start station

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 2 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 3 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 4 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 5 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 6 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 7 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 8 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 9 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 10 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 11 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 12 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

having reached the 13 bus stop

passengers go on the bus

The seller is selling the tickets

The seller tells the driver to close the door

The bus is running

Bus has reached the terminal station

End

 

四.实验源代码:

 

public class firsttask {

private static int bus=0;

private static int seller=1;

private static int t=0;//设置一个线程的执行时间

private static int is=1;

public static void main(String[]
args) {

Thread driver=new Thread(new Runnable() {

 

@Override

public void run() {

// TODO Auto-generated method stub

while(true) {

t++;

if(t==10000) {

System.out.println("The bus is running");

System.out.println("Bus has reached the terminal station");

bus=0;

seller=1;

System.out.println("End");

break;

}

if(bus==1) {

is++;

System.out.println("The bus is running");

System.out.println("having reached the "+
is+" bus stop");

System.out.println("passengers go on the bus");

bus=0;

seller=1;

}

}

}

});

Thread conductor=new Thread(new Runnable() {

 

@Override

public void run() {

while(true) {

if(t==10000) {

break;

}

if(seller==1) {

if(is==1) {

System.out.println("The bus is starting from the start station");

System.out.println("The seller is selling the tickets");

System.out.println("The seller tells the driver to close the door");

bus=1;

seller=0;

}

if(is!=1) {

System.out.println("The seller is selling the tickets");

System.out.println("The seller tells the driver to close the door");

bus=1;

seller=0;

}

}

}

}

});

driver.start();

conductor.start();

}

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息