-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicket.java
More file actions
29 lines (23 loc) · 783 Bytes
/
Ticket.java
File metadata and controls
29 lines (23 loc) · 783 Bytes
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
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Ticket implements Callable {
private int ticket = 1000;
@Override
public Object call() throws Exception {
while (true) {
synchronized (this) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket + "张票");
ticket--;
} else {
return null;
}
}
}
}
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(new FutureTask<Object>(ticket),"窗后1").start();
new Thread(new FutureTask<Object>(ticket),"窗后2").start();
}
}