import java.io.*;
import java.util.List;
import java.util.Random;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Iterator;
import java.sql.*;
import java.lang.*;
public class BankDemo
{
public static int numberOfCounter=10;//假定柜台数
public static int numberOfPeople=1000;//假定用户数
public static int addUserScope=200;
public List AccountList;//银行账户链表
public List CounterList[]=new List[numberOfCounter];//队列链表;这里以后改为10
public BankCounterThread threadNum[]=new BankCounterThread[numberOfCounter];//每个柜台处理线程;这里以后改为10
public CreateUserThread createThread;//产生随机用户线程
public Object obj;
public BankDemo()
{
obj=new Object();
}
public static void main(String args[])
{
BankDemo bank=new BankDemo();
bank.InitialList();
bank.InitialThread();
synchronized(bank.obj)
{
try
{
(bank.obj).wait();
}
catch(InterruptedException e)
{
}
}
try
{
Thread.sleep(30000);
}
catch(InterruptedException e)
{
}
bank.TerminateThread();
System.out.println("********************"+"\n"+"银行处理完毕!"+"\n"+"********************");
}
protected void TerminateThread()
{
for(int i=0;i<numberOfCounter;i++)
{
threadNum[i].stop();
}
}
protected void InitialList()
{
AccountList =Collections.synchronizedList(new LinkedList());
for(int j=0;j<numberOfCounter;j++)//这里以后改为10
{
CounterList[j]=Collections.synchronizedList(new LinkedList());
}
}
protected void InitialThread()
{
for(int i=0;i<numberOfCounter;i++)//这里以后改为10
{
threadNum[i]=new BankCounterThread("柜台"+Integer.toString(i)+"营业线程启动!",i,this);
threadNum[i].start();
}
CreateUserThread createThread=new CreateUserThread("创建用户线程启动!",this);
createThread.start();
}
}
class CreateUserThread extends Thread
{
Random rand;
int userID=0;
int operateState=0;//0:存;1:取
int amountOfMoney=0;
BankDemo bankInCreate;
public CreateUserThread(String userName,BankDemo bankInCreate)
{
super(userName);
System.out.println(userName);
this.bankInCreate=bankInCreate;
rand=new Random();
}
public void run()
{
Iterator iter;
UserRandomClass newUser;
UserRandomClass tempRamUser;
boolean RemFlag=false;//false:没有相同用户;true:有相同用户
int lengthLine[]=new int[bankInCreate.numberOfCounter];//各个柜台的长度,此处以后改为10个0
for(int n=0;n<bankInCreate.numberOfCounter;n++)
{
lengthLine[n]=0;
}
int minLength=0;//最少人数
int numOfLine=0;//第几个柜台前人数最少
for(int i=0;i<bankInCreate.numberOfPeople;i++)//以后这里改为1000
{
newUser=new UserRandomClass();
newUser.SetUserID(10000+rand.nextInt(bankInCreate.addUserScope));
newUser.SetOperateState(rand.nextInt(2));
newUser.SetAmountOfMon(2000+rand.nextInt(3000));
RemFlag=false;
//查询有无相同用户
for(int j=0;j<bankInCreate.numberOfCounter;j++)//此处以后改为10
{
synchronized(bankInCreate.CounterList[j])
{
lengthLine[j]=bankInCreate.CounterList[j].size();
iter=bankInCreate.CounterList[j].iterator();
while(iter.hasNext())
{
tempRamUser=(UserRandomClass)iter.next();
if(newUser.GetUserID()==tempRamUser.GetUserID())
{
RemFlag=true;
break;
}
}
}
if(RemFlag)
{
break;
}
}
//比较队列长度
if(!RemFlag)
{
minLength=lengthLine[0];
numOfLine=0;
for(int m=0;m<bankInCreate.numberOfCounter;m++)//此处以后改为10
{
if(minLength>lengthLine[m])
{
minLength=lengthLine[m];
numOfLine=m;
}
}
synchronized(bankInCreate.CounterList[numOfLine])
{
bankInCreate.CounterList[numOfLine].add(newUser);
}
}
else
{
i-=1;
}
}//for用户产生完成
synchronized(bankInCreate.obj)
{
bankInCreate.obj.notify();
}
}//run
}
class BankCounterThread extends Thread
{
private Random randTime;
int counterNum;
BankDemo bankInCounter;
public BankCounterThread(String SeviceNumber,int counterNum,BankDemo bankInCounter)
{
super(SeviceNumber);
this.counterNum=counterNum;
this.bankInCounter=bankInCounter;
System.out.println(SeviceNumber);
randTime=new Random();
}
public void run()
{
UserRandomClass firstUserAccount;
Iterator iter;
int userID=0;
int amountOfMoney=0;
int operateState=0;
BankAccountClass accountOfBank;
boolean accountAddFlag=false;//false表示储户为新储户,true表示储户已存在
BankAccountClass addAccountClass;
while(true)
{
//从队列中取出第一个用户信息
synchronized(bankInCounter.CounterList[counterNum])
{
iter=bankInCounter.CounterList[counterNum].iterator();
if(iter.hasNext())
{
firstUserAccount=(UserRandomClass)iter.next();
userID=firstUserAccount.GetUserID();
amountOfMoney=firstUserAccount.GetAmountOfMon();
operateState=firstUserAccount.GetOperateState();
iter.remove();//已取得用户信息,从队列中删除
}
else //队列无用户等待10微秒
{
try
{
sleep(10);
}
catch(InterruptedException e)
{
}
continue;
}
}
//银行处理业务延迟5微秒
try
{
sleep(50+randTime.nextInt(450));
}
catch(InterruptedException e)
{
}
//对比银行储户信息
- 1
- 2
- 3
- 4
- 5
- 6
前往页