java并发:Semaphore 的使用

本文详细介绍了Semaphore类在并发编程中的作用,包括其在争用资源时按先进先出顺序授予许可的功能,并通过实例展示了如何使用Semaphore实现数据库连接池,确保连接资源的有效管理和复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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() {
				
				@Override
				public 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() {
			
			@Override
			public 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() {
				
				@Override
				public 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;
		}
		
		@Override
		public String toString() {
			return "con: num-" + num;
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值