java并发:Semaphore 的使用
来源:互联网 发布:去日本必买的东西知乎 编辑:程序博客网 时间:2024/06/04 17:42
package com;import java.util.concurrent.Semaphore;/** * Semapore 类在并发中的作用 * */public class SemaporeDemo {private static Semaphore mSemaphore;public static void print(String msg){try {mSemaphore.acquire();Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + ":" + msg);mSemaphore.release();}/** */public static void main(String[] args) { // 如果此信号量保证在争用时按先进先出的顺序授予许可mSemaphore = new Semaphore(2,false); // true:表示按循序先进先执行,false:表示不需要遵守这样的规则for(int i = 0 ; i < 5 ; i++){Thread th = new Thread(new Runnable() {@Overridepublic void run() {print("d");}});th.start();}}}
package com;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Semaphore;/** *Semaphore模拟实现一个数据库连接池 * * 1.一个对象保存若干个连接 * 2.开启若干个线程来过去这些连接 * 3.获取到连接的执行完成之后,需要释放连接, 让其他的线程来获取到连接 */public class DataPoolDemo {private List<Conn> pool = new ArrayList<DataPoolDemo.Conn>();private static Semaphore semaphore;/** * 初始化多少的连接 * @param num */public DataPoolDemo(int num){for(int i = 0 ; i < num ; i++){pool.add(new Conn(i));}}/** * @param args */public static void main(String[] args) {final DataPoolDemo demo = new DataPoolDemo(3);//需要将连接数量和信号量设置为一致semaphore = new Semaphore(3);Thread th1 = new Thread(new Runnable() {@Overridepublic void run() {try {Conn conn = demo.getConn();System.out.println(conn.toString()); Thread.sleep(3000); demo.realse(conn);} catch (InterruptedException e) {e.printStackTrace();}}});th1.start();for(int i = 0 ; i < 8 ; i ++){new Thread(new Runnable() {@Overridepublic void run() {try {Conn conn = demo.getConn();System.out.println(conn.toString());demo.realse(conn);} catch (InterruptedException e) {e.printStackTrace();}}}).start();}}/** 释放连接 */protected void realse(Conn conn) {pool.add(conn);System.out.println(Thread.currentThread().getName()+" release a conn " + conn.toString()); semaphore.release();}private Conn getConn() throws InterruptedException {semaphore.acquire();synchronized (pool) {Conn conn = pool.remove(0);System.out.println(Thread.currentThread().getName()+" get a conn " + conn.toString()); return conn;}}private class Conn{private int num;public Conn(int num){this.num = num;}@Overridepublic String toString() {return "con: num-" + num;}}}
0 0
- java并发:Semaphore 的使用
- 【Java并发编程】01.Semaphore的使用
- 【java并发】线程同步工具Semaphore的使用
- java并发编程系列之Semaphore信号量的使用
- Java并发编程核心方法与框架-Semaphore的使用
- java并发工具Semaphore的理解和使用
- Java并发编程: 使用Semaphore限制资源并发访问的线程数
- java并发工具包:Semaphore的用法
- Java并发之Semaphore的源码分析
- Java并发编程中Semaphore的用法
- JAVA并发信号量 Semaphore
- Java并发同步器--Semaphore
- java并发-Semaphore
- Java并发之Semaphore
- Java并发之Semaphore
- java并发中的Semaphore
- java并发信号量Semaphore
- (java多线程并发)控制并发线程数的Semaphore
- tomcat插件安装及项目部署
- 向后兼容
- 不要滥用架构隐喻
- Missing Number
- redis获取自增长序号
- java并发:Semaphore 的使用
- java.lang.ClassNotFoundException: org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver
- leetcode Binary Tree Right Side View
- JSON对象遍历
- php导出excel(多种方法)
- 【C Primer Plus】【课后习题】第二章C语言概述
- 自定义数字键盘(固定button键盘)
- Linux守护进程(init.d和xinetd)
- java 二维码工具类(二维码生成 解析)