這個程序和前一個又不是相似之處
所以在看張孝祥老師的視頻之前自己先試著做了一下
結果一做不要緊
從下午
:
多做到晚上快十點了
速度很慢
主要是設計經驗不足
很多東西都得摸索
還有些不知道該怎麼選擇
但所幸最終還是做出來了
主要用到三個類
WIndow
Consumer
BankSystem
Window:
主要用於對六個窗口的描述
裡面提供了private String type;private String index;private boolean busy = false;這些成員變量
還有work()
及stop()方法
Consumer:
主要是提供了一個增加元素的方法addConsumer()
同時提供三個集合裝三種Consumer
BankSystem:
主調類
分別new了六個Window對象
和三個Consumer對象
所以相當於開啟了
個線程
BankSystem
import java
util
*;import java
util
Random;import ncurrent
ExecutorService;import ncurrent
Executors;public class BankDispatch { ArrayList<Window> windowLiabrary = Window
windowLiabrary; Iterator<Window> windowIterator ; Iterator<String> consumerIterator ; Window window ; Consumer consumer;
public static void main(String[] args) {
new BankDispatch()
}
public BankDispatch(){
new Consumer(
ordinary
)
new Consumer(
express
)
new Consumer(
VIP
)
new Window(
ordinary
)
new Window(
ordinary
)
new Window(
ordinary
)
new Window(
ordinary
)
new Window(
express
)
new Window(
VIP
)
while(true){
try {
Thread
sleep(
)
} catch (InterruptedException e) {
e
printStackTrace()
}
} }
}Window
package com
renren
interview;import java
util
ArrayList;import java
util
Random;import ncurrent
Executors;import ncurrent
ScheduledExecutorService;import ncurrent
TimeUnit;public class Window { public static ArrayList<Window> windowLiabrary = new ArrayList<Window>()
private String type; private String index;
private boolean busy = false;
int ordinaryIndex
expressIndex
VIPIndex;
public Consumer consumer;
public int time;
/* (non
Javadoc) * @see java
lang
Object#hashCode() */ @Override public int hashCode() { final int prime =
; int result =
; result = prime * result + ((index == null) ?
: index
hashCode())
result = prime * result + ((type == null) ?
: type
hashCode())
return result; } @Override public boolean equals(Object obj) { if (this == obj)
return true; if (obj == null)
return false; if (getClass() != obj
getClass())
return false; Window other = (Window) obj; if (index == null) {
if (other
index != null)
return false; } else if (!index
equals(other
index))
return false; if (type == null) {
if (other
type != null)
return false; } else if (!type
equals(other
type))
return false; return true; } public String getType(){
return type;
}
public String getIndex(){
return index;
}
public Window(String type
String index) { this
type = type; this
index = index; if(type
equals(
ordinary
) || type
equals(
VIP
)){
deal(
)
} else if(type
equals(
express
)){
deal(
)
} }
public void deal(int delay){
ScheduledExecutorService timer = Executors
newScheduledThreadPool(
)
timer
scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
//System
out
println(Consumer
ordinaryLibrary
size())
if(consumer!=null){
stop(consumer
time)
}
if(!busy ){
if(type
equals(
ordinary
) && Consumerntains(new Consumer(
ordinary
)) ){
work(consumer=Consumer
ordinaryLibrary
remove(
))
}
else if(type
equals(
express
)){
if(Consumentains(new Consumer(
express
))){
work(consumer=Consumer
expressLibrary
remove(
))
}
else
work(consumer = Consumer
ordinaryLibrary
remove(
))
}
else if(type
equals(
VIP
)){
if(Conntains(new Consumer(
VIP
))){
work(consumer = Consumer
VIPLibrary
remove(
))
}
else
work(consumer = Consumer
ordinaryLibrary
remove(
))
}
}
}
}
time = (new Random()
nextInt(delay)+
)
TimeUnit
SECONDS)
}
public boolean isBusy(){
return busy; }
public void work(Consumer consumer){
busy = true;
System
out
println(index+
號普通窗口正在為
+consumer
index+
號普通客戶服務
)
}
public void stop(Consumer consumer
int time){
busy = false;
System
out
println((index+
號普通窗口為
+consumer
index+
號普通客戶服務完成
耗時
+time+
秒
))
//System
out
println(this
type+
window
+this
index+
is not busy
)
}}Consumer
se; } else if (!index
equals(other
index))
return false; if (type == null) {
if (other
type != null)
return false; } else if (!type
equals(other
type))
return false; return true; } public String getType(){
return type;
}
public String getIndex(){
return index;
}
public Window(String type
String index) { this
type = type; this
index = index; if(type
equals(
ordinary
) || type
equals(
VIP
)){
deal(
)
} else if(type
equals(
express
)){
deal(
)
} }
public void deal(int delay){
ScheduledExecutorService timer = Executors
newScheduledThreadPool(
)
timer
scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
//System
out
println(Consumer
ordinaryLibrary
size())
if(consumer!=null){
stop(consumer
time)
}
if(!busy ){
if(type
equals(
ordinary
) && Consumerntains(new Consumer(
ordinary
)) ){
work(consumer=Consumer
ordinaryLibrary
rem
package com
renren
interview;import java
util
ArrayList;import java
util
Random;import ncurrent
ExecutorService;import ncurrent
Executors;import ncurrent
ScheduledExecutorService;import ncurrent
TimeUnit;public class Consumer { public static int ordinaryIndex
expressIndex
VIPIndex; private String type; public static ArrayList<Consumer> ordinaryLibrary = new ArrayList<Consumer>()
public static ArrayList<Consumer> expressLibrary = new ArrayList<Consumer>()
public static ArrayList<Consumer> VIPLibrary = new ArrayList<Consumer>()
public ArrayList<Consumer> consumerLibrary
; public int index; public Consumer(String type){
this
type = type;
if(type
equals(
ordinary
)){
System
out
println((++ordinaryIndex)+
號普通客戶等待服務
)
index = ordinaryIndex;
addConsumer(
ordinaryLibrary)
}
else if(type
equals(
express
)){
System
out
println((++expressIndex)+
號快速客戶等待服務
)
index = expressIndex;
addConsumer(
expressLibrary)
}
else if(type
equals(
VIP
)){
System
out
println((++VIPIndex)+
號VIP客戶等待服務
)
index = VIPIndex;
addConsumer(
VIPLibrary)
}
}
public void addConsumer(final int delay
ArrayList<Consumer> consumerLibrary){
nsumerLibrary
= consumerLibrary;
ExecutorService pool = Executors
newSingleThreadExecutor()
pool
execute(new Runnable() {
@Override
public void run() {
for(int i=
;i<
;i++){
try {
Thread
sleep(/*(new Random()
nextInt(delay)+
)*/delay*
)
consumerLibrary
add(new Consumer(type))
System
out
println(ordinaryLibrary
size())
} catch (InterruptedException e) {
// TODO Auto
generated catch block
e
printStackTrace()
}
}
}
})
}/* (non
Javadoc) * @see java
lang
Object#hashCode() */@Overridepublic int hashCode() { final int prime =
; int result =
; result = prime * result + ((type == null) ?
: type
hashCode())
return result;}/* (non
Javadoc) * @see java
lang
Object#equals(java
lang
Object) */@Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj
getClass()) return false; Consumer other = (Consumer) obj; if (type == null) { if (other
type != null)
return false; } else if (!type
equals(other
type)) return false; return true;}
}
但最後的執行效果還是稍微不太滿意
當然看了張孝祥老師寫的代碼
還是學的了很多
尤其是其中分析問題的思路
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26466.html