现在的位置: 首页 > 综合 > 正文

开启三个线程,然后定时执行

2013年09月02日 ⁄ 综合 ⁄ 共 855字 ⁄ 字号 评论关闭
package com.lesson.threadpool.example;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
public static void main(String[] args) {
//创建三个线程池
ExecutorService threadPool =
Executors.newFixedThreadPool(3);
// Executors.newCachedThreadPool();
for (int i = 1; i <= 10; i++) {
final int task = i;
threadPool.execute(new Runnable() {
public void run() {
//..
for (int j = 1; j <= 10; j++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "is loop of " + j + " for  task
 " + task);
}
}
});
}
System.out.println("all of 10 tasks have committed");
// threadPool.shutdown();//shutdownNow();
//....
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new
Runnable() {
public void run() {
System.out.println("bombing");
}
}, 6, 2, TimeUnit.SECONDS);
}
}

抱歉!评论已关闭.