java.lang.Thread.start()%uA0方法使该线程开始执行时,Java虚拟机调用该线程的run方法。其结果是,两个线程同时运行:在当前线程(其返回从调用start方法)和另一个线程(执行其run方法)。
声明
以下是java.lang.Thread.start()方法的声明
public void start()
参数
-
NA
返回值
此方法不返回任何值。
异常
-
IllegalThreadStateException%uA0-- 如果已经启动线程。
例子
下面的例子显示java.lang.Thread.start()方法的使用。
package com.yiibai import java.lang.* public class ThreadDemo implements Runnable { Thread t ThreadDemo() { // thread created t = new Thread(this, "Admin Thread") // prints thread created System.out.println("thread = " + t) // this will call run() function System.out.println("Calling run() function... ") t.start() } public void run() { System.out.println("Inside run()function") } public static void main(String args[]) { new ThreadDemo() } }
让我们来编译和运行上面的程序,这将产生以下结果:
thread = Thread[Admin Thread,5,main] Calling run() function... Inside run()function