黑马程序员技术交流社区

标题: 创建线程方式1:继承Thread类 [打印本页]

作者: 史世锋    时间: 2015-9-16 22:32
标题: 创建线程方式1:继承Thread类
package com.itheima;

class MyThread extends Thread
{
        //定义一个可以给线程自定义名称的构造函数
        MyThread(String name)
        {
                super(name);
        }
        @Override  
        //复写Thread类的run()方法
        public void run()
        {
                //currentThread()方法用来获取当前运行的线程
                System.out.println(Thread.currentThread().getName() + "--thread start");
        }
}
public class Test024
{

        /**
         * @param args
         */
        public static void main(String[] args)
        {
                //创建一个线程
                MyThread mt = new MyThread("mythread");
                //启动这个线程,并执行该线程的run()方法
                mt.start();
               
                /*
                MyThread mt = new MyThread();
                mt.run();
                上面这两行代码的解读:定义了一个线程mt,但是该线程并未启动,
                                                     所以该程序中只有main()方法这一个线程,
                               main()方法调用mt的run()方法
                */
        }

}







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