使用继承Thread的方式编写线程,我想创建10个线程并运行,下面的代码可不可行:
package com.mfeng;
public class ThreadExample extends Thread{
public void run() {
System.out.println("Thread: " + getName() + " running");
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
for (int i = 0;i < 10;i++) {
ThreadExample threadExample = new ThreadExample();
threadExample.start();
}
}
}
我是用for循环创建10个ThreadExample对象,然后启动线程。这样做有没有什么问题?