zoukankan      html  css  js  c++  java
  • 多线程的两种方法(卖票系统展示)

    public  class MyThread1 implements Runnable{
        
        int i=20;
     String name;
        public MyThread1() {
            // TODO 自动生成的构造函数存根
            this.name=name;
        }
    
    
    
    
        public void run(){
            
            for(int x=0;x<20;x++){
                if(i>0){
                    System.out.print(Thread.currentThread().getName()+"    ");
                    System.out.println("售票窗口:"+"  "+"余票	"+i--);
                    
                }
            }
        }
    }
     class Test{
        public static void main(String []args){
            
            MyThread1 myth1=new MyThread1();//创建线程对象
            
            new Thread(myth1,"线程1").start();
            
            new Thread(myth1,"线程2").start();
            
            new Thread(myth1,"线程3").start();
    //        myth1.start();
    //        myth1.start();
    //        myth1.start();
        }
    }
    ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
    20张票的输出结果:为三个线程共卖出20张票

    public class MyThread extends Thread{
    //String name;
    int i=20;
    
    public void run (){
        for(int x =0;x<20;x++){
            if(i>0){
                System.out.println("卖票"+"     "+"余票	="+i--);
            }
        }
    }
    ////
    }
    //创建了三个线程,每个线程售出20张票
     class TestDemo{
        public static void main(String []args){
        MyThread myth1=new MyThread();
        MyThread myth2=new MyThread();
        MyThread myth3=new MyThread();
        myth1.start();
        myth2.start();
        myth3.start();
        
        }
    }
    ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
    20张票的输出结果为:三个线程每个卖出20张,共卖出60张票

  • 相关阅读:
    工具
    BZOJ 1202: [HNOI2005]狡猾的商人
    BZOJ 4562: [Haoi2016]食物链
    BZOJ 1922: [Sdoi2010]大陆争霸
    BZOJ 2429: [HAOI2006]聪明的猴子
    BZOJ 1083: [SCOI2005]繁忙的都市
    BZOJ 1012: [JSOI2008]最大数maxnumber
    Luogu P1078 文化之旅
    快速读入
    [HNOI2014]道路堵塞
  • 原文地址:https://www.cnblogs.com/-strong/p/7157342.html
Copyright © 2011-2022 走看看