zoukankan      html  css  js  c++  java
  • Thread 使用Runnable接口模拟实现4个窗口售票

    //copyright©liupengcheng
    //http://www.cnblogs.com/liupengcheng

    /**
    * Created by Administrator on 2014/10/23.
    *以下事例模拟4个窗口售100张票
    */

    // 创建线程的另一种方法是声明实现 Runnable 接口的类。该类然后实现 run 方法。然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。

    //copyright©liupengcheng
    //http://www.cnblogs.com/liupengcheng

    public class ThreadDemo5 {
        public static void main(String [] args){
            TestThread t = new TestThread();
            new Thread(t).start();
            new Thread(t).start();
            new Thread(t).start();
            new Thread(t).start();
        }
    }

    //copyright©liupengcheng
    //http://www.cnblogs.com/liupengcheng


    class TestThread implements Runnable{  //类调用Runnable 接口的类
        private int ticket = 100;
        public void run()
        {
            while (true)
            {
                if(ticket>0)
                    System.out.println(Thread.currentThread().getName()+"is sailing ticket"+ ticket--);
            }
        }
    }

    //copyright©liupengcheng
    //http://www.cnblogs.com/liupengcheng

  • 相关阅读:
    Linux
    CUDA开发
    Linux C++
    Python
    C++
    模式识别
    Linux
    算法
    算法
    leetcode 数组,链表,跳表
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/4045791.html
Copyright © 2011-2022 走看看