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

  • 相关阅读:
    select接收后台返回值的解决方案
    junit 使用
    jsoup解析HTML及简单实例
    面向接口编程及适配器模式
    数据库 SQL语句小结(更新中)
    最近关于虚拟机的学习
    AQS源码解析
    ReentrantLock源码阅读记录(二)之ReentrantReadWriteLock
    Semaphore 源码解读
    Httpclient学习多一点儿
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/4045791.html
Copyright © 2011-2022 走看看