- 浏览: 138579 次
- 性别:
- 来自: 深圳
文章分类
最新评论
多线程 ExecutorService CountDownLatch
- 博客分类:
- Java
package com.hbw.model;
public class User {
public User(){}
public User(int id,String name){
this.id=id;
this.name=name;
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.hbw.dao;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.hbw.model.User;
public class UserDao {
private static int recordCount=899;
private int pageSize=100;
private int page=1;
private static List<User> list=Collections.synchronizedList(new ArrayList<User>());
static{
for (int i = 001; i <= recordCount; i++) {
User user=new User(i,"姓名:"+i);
list.add(user);
}
}
public List<User> getUserData(int pageSize, int page){
this.pageSize=pageSize;
this.page=page;
if(pageSize*(page+1)>list.size()){
return list.subList(page*100, list.size());
}else{
return list.subList(page*100, pageSize*(page+1));
}
}
public int getPageCount() {
return (recordCount == 0) ? 1 : ((recordCount % pageSize == 0) ? (recordCount / pageSize)
: (recordCount / pageSize) + 1);
}
public static void main(String[] args) {
UserDao dao=new UserDao();
List<User> list=dao.getUserData(100, 4);
for (User user : list) {
System.out.println(user.getId()+"____"+user.getName());
}
}
}
package com.hbw.thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import com.hbw.dao.UserDao;
import com.hbw.model.User;
public class ExecutorServiceDemo {
// 默认的并发级别
private final static int DEFAULT_CONCURRENCY_LEVEL = 5;
// 是否已经加载到最后一页
private volatile boolean isLastPage;
// 当前页
private AtomicInteger currentPage = new AtomicInteger(1);
// 总数
private static AtomicInteger count = new AtomicInteger(0);
private static ExecutorService executorService = Executors.newFixedThreadPool(DEFAULT_CONCURRENCY_LEVEL);
public static List<User> tempList= Collections.synchronizedList(new ArrayList<User>());
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(DEFAULT_CONCURRENCY_LEVEL);
long startTime=System.currentTimeMillis();
ExecutorServiceDemo test=new ExecutorServiceDemo();
test.getData(latch);
latch.await();
executorService.shutdown();
long endTime=System.currentTimeMillis(); //获取结束时间
System.err.println(tempList.size());
System.err.println("程序运行时间: "+((endTime-startTime)/1000)+"s:"+count.get());
}
public int getData(final CountDownLatch latch) throws InterruptedException{
final UserDao dao=new UserDao();
System.out.println(dao.getPageCount());
for(int i = 0; i < DEFAULT_CONCURRENCY_LEVEL; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
while(!isLoadLast()) {
int pageNum = currentPage.getAndIncrement();
if(pageNum > dao.getPageCount()) {
isLastPage = true;
continue ;
}
List<User> list=dao.getUserData(100,pageNum-1);
for (User u : list) {
System.out.println(u.getId()+"_____"+u.getName());
count.incrementAndGet();
tempList.add(u);
}
}
latch.countDown();
}
});
}
return count.get();
}
public boolean isLoadLast() {
return isLastPage;
}
}
public class User {
public User(){}
public User(int id,String name){
this.id=id;
this.name=name;
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.hbw.dao;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.hbw.model.User;
public class UserDao {
private static int recordCount=899;
private int pageSize=100;
private int page=1;
private static List<User> list=Collections.synchronizedList(new ArrayList<User>());
static{
for (int i = 001; i <= recordCount; i++) {
User user=new User(i,"姓名:"+i);
list.add(user);
}
}
public List<User> getUserData(int pageSize, int page){
this.pageSize=pageSize;
this.page=page;
if(pageSize*(page+1)>list.size()){
return list.subList(page*100, list.size());
}else{
return list.subList(page*100, pageSize*(page+1));
}
}
public int getPageCount() {
return (recordCount == 0) ? 1 : ((recordCount % pageSize == 0) ? (recordCount / pageSize)
: (recordCount / pageSize) + 1);
}
public static void main(String[] args) {
UserDao dao=new UserDao();
List<User> list=dao.getUserData(100, 4);
for (User user : list) {
System.out.println(user.getId()+"____"+user.getName());
}
}
}
package com.hbw.thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import com.hbw.dao.UserDao;
import com.hbw.model.User;
public class ExecutorServiceDemo {
// 默认的并发级别
private final static int DEFAULT_CONCURRENCY_LEVEL = 5;
// 是否已经加载到最后一页
private volatile boolean isLastPage;
// 当前页
private AtomicInteger currentPage = new AtomicInteger(1);
// 总数
private static AtomicInteger count = new AtomicInteger(0);
private static ExecutorService executorService = Executors.newFixedThreadPool(DEFAULT_CONCURRENCY_LEVEL);
public static List<User> tempList= Collections.synchronizedList(new ArrayList<User>());
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(DEFAULT_CONCURRENCY_LEVEL);
long startTime=System.currentTimeMillis();
ExecutorServiceDemo test=new ExecutorServiceDemo();
test.getData(latch);
latch.await();
executorService.shutdown();
long endTime=System.currentTimeMillis(); //获取结束时间
System.err.println(tempList.size());
System.err.println("程序运行时间: "+((endTime-startTime)/1000)+"s:"+count.get());
}
public int getData(final CountDownLatch latch) throws InterruptedException{
final UserDao dao=new UserDao();
System.out.println(dao.getPageCount());
for(int i = 0; i < DEFAULT_CONCURRENCY_LEVEL; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
while(!isLoadLast()) {
int pageNum = currentPage.getAndIncrement();
if(pageNum > dao.getPageCount()) {
isLastPage = true;
continue ;
}
List<User> list=dao.getUserData(100,pageNum-1);
for (User u : list) {
System.out.println(u.getId()+"_____"+u.getName());
count.incrementAndGet();
tempList.add(u);
}
}
latch.countDown();
}
});
}
return count.get();
}
public boolean isLoadLast() {
return isLastPage;
}
}
- ThreadDemo.rar (116.2 KB)
- 下载次数: 0
发表评论
-
springcloud feign监听启动调用服务熔断问题处理
2018-04-24 17:33 1794spring cloud 版本:Edgware.SR2 ... -
CyclicBarrier的用法
2016-07-20 18:23 453本例介绍第三个同步装置:CyclicBarrier,它维护一个 ... -
深入浅出REST
2013-08-07 11:22 731不知你是否意识到,围绕着什么才是实现异构的应用到应用通信的“ ... -
log4j.properties配置详解(转载)
2013-07-11 10:23 712Log4J的配置文件(Configuration File ... -
java反射详解
2012-12-13 11:31 808本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的 ... -
关于could not find action or result
2012-12-04 15:27 894说说这两天遇到的关于could not find acti ... -
java项目几种常见数据库连接池的使用比较
2012-07-06 11:17 979最原始的数据库使用就 ... -
java 常用7种数据库连接方式
2012-07-06 11:04 950MySQL: String Driver=&quo ... -
java数据源的几种配置
2012-07-03 16:06 993几种常用的Java数据源解决方案 Java中的数据源就是ja ... -
Java Quartz自动调度
2012-06-19 16:02 822package com.test; import or ... -
Java正则表达式详解
2012-06-13 11:31 755一、正则表达式基础知 ... -
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '
2010-12-16 14:25 1857org.apache.commons.dbcp.SQLNest ...
相关推荐
今天小编就为大家分享一篇关于在spring boot中使用java线程池ExecutorService的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了线程并发ScheduledExecutorService类,设置 ScheduledExecutorService ,2秒后,在 1 分钟内每 10 秒钟蜂鸣一次
主要介绍了Java使用ExecutorService来停止线程服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
看完《think in java》多线程章节,自己写的多线程文档,还结合了其他的相关网络资料。 线程 一. 线程池 1)为什么要使用线程池 2 2)一个具有线程池的工作队列 3 3)使用线程池的风险: 4 4)有效使用线程池的原则 5...
这是利用java的udp通信,利用多线程保持通信持续的java聊天小程序
主要介绍了Java利用ExecutorService实现同步执行大量线程,ExecutorService可以维护我们的大量线程在操作临界资源时的稳定性。
使用 ExecutorService 多线程处理某个进程的简单示例。
主要帮助大家理解java多线程中ExcetorServiced的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java多线程入阶干货分享 1.使用线程的经验:设置名称、响应中断、使用ThreadLocal 2.Executor:ExecutorService和Future 3.阻塞队列:put和take、offer和poll、drainTo 4.线程间通信:lock、condition、wait、notify...
本代码是JAVA 编码,使用多线程泛型例子获取对应多线程返回结果,便于后期扩充,可以改变ExecutorService。
ExecutorService方法案例文件.zip
接口 java.util.concurrent.ExecutorService 表述了异步执行的机制,并且可以让任务在后台执行。壹個 ExecutorService 实例因此特别像壹個线程池。事实上,在 java.util.concurrent 包中的 ExecutorService 的实现...
ExecutorService的execute和submit方法
三个不同package:1、只有多线程;2、同步账户内充值方法;3、同步业务调用充值方法。 线程执行结束判断语句:如果executorService.isShutdown()和executorService.isTerminated()都为true,则执行结束。
本程序实现了ExecutorService线程池,内置说明txt说明,可以参考
Java语言虽然内置了多线程支持,启动一个新线程非常方便,但是,创建线程需要操作系统资源(线程资源,栈空间等),频繁创建和销毁大量线程需要消耗大量时间。 而线程池内部维护了若干个线程,没有任务的时候,这些...
包括阻塞队列、阻塞栈、ExecutorService、Future、ExecutorCompletionService、死锁、join、重入锁、读写锁、多线程抢票、信号量、signal/await、ThreadLocal等的实例。
JAVA线程基本学习, JAVA多线程的特性= 线程池: 本质上是一个对象池, 用来管理线程资源. 在任务执行前, 需要从线程池中拿出线程来执行. 在任务执行完成之后, 需要把线程放回线程池. 线程池好处: 降低资源的消耗...
NULL 博文链接:https://songjianyong.iteye.com/blog/2056990
其中采用Java 的ExecutorService来进行线程池的方式实现多线程,模拟客户端多用户向同一服务器端发送请求. 注意,此为学习笔记,可以作为参考学习使用,不建议商业使用或生产使用。 废话不多说,直接上代码。