import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
-
Description
-
@Author blindore
-
@Date Created in 2021-7-1 9:59
-
@Version 2.10.6
*/
public class TestLock {
public static void main(String[] args) {
Tickets td = new Tickets();
new Thread(()->{td.sale();},“窗口1”).start();
new Thread(()->{td.sale();},“窗口2”).start();
new Thread(()->{td.sale();},“窗口3”).start();
}
}
class Tickets {
private Lock lock = new ReentrantLock();//创建lock锁
private int ticket = 3000;
public void sale() {
while (true) {
lock.lock();//上锁
try {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + “完成售票,余票为:” + (–ticket));
}
}finally {
lock.unlock();//释放锁
}}
}
}