阻塞队列

ArrayBlockingQueue为阻塞队列,加入和取出元素都是阻塞的!构造方法提供队列的长度
例1:
package org.test.concurrent;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BlockingQueueTest {
    /** *//**
     * 定义装苹果的篮子
     */
    public static class Basket{
        // 篮子,能够容纳3个苹果
        BlockingQueue<String> basket = new ArrayBlockingQueue<String>(3);
        
        // 生产苹果,放入篮子
        public void produce() throws InterruptedException{
            // put方法放入一个苹果,若basket满了,等到basket有位置
            basket.put("An apple");
        }
        // 消费苹果,从篮子中取走
        public String consume() throws InterruptedException{
            // get方法取出一个苹果,若basket为空,等到basket有苹果为止
            return basket.take();
        }
    }
    // 测试方法
    public static void testBasket() {
        // 建立一个装苹果的篮子
        final Basket basket = new Basket();
        // 定义苹果生产者
        class Producer implements Runnable {
            public void run() {
                try {
                    while (true) {
                        // 生产苹果
//                        System.out.println("生产者准备生产苹果:"+ System.currentTimeMillis());
                        basket.produce();
                        System.out.println("生产者生产苹果完毕:" +System.currentTimeMillis()+"篮子里有多少苹果:"+basket.basket.size());
                        // 休眠300ms
                        Thread.sleep(300);
                    }
                } catch (InterruptedException ex) {
                }
            }
        }
        // 定义苹果消费者
        class Consumer implements Runnable {
            public void run() {
                try {
                    while (true) {
                        // 消费苹果
                        /*System.out.println("消费者准备消费苹果:" 
                                + System.currentTimeMillis());*/
                        basket.consume();
                        System.out.println("消费者消费苹果完毕:" 
                                + System.currentTimeMillis()+"篮子里有多少苹果:"+basket.basket.size());
                        // 休眠1000ms
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException ex) {
                }
            }
        }
        
        ExecutorService service = Executors.newCachedThreadPool();
        Producer producer = new Producer();
        Consumer consumer = new Consumer();
        service.submit(producer);
        service.submit(consumer);
        // 程序运行5s后,所有任务停止
       // service.shutdownNow();
    }

    public static void main(String[] args) {
        BlockingQueueTest.testBasket();
    }
}


例2:
 package org.test.concurrent;

package com.hollyinfo.cuibao.biz;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.hollyinfo.cuibao.logger.HollyinfoLog;
import com.hollyinfo.cuibao.logger.HollyinfoLogger;
import com.hollyinfo.cuibao.model.Basket;
import com.hollyinfo.cuibao.util.SystemParameter;
import com.hollyinfo.cuibao.webservice.LogServer;
public class BlockingQueueBiz   {
	private static final HollyinfoLog LOG = HollyinfoLogger.getLog(LogServer.class);
	private static final long serialVersionUID = 1L;
      //私有的静态的实例   
	   public Basket basket=null;
	   private static BlockingQueueBiz instance = new BlockingQueueBiz();   
	   //私有的构造方法   
	   private BlockingQueueBiz() {
		   basket= Basket.getInstance();
			process();
	   }
	   //公有静态的访问接口,返回当前类的实例   
	   public static BlockingQueueBiz getInstance() {   
	       return instance;   
	   }
	   
      public  void process() {
    	  
    	class Producer implements Runnable {
            public void run() {
            	 while (true) {
                try {
                     basket.produce("ss");
                     Thread.sleep(1000);
                } catch (Exception ex) {
                	ex.printStackTrace();
                }
            	 }
            }
        }
        
        
        class Consumer implements Runnable {
            public void run() {
            	 while (true) {
                try {
                        String p2=basket.consume();
                        LogBizImpl.getInstance().SynchLog(SystemParameter.lua_path, p2);
                        Thread.sleep(1000);
	                } catch (Exception ex) {
	                	ex.printStackTrace();
	                }
	                catch (Error ex) {
	                	ex.printStackTrace();
	                }
	            }
            }
        }
    	
        ExecutorService service = Executors.newCachedThreadPool();
        Consumer consumer = new Consumer();
        service.submit(consumer);
        Consumer produce = new Consumer();
        service.submit(produce);
        //service.shutdownNow();
    }
}

你可能感兴趣的:(thread,apple,webservice,lua)