黑马程序员技术交流社区

标题: 多线程问题:为什么我设置了名字在打印时却不是我写的名字那 [打印本页]

作者: 公孙国禄    时间: 2016-3-26 00:06
标题: 多线程问题:为什么我设置了名字在打印时却不是我写的名字那
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;
                                }       
                        }
               
                }
        }
      




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2