济南IT培训 > 达内新闻
Java多线程和线程池(二)
- 发布:济南达内培训
- 来源:济南达内培训
- 时间:2018-12-26 15:55
一、Java自带线程池-济南达内培训负责整理
先看看Java自带线程池的例子,开启5个线程打印字符串List:
package com.luo.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadTest {
public static void main(String[] args) {
List<String> strList = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
strList.add(“String” + i);
}
int threadNum = strList.size() < 5 ? strList.size() : 5;
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, threadNum, 300,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < threadNum; i++) {
executor.execute(new PrintStringThread(i,strList,threadNum));
}
executor.shutdown();

}
}
class PrintStringThread implements Runnable {
private int num;
private List<String> strList;
private int threadNum;
public PrintStringThread(int num, List<String> strList, int threadNum) {
this.num = num;
this.strList = strList;
this.threadNum = threadNum;
}
public void run() {
int length = 0;
for(String str : strList){
if (length % threadNum == num) {
System.out.println(“线程编号:” + num + “,字符串:” + str);
}
length ++;
}
}
}
Java自带线程池构造方法
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue
RejectedExecutionHandler handler)corePoolSize
以上就是济南达内培训给大家做的内容详解,更多关于IT的学习,请继续关注济南达内培训
最新开班时间
- 北京
- 上海
- 广州
- 深圳
- 南京
- 成都
- 武汉
- 西安
- 青岛
- 天津
- 杭州
- 重庆
- 哈尔滨
- 济南
- 沈阳
- 合肥
- 郑州
- 长春
- 苏州
- 长沙
- 昆明
- 太原
- 无锡
- 石家庄
- 南宁
- 佛山
- 珠海
- 宁波
- 保定
- 呼和浩特
- 洛阳
- 烟台
- 运城
- 潍坊
Java多线程和线程池(二)
- 发布:济南达内培训
- 来源:济南达内培训
- 时间:2018-12-26 15:55
一、Java自带线程池-济南达内培训负责整理
先看看Java自带线程池的例子,开启5个线程打印字符串List:
package com.luo.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadTest {
public static void main(String[] args) {
List<String> strList = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
strList.add(“String” + i);
}
int threadNum = strList.size() < 5 ? strList.size() : 5;
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, threadNum, 300,
TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < threadNum; i++) {
executor.execute(new PrintStringThread(i,strList,threadNum));
}
executor.shutdown();

}
}
class PrintStringThread implements Runnable {
private int num;
private List<String> strList;
private int threadNum;
public PrintStringThread(int num, List<String> strList, int threadNum) {
this.num = num;
this.strList = strList;
this.threadNum = threadNum;
}
public void run() {
int length = 0;
for(String str : strList){
if (length % threadNum == num) {
System.out.println(“线程编号:” + num + “,字符串:” + str);
}
length ++;
}
}
}
Java自带线程池构造方法
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue
RejectedExecutionHandler handler)corePoolSize
以上就是济南达内培训给大家做的内容详解,更多关于IT的学习,请继续关注济南达内培训
最新开班时间
- 北京
- 上海
- 广州
- 深圳
- 南京
- 成都
- 武汉
- 西安
- 青岛
- 天津
- 杭州
- 重庆
- 厦门
- 哈尔滨
- 济南
- 福州
- 沈阳
- 合肥
- 郑州
- 长春
- 苏州
- 大连
- 长沙
- 昆明
- 温州
- 太原
- 南昌
- 无锡
- 石家庄
- 南宁
- 中山
- 兰州
- 佛山
- 珠海
- 宁波
- 贵阳
- 保定
- 呼和浩特
- 东莞
- 洛阳
- 潍坊
- 烟台
- 运城