public class ImplementsRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
在主方法里启动你的线程
package com.demo;
public class ExcutorClass {
public static void main(String[] args) {
Runnable runnable = new ImplementsRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
}
结果:
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
3、通过线程池的方式来创建线程
package com.demo;