多线程

package com;

public class ThreadDemo {

/**
* @param args
*/
public static void main(String[] args) {
//new TestThread().start();//调用Thread的start方法,实际上调用run方法
Test1Thread t = new Test1Thread();//产生Runnable接口的子类实例化
new Thread(t).start();//启动多线程
//循环输出
for( int i = 0; i < 10; i++ ){
System.out.println("main 线程在运行");
}
}

}

class Test1Thread implements Runnable {

public void run(){
//复写Thread类中的run方法
for( int i = 0 ; i < 10; i++ ){
System.out.println("TestThread 在运行");
}
}
}

 

继承thread和实现runnable接口,都是启动多线程。

区别是,两种启动时,如果多次调用start方法后,继承thread的线程都是独立的,而实现runnable接口的线程共享资源。

posted @ 2012-04-12 14:40  幻星宇  阅读(189)  评论(0编辑  收藏  举报