阅读背景:

线程实现方式一 、通过继承Thread类本身_promiseful的博客

来源:互联网 
    package com.hao.javabasis.test.thread;

    import java.util.Objects;

    /**
     * 继承自 Thread
     */
    public class ThreadTwo extends Thread {
        private Thread thread;

        private String name;

        public ThreadTwo(String threadName) {
            name = threadName;
        }

        public void run() {
            System.out.println("线程开始启动" + name);

            try {
                for (int i = 0; i < 4; i++) {
                    System.out.println("Thread: " + name + ", " + i);
                    Thread.sleep(50); //线程休眠
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程退出" + name);
        }

        public void start() {
            System.out.println("Starting" + name);
            if (Objects.isNull(thread)) {
                thread = new Thread(this, name);
                thread.start();
            }
        }
    }

    class TestThreadTwo {
        public static void main(String[] args) {
            ThreadTwo one = new ThreadTwo("thread-1");
            one.start();
            ThreadTwo two = new ThreadTwo("thread-2");
            two.start();
        }
    }    package com.hao.javabasis.test.thread;

   



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: