线程池
2016-01-17 15:21
267 查看
package com.ygl;
public class ThreadPoolRunnable implements Runnable{
@Override
public void run() {
for(int i=1;i<=10;i++){
System.out.println(Thread.currentThread().getName()+" is looping of "+i);
}
}
}
//*****************************************************
package com.ygl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
public static void main(String[] args) {
// ThreadPoolRunnable threadPoolRunnable=new ThreadPoolRunnable();
ExecutorService threadPool= Executors.newFixedThreadPool(3);//创建3个线程
for(int i=1;i<=10;i++){
//System.out.println("Task: "+i+"===============");
threadPool.execute(new ThreadPoolRunnable());
}
System.out.println("all task commit");
threadPool.shutdown();
}
}
//*************************************************************
package com.ygl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
public static void main(String[] args) {
// ThreadPoolRunnable threadPoolRunnable=new ThreadPoolRunnable();
ExecutorService threadPool= Executors.newCachedThreadPool();//根据任务动态创建线程
for(int i=1;i<=10;i++){
//System.out.println("Task: "+i+"===============");
threadPool.execute(new ThreadPoolRunnable());
}
System.out.println("all task commit");
threadPool.shutdown();
}
}
//************************************************************
package com.ygl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
public static void main(String[] args) {
// ThreadPoolRunnable threadPoolRunnable=new ThreadPoolRunnable();
ExecutorService threadPool= Executors.newCachedThreadPool();//创建3个线程
for(int i=1;i<=10;i++){
//System.out.println("Task: "+i+"===============");
threadPool.execute(new ThreadPoolRunnable());
}
System.out.println("all task commit");
threadPool.shutdown();
//定时任务调度
Executors.newScheduledThreadPool(3).schedule(new ThreadPoolRunnable(),10,TimeUnit.SECONDS);
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new ThreadPoolRunnable(),10,2,TimeUnit.SECONDS);//10s后炸,每隔2s触发一次
}
}
public class ThreadPoolRunnable implements Runnable{
@Override
public void run() {
for(int i=1;i<=10;i++){
System.out.println(Thread.currentThread().getName()+" is looping of "+i);
}
}
}
//*****************************************************
package com.ygl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
public static void main(String[] args) {
// ThreadPoolRunnable threadPoolRunnable=new ThreadPoolRunnable();
ExecutorService threadPool= Executors.newFixedThreadPool(3);//创建3个线程
for(int i=1;i<=10;i++){
//System.out.println("Task: "+i+"===============");
threadPool.execute(new ThreadPoolRunnable());
}
System.out.println("all task commit");
threadPool.shutdown();
}
}
//*************************************************************
package com.ygl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
public static void main(String[] args) {
// ThreadPoolRunnable threadPoolRunnable=new ThreadPoolRunnable();
ExecutorService threadPool= Executors.newCachedThreadPool();//根据任务动态创建线程
for(int i=1;i<=10;i++){
//System.out.println("Task: "+i+"===============");
threadPool.execute(new ThreadPoolRunnable());
}
System.out.println("all task commit");
threadPool.shutdown();
}
}
//************************************************************
package com.ygl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
public static void main(String[] args) {
// ThreadPoolRunnable threadPoolRunnable=new ThreadPoolRunnable();
ExecutorService threadPool= Executors.newCachedThreadPool();//创建3个线程
for(int i=1;i<=10;i++){
//System.out.println("Task: "+i+"===============");
threadPool.execute(new ThreadPoolRunnable());
}
System.out.println("all task commit");
threadPool.shutdown();
//定时任务调度
Executors.newScheduledThreadPool(3).schedule(new ThreadPoolRunnable(),10,TimeUnit.SECONDS);
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new ThreadPoolRunnable(),10,2,TimeUnit.SECONDS);//10s后炸,每隔2s触发一次
}
}
相关文章推荐
- Maven基础配置--nexus私服配置
- 线性回归及其Python实现(最大似然法)
- 图像哈希检索背景综述(一)
- Unicode下CString转char*
- coalesce、nvl、nvl2比较
- Java使用HttpURLConnection上传文件
- 【转】hadoop深入研究:(十一)——序列化与Writable实现
- ACM学习之路
- cookie 和session 的区别详解
- 叶子结点
- List的常用方法
- CONNECT BY
- tcpdump丢包问题分析
- CentOs中触摸板配置
- Android 开发笔记(一) 按钮事件调用Activity
- awesome的快捷键
- Java 入门 之 数组与循环
- Linux查看硬件信息
- 腾讯管家,小火箭效果的实现
- MapReduce的矩阵乘法的实现