线程池

来源:互联网 发布:爬虫为什么用python 编辑:程序博客网 时间:2024/06/02 08:40
package com.zy.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPool {

public static void main(String[] args) {

ExecutorService threadpool=Executors.newSingleThreadExecutor();
for (int i = 0; i <=10;i++) {
final int task=i;
threadpool.execute(new Runnable() {
@Override
public void run() {
for (int j = 0; j <=10; j++) {
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"is loop of"+j+"for task of"+task);
}
}
});
}
System.out.println("all of 10 tasks has commits");
Executors.newScheduledThreadPool(3).scheduleAtFixedRate
(new Runnable() {
@Override
public void run() {
System.out.println("bording");

}
}, 6, 2, TimeUnit.SECONDS);
}


}
原创粉丝点击