java多线程应用

来源:百度知道 编辑:UC知道 时间:2024/05/28 03:37:25
创建一个程序,利用多线程模拟银行的排号器的工作过程,即利用多个线程模拟客户到排号器上取号的过程。
提示:设计一个类用于产生号码,设计多个线程模拟用户得到号码。要采用两种办法实现。
提前谢谢回答者!

import java.lang.Thread;
import java.util.*;
class Test extends Thread{
static NumberMachine machine=new NumberMachine();
public void run(){

int x=0;
try{

for(;;){
x=(int)(Math.random()*5000);
sleep(x);
System.out.println("打印出第"+machine.printNumber()+"张号码");

}
}catch(Exception e){
e.printStackTrace();
System.out.println("消息:"+e.getMessage());
}
}
public static void main(String[] str){

Test t=new Test();
int x=0;
t.start();
try{
sleep(1000);
for(;;){
x=(int)(Math.random()*2000);
sleep(x);
if(machine.getMax()<=machine.getMin()) System.out.println("暂时没有客户来办理业务");
else System.out.println(machine.finish()+"号顾客办理结束");

}
}catch(Exception e){
e.print