黑马程序员技术交流社区

标题: 线程的启动 [打印本页]

作者: wwwdai    时间: 2015-3-14 22:26
标题: 线程的启动
新创建的线程不会自动运行。必须调用线程的start( )方法。
使用start()方法的语句很简单,例:
Thread ThreadName=new ThreadClass();
ThreadName.start();
public class StartThread extends Thread {
        public void run() {
                // 获取当前线程的引用
                Thread t = Thread.currentThread();
                // 获取线程的名字
                String name = t.getName();
                System.out.println("线程名字:" + name);
        }
        public static void main(String[] args) {
                StartThread myt = new StartThread();
                myt.start();
                // 在主线程中调用run方法
                myt.run();
        }
}





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