1. 程式人生 > >java 多執行緒基礎之銀行取號排隊系統

java 多執行緒基礎之銀行取號排隊系統

練習:銀行取號排隊
規則:銀行有四個視窗 1個vip視窗,只能接待vip顧客,另外三個視窗優先接待vip客戶

這裡對於執行緒的一些基礎介紹我就不累贅了

主要講講銀行排隊執行緒要注意哪些點?

1.肯定要寫一個客戶實體類,主要存放客戶的票號和客戶的種類(普通或VIP)

2.寫一個客戶的連結串列類,單鏈表足以,迴圈也行

3.有規則知有兩種視窗,你可以寫兩個runable介面來分別實現相應視窗,也可以寫一個在裡面進行判斷(注意推薦通過繼承Runnable介面來實現run()方法)

好了,不多說了,上程式碼


public class Test01 {
public static void main(String[] args){
St  st = new St();
Custm[] cus = new Custm[20];
//int[] ran = {1,1,1,0,1,0,1,1,1,0,1};
int[] ran = {1,0,1,1,0,1,0,1,0,1,1};
for(int i=0;i<20;i++){
int a = (int)(Math.random()*10);
cus[i] = new Custm(i+1,ran[a]);
st.add(cus[i]);

}
Worke wort = new Worke(st, "vip", true);//vip視窗,執行緒1
Thread t1 = new Thread(wort);
t1.start();

Worke wort1 = new Worke(st, "1號" , false);//普通視窗,執行緒2,
Thread t2 = new Thread(wort1);
t2.start();

Worke wort2 = new Worke(st, "2號" , false);//普通視窗,執行緒3,
Thread t3 = new Thread(wort2);
t3.start();
Worke wort4 = new Worke(st, "3號" , false);//普通視窗,執行緒4,
Thread t4 = new Thread(wort4);
t4.start();


}
}
class Custm{
int key;
int num;
public Custm(int n, int k){
num=n;
key=k;
}
}


class base {

}
class Worke implements Runnable{
boolean isvip;
St s;
String ThreadName;
public Worke(St ss,String n ,boolean vip){
s=ss;
ThreadName = n;
isvip = vip;
}
boolean asd=true;
public void run(){
while(asd){
if(s.isEmpty()==false){

if(isvip){
//synchronized(this){

Custm cu = s.first().cus;
if(cu.key==0){
s.dell();
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗無人,請"+cu.num+"號【vip】顧客準備!");
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【vip】顧客就緒!");
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【vip】顧客正在辦理!");
try {
Thread.sleep((int)(Math.random()*500));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【vip】顧客已離開-------!");

//}else{

//try {
//Thread.sleep((int)(Math.random()*500));
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//}
//}
}

}else{
//synchronized(this){

Custm cu = s.first().cus;
if(cu.key==0){
s.dell();
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗無人,請"+cu.num+"號【vip】顧客準備!");
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【vip】顧客就緒!");
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【vip】顧客正在辦理!");
try {
Thread.sleep((int)(Math.random()*500));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【vip】顧客已離開-------!");
}else{
s.dell();
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗無人,請"+cu.num+"號【普通】顧客準備!");
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【普通】顧客就緒!");
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【普通】顧客正在辦理!");
try {
Thread.sleep((int)(Math.random()*500));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("執行緒id" + Thread.currentThread().getId() +"\t"+ThreadName+"---視窗"+cu.num+"號【普通】顧客已離開-------!");

}
//}

}
}else{
asd=false;
}

}
}
}
class St{
Node head;
Node tail;
public St(){
tail=head=null;
}
public boolean isEmpty(){
if(head==null){
return true;
}
return false;
}

public void add(Custm c){
Node node = new Node(c);
if(isEmpty()){
head=tail=node;
head.front=null;
tail.next=null;
}else{
tail.next=node;
node.front=tail;
tail=node;
tail.next=null;
}
}
public void dell(){
if(isEmpty()==false){
if(head.next==null){
Node nod=head;
head=null;
//return nod;
}else{
Node nod=head;
head=head.next;
head.front=null;
//return nod;
}
}else{
System.out.println("佇列空了");
//return null;
}
}
public Node first(){
if(isEmpty()==false){
return head;
}else{
System.out.println("佇列空了");
return null;
}
}

public Node  del(){
if(isEmpty()==false){
Node nod = findvip();
if(nod==null){

Node temp=head;
if(head.next!=null){
Node node = head.next;
node.front=null;
head=node;
}else{
head=null;
}
//head.front=null;
temp.next=null;
return temp;
}else{
if(nod==head){
nod=head;
head=null;
return nod;
}else if(nod==tail){
Node tm = nod;
nod.front.next=null;
return tm;
}else{
nod.front.next=nod.next;
nod.next.front=nod.front;
return nod;
}
}
}else{
System.out.println("佇列空了");
return null;
}
}

public Node findvip(){
if(isEmpty()==false){
Node temp = head;
while(temp!=null){
if(temp.cus.key==0){
return temp;
}
temp = temp.next;
}
}
return null;
}



}
class Node{
Custm cus;
Node front;
Node next;
public Node(Custm c){
cus =c;
front = next = null;
}
}