A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Text {
  public static void main(String[] args) {
          //创建线程执行目标类对象
         Rung runnable=new Rung();

        Thread thread= new Thread(runnable,"zhang");
        Thread thread2= new Thread(runnable,"niaho");
        Thread thread3= new Thread(runnable,"llao");
         //创建线程池
    ExecutorService exector= Executors.newFixedThreadPool(3);
    exector.submit(runnable);
    exector.submit(thread2);
    exector.submit(thread3);
   
   }
public class Rung01 implements Runnable {
     public int number=100;
        @Override
        public  void run() {
                Thread thread = new Thread();
                while(true){
                        try {
                                thread.sleep(20);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        synchronized(this){
                                if(number>0){
                               
                                        System.out.println(thread.currentThread().getName()+"正在销售第"+number+"张票");
                                        number--;
                                }else{
                                        System.out.println("今天的票卖完了");
                                        break;
                                }       
                        }
               
                }
        }
      

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马