-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClickListener.java
More file actions
108 lines (92 loc) · 2.74 KB
/
ClickListener.java
File metadata and controls
108 lines (92 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package Thread.monitor;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;
/**
* Created by szh on 2017/5/12.
*/
public class ClickListener implements Runnable {
private Queue<Integer> queue =new ConcurrentLinkedDeque<>();
@Override
public void run() {
// queue.add(this.getGotoNum()); // 将按得楼层添加到queue
while (queue.size() !=0){
System.out.println(queue.size()+"queue的size");
int num =queue.poll();
if (num > this.getId()){
try {
goUp(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else{
try {
goDown(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private int id;
private boolean isRun;
private int gotoNum;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGotoNum() {
return gotoNum;
}
public void setGotoNum(int gotoNum) {
this.gotoNum = gotoNum;
}
public boolean isRun() {
return isRun;
}
public void setRun(boolean run) {
isRun = run;
}
public ClickListener(int id) {
this.id = id;
}
//该构造器方便在电梯运行的时候调整楼数
public ClickListener() {
this.id=0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private void goUp(int num) throws InterruptedException {
isRun =true;
while (isRun) {
System.out.println("当前电梯" +this.getName()+":::::当前线程"+ Thread.currentThread().getName() + "位于" + this.getId() + "正在往" + num + "运行");
Thread.currentThread().sleep(1000);
this.id = this.id + 1;
if (this.id == num){
System.out.println("到达第" + num + "层");
isRun =false;
}
}
}
private void goDown(int num) throws InterruptedException {
isRun=true;
while (isRun) {
System.out.println("当前电梯"+this.getName()+":::::当前线程" + Thread.currentThread().getName() + "位于" + this.getId() + "正在往" + num + "运行");
Thread.currentThread().sleep(1000);
this.id = this.id - 1;
if (this.id == num){
System.out.println("到达第" + num + "层");
isRun =false;
}
}
}
public Queue<Integer> getQueue() {
return queue;
}
}