进阶day06-线程安全-卖票案例的实现
This commit is contained in:
24
javaSE-day06/src/com/inmind/thread_safe04/TicketTask.java
Normal file
24
javaSE-day06/src/com/inmind/thread_safe04/TicketTask.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.inmind.thread_safe04;
|
||||
|
||||
public class TicketTask implements Runnable{
|
||||
//定义100张票电影票
|
||||
int tickeCount = 100;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//有票就卖
|
||||
while (true) {
|
||||
if (tickeCount > 0) {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(Thread.currentThread().getName() + "正在卖第" + tickeCount + "张电影票");
|
||||
tickeCount--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user