Java:Thread

1、具体实现
  1. 创建线程类 MyThread 继承 Thread。
  2. 创建线程对象:MyThread thread1 = new MyThread()。
  3. 调用线程对象的 start() 方法。
public class ThreadDemo {

    public static void main(String[] args) {
        MyThread thread1 = new MyThread("线程一");
        MyThread thread2 = new MyThread("线程二");
        thread1.start();
        thread2.start();
    }

    static class MyThread extends Thread {

        // 100 张票
        private static int ticket = 100;

        public MyThread() {
            super();
        }

        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {

            while (ticket > 0) {
                ticket--;
                System.out.println(Thread.currentThread() + "卖掉了1张票,剩余票数为:" + ticket);
            }

        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章