import java.util.concurrent.locks.ReentrantLock; /** * PrivateRyan * 2021.11.5 * 线程池的使用 */ public class Window78 implements Runnable{ private int ticket=100; private ReentrantLock lock=new ReentrantLock(); @Override public void run() { Thread.currentThread().setName("宝贵线程"); while (true){ try { lock.lock(); if (ticket>0){ try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+ticket); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } ticket--; }else { break; } }finally { lock.unlock(); } } } }
线程池基础练习2
最新推荐文章于 2024-06-25 21:35:06 发布