使用信号量管理多线程

应用场景如下:
生产者是单线程程序,会从数据库获得数据插入阻塞队列
消费者是多线程程序,会从阻塞队列获得数据进行处理
为了保持一个较好的并发量,系统需要设置消费者的最高并发数

程序如下:


import   static  java.util.concurrent.TimeUnit.MILLISECONDS;

import  java.util.ArrayList;
import  java.util.Iterator;
import  java.util.List;
import  java.util.concurrent.BlockingQueue;
import  java.util.concurrent.Executor;
import  java.util.concurrent.Executors;
import  java.util.concurrent.LinkedBlockingQueue;
import  java.util.concurrent.ScheduledExecutorService;
import  java.util.concurrent.Semaphore;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;

/**
 * 测试信号量
 * 
 * 
@author Kent Chen
 * @date 2007-12-5
 
*/

public   class  StudySemaphore  {
    
final static Log log = LogFactory.getLog(StudySemaphore.class);

    
public static void main(String[] args) {
        
final Executor pool = Executors.newFixedThreadPool(20);// 线程池
        final Semaphore semaphore = new Semaphore(1);// 定义信号量最大值
        final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();// 队列长度
        
        
// 初始化messages,插入1000条消息
        List<String> messages = new ArrayList<String>();
        
for (int i = 0; i < 1000; i++{
            messages.add(
"消息" + i);
        }


        
final Iterator<String> messageIter = messages.iterator();

        
// 启动线程,往队列里面插入值,每隔10毫秒往队列里面插值
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleWithFixedDelay(
new Runnable() {
            
public void run() {
                
try {
                    
if (messageIter.hasNext()) {
                        semaphore.acquire();
// 获得信号量
                        String message = messageIter.next();
                        log.info(
"往队列插入值:" + message);
                        queue.put(message);
                    }

                }
 catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
02000, MILLISECONDS);// 0秒后开始,执行完后休息sleeptime并重新执行

        pool.execute(
new Runnable() {
            
public void run() {
                
while (true{
                    
try {
                        
final String message = queue.take();
                        pool.execute(
new Runnable() {
                            
public void run() {
                                log.info(
"线程休息5秒");
                                
try {
                                    Thread.sleep(
5000);
                                }
 catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                log.info(
"处理数据:" + message);
                                semaphore.release();
//释放信号量
                            }

                        }
);
                    }
 catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }

        }
);

    }

}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值