import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class BankQueueThread {
private static List<CustomerInfo> queue = Collections
.synchronizedList(new ArrayList<CustomerInfo>());
private static List<String> vipqueue = Collections
.synchronizedList(new ArrayList<String>());
private static int num = 0;
private static int rnum = 0;
private SimpleDateFormat datef = new SimpleDateFormat("HH:mm");
public static void main(String args[]) {
BankQueueThread bankQueueThread = new BankQueueThread();
AddUser adduser = bankQueueThread.new AddUser();
Thread th = new Thread(adduser);
th.start();
for (int i = 0; i < 3; i++) {
OrderNormalQueue orn = bankQueueThread.new OrderNormalQueue();
th = new Thread(orn);
th.start();
}
OrderVIPQueue orv = bankQueueThread.new OrderVIPQueue();
th = new Thread(orv);
th.start();
}
// 增加普通用户排队
public synchronized void addUser(int nums) {
String s = "" + (++num);
CustomerInfo customerInfo = new CustomerInfo();
customerInfo.setType("普通");
customerInfo.setId(s);
customerInfo.setFtime(nums);
customerInfo.setStime(datef.format(new Date()));
queue.add(customerInfo);
}
// 增加VIP排队
public synchronized void addVipUser(int nums) {
String s = "" + (++num);
CustomerInfo customerInfo = new CustomerInfo();
customerInfo.setType("VIP");
customerInfo.setId(s);
customerInfo.setFtime(nums);
customerInfo.setStime(datef.format(new Date()));
queue.add(customerInfo);
vipqueue.add(s);
}
// 普通窗口假设处理业务
public synchronized void removieUser() {
CustomerInfo cus = null;
if (queue.size() > 0) {
rnum++;
cus = (CustomerInfo) queue.remove(0);
if (vipqueue.size() > 0 && vipqueue.get(0).equals(cus.getId())) {
vipqueue.remove(0);
System.out.println(Thread.currentThread().getName() + ":>> "
+ rnum + " " + cus.getId() + " " + cus.getType() + " "
+ cus.getStime() + " " + cus.getFtime() + " <<办理业务完毕");
} else {
System.out.println(Thread.currentThread().getName() + ":>> "
+ rnum + " " + cus.getId() + " " + cus.getType() + " "
+ cus.getStime() + " " + cus.getFtime() + " <<办理业务完毕");
}
}
}
// VIP窗口假设处理业务
public synchronized void removieVipUser() {
String d = "";
CustomerInfo cus = null;
if (vipqueue.size() > 0) {
rnum++;
d = vipqueue.remove(0);
for (int i = 0; i < queue.size(); i++) {
cus = (CustomerInfo) queue.get(i);
if (cus.getId().equals(d)) {
queue.remove(i);
System.out.println(Thread.currentThread().getName()
+ ":>> " + rnum + " " + cus.getId() + " "
+ cus.getType() + " " + cus.getStime() + " "
+ cus.getFtime() + " <<办理业务完毕");
break;
}
}
} else if (queue.size() > 0) {
rnum++;
cus = (CustomerInfo) queue.remove(0);
System.out.println(Thread.currentThread().getName() + ":>> " + rnum
+ " " + cus.getId() + " " + cus.getType() + " "
+ cus.getStime() + " " + cus.getFtime() + " <<办理业务完毕");
}
}
// 用户排队
class AddUser implements Runnable {
Random rd = new Random();
int sd = 0;
public void run() {
// TODO Auto-generated method stub
while (true) {
sd = rd.nextInt(10);
if (sd == 6) {
addVipUser(sd);
} else {
addUser(sd);
}
if (queue.size() >= 100) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// 普通窗口业务受理
class OrderNormalQueue implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (true) {
removieUser();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// VIP窗口业务受理
class OrderVIPQueue implements Runnable {
public void run() {
// TODO Auto-generated method stub
while (true) {
removieVipUser();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class CustomerInfo {
private String stime;
private String id;
private String type;
private int ftime;
public String getStime() {
return stime;
}
public void setStime(String stime) {
this.stime = stime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getFtime() {
return ftime;
}
public void setFtime(int ftime) {
this.ftime = ftime;
}
}
}