public class TicketOffice implements Runnable {
    private int count = 100; // 记录剩余票数
    private int no = 0; // 记录买到第几张票
    private boolean flag = false; //记录是否售完
    public void run() {
        while (true) {
            sale();
            if(count<=0){
            break;
            }
        }
    }
    // 卖票
     public boolean sale() {
        if (flag==true) {
            return false;
        }
        // 第一步:设置买到第几张票和剩余票数
        no++;
        count--;
        try {
            Thread.sleep(5);
            // 模拟网络延时
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 第二步:显示信息
        System.out.println(Thread.currentThread().getName() + "抢到第" + no + "张票," + "剩余" + count + "张票!");
        if(count<=0){
            flag=true;
            return false;
        }
        if(Thread.currentThread().getName().equals("票贩子")){            //非同步的情况下。如何将票贩子这一线程停止??????
        }
        return true;
    }
}public class Test {
    public static void main(String[] args) {
        TicketOffice ticket =new TicketOffice();
        new Thread(ticket,"张三").start();
        new Thread(ticket,"李四").start();
        new Thread(ticket,"票贩子").start();
    }
}

解决方案 »

  1.   

    interrupt停止票贩这个线程
      

  2.   

    stop()方法,suspend()方法
      

  3.   

    public void run() {
    if(Thread.currentThread().getName().equals("票贩子")){
             return;          
    }
            while (true) {
                sale();
                if(count<=0){
                break;
                }
            }
        }
      

  4.   

    粗略看一下,要阻塞线程的话可以用 CountDownLatch
    https://www.cnblogs.com/flyme/p/4568063.html public class TicketOffice implements Runnable {
        private int count = 100; // 记录剩余票数
        private int no = 0; // 记录买到第几张票
        private boolean flag = false; //记录是否售完

        private CountDownLatch latch=new CountDownLatch(2);
        
        public void run() {
            while (true) {
                sale();
                if(count<=0){
    break;
                }
            }
        }    // 卖票
         public boolean sale() {
            if (flag==true) {
                return false;
            }
            // 第一步:设置买到第几张票和剩余票数
            no++;
            count--;
            try {
                Thread.sleep(5);
                // 模拟网络延时
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 第二步:显示信息
            System.out.println(Thread.currentThread().getName() + "抢到第" + no + "张票," + "剩余" + count + "张票!");
            if(count<=0){
                flag=true;
                return false;
            }

            if(Thread.currentThread().getName().equals("票贩子")){
    latch.await(); //非同步的情况下。如何将票贩子这一线程停止??????       
            }
            return true;
        }
    }public class Test {
        public static void main(String[] args) {
            TicketOffice ticket =new TicketOffice();
            new Thread(ticket,"张三").start();
            new Thread(ticket,"李四").start();
            new Thread(ticket,"票贩子").start();
        }
    }