学习的张孝祥的《Java就业培训教程》觉得讲的不错,是比较好的入门教程吧。
下面是最简单的例子。
public class ThreadDemo {
public static void main(String[] args) {
new TestThread().start();
while(true) {
System.out.println("main(): "+Thread.currentThread().getName() + " is running");
}
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread: "+Thread.currentThread().getName() + " is running");
}
}
}
输出结果为:
输出结果为 写道
main(): main is running
TestThread: Thread-0 is running
main(): main is running
TestThread: Thread-0 is running
main(): main is running
TestThread: Thread-0 is running
main(): main is running
TestThread: Thread-0 is running
main(): main is running
TestThread: Thread-0 is running
main(): main is running
TestThread: Thread-0 is running