package com.yy.ticket;
import java.util.UUID;
import java.util.concurrent.*;
public class SellTicket {
public static void main(String[] args) {
// SellTicket01 sellTicket01 = new SellTicket01();
//
// SellTicket01 sellTicket02 = new SellTicket01();
//
// SellTicket01 sellTicket03 = new SellTicket01();
//
// //这里会出现超卖的情况
// sellTicket01.start();
//
// sellTicket02.start();
//
// sellTicket03.start();
// ExecutorService service = Executors.newFixedThreadPool(5);
// SellTicker02 sellTicker02 = new SellTicker02();
// service.execute(sellTicker02);
// service.execute(sellTicker02);
// service.shutdown();
// new Thread(sellTicker02).start();
// new Thread(sellTicker02).start();
// new Thread(sellTicker02).start();
Sellticket03 sellticket031 = new Sellticket03();
sellticket031.start(3);
// new Thread(sellticket031).start();
// new Thread(sellticket031).start();
// Sellticket04 sellticket04 = new Sellticket04();
// FutureTask task = new FutureTask(sellticket04);
// Thread thread = new Thread(task);
// thread.start();
// try {
// System.out.println("线程执行结果"+ task.get());
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// } catch (ExecutionException e) {
// throw new RuntimeException(e);
// }
}
}
class SellTicket01 extends Thread{
private static int tickerNum = 100;//多个线程共享
@Override
public void run() {
while (true){
if(tickerNum <= 0){
System.out.println("售票结束");
break;
}
//休眠50s
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("窗口:" + Thread.currentThread().getName() + "售出一张票" + ",余票 " + (--tickerNum));
}
}
}
class SellTicker02 implements Runnable{
private int tickerNum = 100;//多个线程共享
@Override
public void run() {
while (true){
synchronized (SellTicker02.class){
if(tickerNum <= 0){
System.out.println("售票结束");
break;
}
//休眠50s
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("窗口:" + Thread.currentThread().getName() + "售出一张票" + ",余票 " + (--tickerNum));
}
}
}
}
class Sellticket03 implements Runnable{
private int ticketNum = 50;
@Override
public void run() {
while (true){
synchronized (Sellticket03.class) {
if(ticketNum <= 0){
System.out.println("票已卖完");
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("线程" + Thread.currentThread().getName() + "卖出一张票,余票" + (--ticketNum));
}
}
}
public void start(int threadNum){
for (int i = 0; i < threadNum; i++) {
new Thread(this,"Sellticket03" + i).start();
}
}
}
class Sellticket04 implements Callable{
@Override
public Object call() throws Exception {
//执行线程业务的方法
for (int i = 0; i < 10; i++) {
//返回正在执行的线程实例
Thread thread = Thread.currentThread();
//getName()获取当前线程实例的名称
System.out.println(thread.getName()+"执行打印"+i);
}
//获取随机数
UUID uuid = UUID.randomUUID();
System.out.println("随机数字符串为:"+uuid);
return uuid;
}
}
class Sellticket05{
static int a=0;
static int count=2000;
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newCachedThreadPool();
//闭锁 在一些条件下可放开 参数:加多少把锁
CountDownLatch countDownLatch=new CountDownLatch(count);
for(int i=0;i<count;i++){
service.execute(new Runnable() {
@Override
public void run() {
synchronized (Sellticket05.class) {
a++;
//解一把锁
countDownLatch.countDown();
}
}
});
}
service.shutdown();
//会进入阻塞状态 什么时候把锁全解了 阻塞状态才会解除
countDownLatch.await();
System.out.println(a);
//2000
}
}
01-30
5749