//创建线程:实现Runnable接口
public class MyThread implements Runnable {
public void run(){
for (int i = 1; i<=100; i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
测试类
//测试线程
public class TestThread2 {
public static void main(String[] args) {
//创建线程对象
Runnable runnable = new MyThread();
Thread thread = new Thread(runnable,"Mythread1");
Thread thread2 = new Thread(runnable,"Mythread2");
//启动线程
thread.start();
thread2.start();
}
}
本文介绍了如何通过实现Runnable接口创建线程,并展示了MyThread类的run方法,以及在TestThread2中如何实例化并启动两个线程进行测试。
3033

被折叠的 条评论
为什么被折叠?



