码迷,mamicode.com
首页 > 编程语言 > 详细

多线程学习之多线程访问共同资源(队列,多线程,锁机制)实例

时间:2015-10-22 12:30:15      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

模拟场景:main方法为网络请求线程(也叫生产者线程),在网络请求线程中开启四个线程(消费者线程),进行高效处理队列中的共同资源(生产者线程生产的共同资源),等待资源处理完毕,网络请求线程执行结束,响应客户端。

 

消费者线程体

技术分享
  1 /**
  2  * 
  3  */
  4 package com.benxq.Queue;
  5 
  6 import java.text.SimpleDateFormat;
  7 import java.util.Date;
  8 import java.util.concurrent.BlockingQueue;
  9 import java.util.concurrent.LinkedBlockingQueue;
 10 import java.util.concurrent.TimeUnit;
 11 
 12 
 13 /**
 14  * Created by qucf on 2015年10月22日. 
 15  */
 16 public class MyThread implements Runnable{
 17 
 18     //多线程缓存队列
 19     private BlockingQueue<String> queue=new LinkedBlockingQueue<String>();
 20     
 21     //队列中的资源数
 22     private Integer count=0;
 23     
 24     //线程锁
 25     private Object object =new Object();
 26     
 27     //生产者完成标示
 28     private boolean flag=false;
 29     
 30     //线程构造器
 31     public MyThread(BlockingQueue<String> queue){
 32         this.queue=queue;
 33     }
 34     
 35     //获取线程对象
 36     public Object getObjectByLock(){
 37         return this.object;
 38     }
 39     
 40     //标志着生产者生产结束
 41     public void setFlag(){
 42         this.flag=true;
 43     }
 44     
 45     //队列数据加一个
 46     public void setCount(){
 47         //当队列小于0时 默认恢复到0
 48         if(count<0){
 49             count=0;
 50         }
 51         count++;
 52     }
 53     
 54     @Override
 55     public void run() {
 56         while(true){
 57             //获取当前线程的名字
 58             String threadName=Thread.currentThread().getName();
 59             
 60             //资源处理完毕 和生产者生产结束  跳出循环
 61             if(count==0&& flag){
 62                 break;
 63             }
 64             
 65             try {
 66                 //从队列中获取数据,如果没有数据等待100毫秒,如果100毫秒后还是没有数据 则返回null
 67                 String pollInteger=queue.poll(1000, TimeUnit.MILLISECONDS);
 68                 
 69                 //如果取出的数据为空,则暂停本次循环,进行下次循环
 70                 if(pollInteger==null){
 71                     continue;
 72                 }
 73 
 74                 //如果队列中的数据为0 则暂停本次循环执行下次循环
 75                 if(count<=0){
 76                     continue;
 77                 }
 78                 
 79                 //队列中的数据减1
 80                 count--;
 81                 
 82                 //获取执行时间点
 83                 SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 84                 
 85                 //从队列中消费数据
 86                 System.out.println("MyThread-->"+threadName+"-->"+pollInteger+"-->"+sd.format(new Date()));
 87                 
 88                 
 89             } catch (InterruptedException e) {
 90                 // TODO Auto-generated catch block
 91                 e.printStackTrace();
 92             }
 93             
 94             
 95         }
 96         //当消费结束后 唤醒该锁锁住的所有线程
 97         synchronized (object) {
 98             object.notifyAll();
 99             System.out.println("唤醒线程"+Thread.currentThread().getName());
100         }
101     }
102 
103 }
View Code

 

主方法(模拟网络请求)

技术分享
/**
 * 
 */
package com.benxq.Queue;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by qucf on 2015年10月22日. 
 */
public class Test {

    //模拟消费线程队列
    private static BlockingQueue<String> queue=new LinkedBlockingQueue<String>();
    
    //模拟网络请求
    public static void main(String[] args) {
        
        MyThread myThread=new MyThread(queue);
        
        //线程池 开启 5个消费线程
        ExecutorService es=Executors.newFixedThreadPool(5);
        //开启5个线程
        for (int i = 0; i < 5; i++) {
            es.execute(myThread);
        }
        
        //模拟生产线程
        try {
            for (int i = 0; i <= 100; i++) {
                queue.put(i+"benxq");
                myThread.setCount();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            myThread.setFlag();
        }
        
        //生产结束后,网络请求线程被阻塞
        synchronized (myThread.getObjectByLock()) {
            try {
                myThread.getObjectByLock().wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //当线程被唤醒后,执行完毕
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("执行完毕"+sd.format(new Date()));
    }
}
View Code

 

测试结果

MyThread-->pool-1-thread-3-->4benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->1benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->2benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->5benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->3benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->8benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->0benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->9benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->7benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->12benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->6benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->14benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->15benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->16benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->17benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->18benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->19benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->20benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->13benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->11benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->10benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->23benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->24benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->22benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->26benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->27benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->29benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->21benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->30benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->31benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->32benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->33benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->34benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->35benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->37benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->36benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->28benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->39benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->40benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->41benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->42benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->43benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->44benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->45benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->46benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->47benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->48benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->49benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->25benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->51benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->52benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->54benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->55benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->56benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->50benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->58benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->59benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->60benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->61benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->62benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->38benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->63benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->64benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->65benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->57benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->67benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->68benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->53benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->70benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->71benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->72benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->69benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->75benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->76benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->66benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->77benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->78benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->80benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->74benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->81benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->82benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->83benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->84benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->85benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->86benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->87benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->88benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->73benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->90benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->91benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->92benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->93benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->94benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->89benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-1-->96benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->79benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-5-->98benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-3-->97benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-4-->95benxq-->2015-10-22 11:12:55
MyThread-->pool-1-thread-2-->100benxq-->2015-10-22 11:12:55
唤醒线程pool-1-thread-5
MyThread-->pool-1-thread-1-->99benxq-->2015-10-22 11:12:55
唤醒线程pool-1-thread-2
唤醒线程pool-1-thread-4
唤醒线程pool-1-thread-3
唤醒线程pool-1-thread-1
执行完毕2015-10-22 11:12:55

 

多线程学习之多线程访问共同资源(队列,多线程,锁机制)实例

标签:

原文地址:http://www.cnblogs.com/quchengfeng/p/4900265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!