class NumberThread1 implements Runnable{
public void run(){
//遍历0~100的偶数
for(int i = 0;i <= 100;i++){
if(i % 2 == 0){
System.out.println(Thread.currentThread().getName() + ":" +i);
}
}
}
}
/*class NumberThread2 implements Runnable{
public void run(){
//遍历0~100的奇数
for(int i = 0;i <= 100;i++){
if(i % 2 != 0){
System.out.println(Thread.currentThread().getName() + ":" +i);
}
}
}
}*/
public class ThreadDemo{
public static void main(String[] args){
//1.提供指定线程数量的线程池
ExecutorService service = Executors.newSingleThreadExecutor();
//2.执行指定线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
NumberThread1 t=new NumberThread1();
service.execute(t); //适用于Runnable
//service.execute(new NumberThread2);
//service.submit(Callable callable); //适用于Callable
//3.关闭连接池
service.shutdown();
}
}