java中施用多线程的继承和实现Runnable两种方法

java中使用多线程的继承和实现Runnable两种方法

    分别使用多线程的继承和实现Runnable两种方法,各写出运行结果是每间隔nTime时间打印出对象实例中的字符串成员变量strConsumer的内容的多线程程序。
提示:建立类Consumer,继承线程类Thread,构造方法中传入两个参数nTime,和strConsumer,调用对象实例的start方法启动对象线程运行。


你要换成实现Runnable的话  就改下 代码第一行!

public class MainTest /*implements Runnable */extends Thread {

	private String strConsumer;
	private long sleepT;

	public MainTest_T4(long nTime, String strConsumer) {
		this.strConsumer = strConsumer;
		this.sleepT = nTime;
		new Thread(this).start();
	}

	public void run() {
		while (true) {
			System.out.println("strConsumer:" + strConsumer);
			try {
				Thread.sleep(sleepT);
			} catch (Exception e) {
				System.out.println("Exception...");
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new MainTest(1000, "a shine customer");
	}
}