多线程购票

package test;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class Main{
    public static void main(String args[ ]){
        String s1="售票口1";
        String s2="售票口2";
        String s3="售票口3";
        Boxoffice office=new Boxoffice(s1,s2,s3);
        Thread a=new Thread(office);
        Thread b=new Thread(office);
        Thread c=new Thread(office);
        a.setName(s1);
        b.setName(s2);
        c.setName(s3);
        a.start();
        b.start();
        c.start();

    }
}
class  Boxoffice implements Runnable{
    String name1,name2,name3;
    int total=10;
        Boxoffice(String s1,String s2,String s3){
            this.name1=s1;
            this.name2=s2;
            this.name3=s3;
        }
        public   void run(){
            while(total>1){


            sellTicket();
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {

            }   
            }
        }



        synchronized void sellTicket(){
                if(Thread.currentThread().getName().equals(name1))
                {   
                    total--;
                    System.out.println(name1+"卖掉了一张"+"还剩下"+total+"张");



                }
                else if(Thread.currentThread().getName().equals(name2))
                {   
                    total--;
                    System.out.println(name2+"卖掉了一张"+"还剩下"+total+"张");


                }
                else if(Thread.currentThread().getName().equals(name3))
                {   
                    total--;
                    System.out.println(name3+"卖掉了一张"+"还剩下"+total+"张");


                }
            }



}

还要看看这个的
多线程购票

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值