多线程笔记
2016-06-18 10:19
155 查看
package demo;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class BooundedExecutor {
private Executor exec;
private Semaphore semaphore;
public BooundedExecutor(Executor exec, int bound){
this.exec = exec;
this.semaphore = new Semaphore(bound);
}
public void submit(final Runnable command) throws InterruptedException{
exec.execute(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
command.run();
System.out.println("线程"+Thread.currentThread().getId()+"线进入,当前并发数"+ (20 - semaphore.availablePermits()));
Thread.sleep(500);
} catch (InterruptedException e) {
} finally {
semaphore.release();
}
System.out.println("第条"+Thread.currentThread().getId()+"线程离开,当前并发数"+ (20 - semaphore.availablePermits()));
}
});
}
public static void main(String[] args) throws InterruptedException {
int size = 20;
int bound = 20;
ExecutorService exec = Executors.newFixedThreadPool(size);
BooundedExecutor booundExecutor = new BooundedExecutor(exec, bound);
for (int i = 0; i < 20; i++){
TaskRunnable command = new TaskRunnable("www.baidu.com", null);
booundExecutor.submit(command);
}
exec.shutdown();
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
System.out.println("所有任务结束");
}
private static class TaskRunnable implements Runnable{
private String url = null;
private String parm = null;
public TaskRunnable(String url, String parm){
this.url = url;
this.parm = parm;
}
@Override
public void run() {
try {
/* String response = HttpUtil.openStream(url, parm);
System.out.println(response);*/
} catch (Exception e) {
}finally{
HttpUtil.close();
}
}
}
}
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class BooundedExecutor {
private Executor exec;
private Semaphore semaphore;
public BooundedExecutor(Executor exec, int bound){
this.exec = exec;
this.semaphore = new Semaphore(bound);
}
public void submit(final Runnable command) throws InterruptedException{
exec.execute(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
command.run();
System.out.println("线程"+Thread.currentThread().getId()+"线进入,当前并发数"+ (20 - semaphore.availablePermits()));
Thread.sleep(500);
} catch (InterruptedException e) {
} finally {
semaphore.release();
}
System.out.println("第条"+Thread.currentThread().getId()+"线程离开,当前并发数"+ (20 - semaphore.availablePermits()));
}
});
}
public static void main(String[] args) throws InterruptedException {
int size = 20;
int bound = 20;
ExecutorService exec = Executors.newFixedThreadPool(size);
BooundedExecutor booundExecutor = new BooundedExecutor(exec, bound);
for (int i = 0; i < 20; i++){
TaskRunnable command = new TaskRunnable("www.baidu.com", null);
booundExecutor.submit(command);
}
exec.shutdown();
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
System.out.println("所有任务结束");
}
private static class TaskRunnable implements Runnable{
private String url = null;
private String parm = null;
public TaskRunnable(String url, String parm){
this.url = url;
this.parm = parm;
}
@Override
public void run() {
try {
/* String response = HttpUtil.openStream(url, parm);
System.out.println(response);*/
} catch (Exception e) {
}finally{
HttpUtil.close();
}
}
}
}
相关文章推荐
- firefox 插件Tab Mix Plus配置截图
- nginx代理设置域名跳转/域名重定向
- @Autowired与@Resource的区别
- Listview使用BaseAdapter添加不同布局
- JavaScript 正则表达式对象RegExp test方法慎用/g
- jsp代码页面报:javax.servlet.jsp.PageContext cannot be resolved to a type错误的解决办法
- 判断是为魅族系统(Flyme OS)
- ux.form.field.TreePicker 扩展,修复火狐不能展开bug
- ROS_Kinetic_19 群机器人框架示例(micros swarm framework)
- Python模块的编写和调用
- 关键字static/const的作用
- ROS_Kinetic_19 群机器人框架示例(micros swarm framework)
- ROS_Kinetic_19 群机器人框架示例(micros swarm framework)
- c语言中进程的基本概念
- ux.form.field.SearchField 列表、树形菜单查询扩展
- DCMTK 时间段查询
- 无情的独裁者-特斯拉的马斯克
- VII Python(5)基础知识(模块、包、异常)
- ToolBar踩过的坑
- Dos命令查看端口占用及关闭进程