`

Thread类创建线程

 
阅读更多

通过代码来理解:

 

public class ThreadDemo {
	public static void main(String[] args) {
		new TestThread().start();
		while(true){
			System.out.println("main is running !");
		}
	}
}

class TestThread extends Thread{
	@Override
	public void run() {
		while(true){
			System.out.println(Thread.currentThread().getName()+" is running!");
		}
	}
}

 

理解:

(1),要实现多线程,必须编写一个继承Thread 的子类,并覆盖父类的run 方法。这个run方法就写你想让新线程干的事情!

(2),启动线程是调用Thread子类中的start方法,而不是run!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics