Skip to content

Commit f15677f

Browse files
committed
关于电梯调度的多线程demo初步以及上次的一些修改
1 parent 6004b9d commit f15677f

7 files changed

Lines changed: 72 additions & 12 deletions

File tree

src/main/java/Thread/Bank/QueuingNum1.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import Thread.Bank.system.DoCall;
44

5-
import javax.print.Doc;
6-
import java.util.ArrayList;
7-
import java.util.List;
5+
86

97
/**
108
* Created by szh on 2017/5/8.

src/main/java/Thread/Bank/system/BankSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void doSpend() {
1313
if (Store.queue.isEmpty()) {
1414
executor.shutdownNow();
1515
} else {
16-
executor.execute(new PopWindows()); // 执行操作任务
16+
executor.execute(new ServiceWindows(Store.queue.poll())); // 执行操作任务
1717
}
1818
}
1919
}

src/main/java/Thread/Bank/system/InitBank.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,21 @@ private void init(){
3030
while (!Store.queue.isEmpty()){
3131
// System.out.println("队列是否为空+!"+!Store.queue.isEmpty());
3232
// bankSystem.doSpend();
33-
this.executorService.execute(new PopWindows());
33+
this.executorService.execute(new ServiceWindows(Store.queue.poll()));
3434
}
3535
// System.out.println("已经执行到这里了");
3636

3737
}
3838
public static void main(String args[]) throws InterruptedException {
3939
InitBank initBank =new InitBank();
4040
initBank.init();
41-
Thread.sleep(1000);
42-
if (Store.queue.isEmpty())
43-
initBank.executorService.shutdownNow();
44-
41+
Thread.currentThread().sleep(20000);// 需要设置长时间,否则导致main方法结束后的下面方法结束了线程池
42+
// if (Store.queue.isEmpty()) {
43+
// try {
44+
// initBank.executorService.shutdownNow();
45+
// } catch (java.lang.NullPointerException e) {
46+
// System.out.println("一经被捕捉");
47+
// }
48+
// }
4549
}
4650
}

src/main/java/Thread/Bank/system/PopWindows.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ public void run() {
1717
}
1818

1919
public void spend() throws InterruptedException {
20-
int nowNUm =Store.queue.poll();
21-
System.out.println("线程:" + Thread.currentThread().getName() + "正在为" + nowNUm + "服务");
22-
Thread.currentThread().sleep(1000);
20+
if(!Store.queue.isEmpty()) {
21+
int nowNUm = Store.queue.poll();
22+
System.out.println("线程:" + Thread.currentThread().getName() + "正在为" + nowNUm + "服务");
23+
Thread.currentThread().sleep(1000);
24+
}else{
25+
System.out.println("当前一经没有客户");
26+
}
2327
}
2428
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Thread.elevator;
2+
3+
/**
4+
* Created by szh on 2017/5/10.
5+
*/
6+
public class EevatorControll {
7+
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Thread.elevator;
2+
3+
/**
4+
* Created by szh on 2017/5/10.
5+
*/
6+
public class Elevator {
7+
private int NowNum;
8+
9+
public Elevator() {
10+
}
11+
12+
public int getNowNum() {
13+
return NowNum;
14+
}
15+
16+
public void setNowNum(int nowNum) {
17+
NowNum = nowNum;
18+
}
19+
public void elevatorUp(){
20+
if(this.NowNum == 10){
21+
System.out.println("已经到达最高层");
22+
return;
23+
}
24+
this.NowNum=this.NowNum +1;
25+
}
26+
public void elevatorDown(){
27+
if(this.NowNum == 0){
28+
System.out.println("已到达最底层");
29+
return;
30+
}
31+
this.NowNum=this.NowNum - 1;
32+
}
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Thread.elevator;
2+
3+
/**
4+
* Created by szh on 2017/5/10.
5+
*/
6+
public class ElevatorTest {
7+
public static void main(String args[]){
8+
Elevator elevator =new Elevator();
9+
elevator.setNowNum(10);
10+
elevator.elevatorDown();
11+
System.out.println(elevator.getNowNum());
12+
}
13+
}

0 commit comments

Comments
 (0)