Java多线程生产者消费者调度实现

生产者消费者模型是多线程中最常见的模型,有着广泛的应用。其主要目的是实现一种动态的平衡,让生产者消费者和谐共处,获得最大化的执行效率。
 
所说的动态平衡其实就是生产者与消费者协作控制仓储,让消费者不至于缺货,也不能导致不合理不和谐的库存。
 
生产者消费者实现最简单的方式是通过java5之后的线程池来实现,下面的例子很粗糙,但是能良好运行。
 
在实际应用中,可以基于数据库,加上复杂逻辑,实现更强悍的后台处理程序,目前基于此模型构建的后台程序良好运行,处理效果极佳。
 
/** 
* 消费者 

* @author leizhimin 12-10-23 下午4:10 
*/
 
public  class MyTask  implements Runnable{ 
         private  long id; 

         public MyTask( long id) { 
                 this.id = id; 
        } 

        @Override 
         public  void run() { 
                 try { 
                        Thread.sleep(100L); 
                }  catch (InterruptedException e) { 
                        e.printStackTrace(); 
                } 
                System.out.println( "\t"+Thread.currentThread().getName()+ ":"+id); 
        } 

 
 
import javax.swing.plaf.metal.MetalBorders; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ThreadPoolExecutor; 

/** 
* 总调度程序 (包括协作生产) 

* @author leizhimin 12-10-23 下午4:18 
*/
 
public  class TaskPool  extends Thread { 
         private String poolname; 
         private ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(5); 

         public TaskPool(String poolname) { 
                 this.poolname = poolname; 
        } 

        @Override 
         public  void run() { 
                System.out.println(poolname +  ":池中的当前线程数getPoolSize()=" + pool.getPoolSize()); 
                 int i = 0; 
                 while( true){ 
                         int x = pool.getQueue().size(); 
//                        System.out.println("返回核心线程数="+pool.getCorePoolSize()); 
                        System.out.println( "返回此执行程序使用的任务队列="+pool.getQueue().size()); 
                         if(x>=5) 
                                 try { 
                                        Thread.sleep(10L); 
                                         continue
                                }  catch (InterruptedException e) { 
                                        e.printStackTrace(); 
                                } 
                        System.out.println(poolname +  "该加入任务了");             //生产过程 
                         for( int k =i+10;i<k;i++){ 
                                pool.submit( new MyTask(i)); 
                        } 
                } 
        } 

         public  static  void main(String[] args) { 
                  new TaskPool( "pool1").start(); 
        } 

 
 
E:\jdk1.6.0_33\bin\java -Didea.launcher.port=7534 -Didea.launcher.bin.path=C:\IDEA11.1.3\bin -Dfile.encoding=UTF-8 -classpath E:\jdk1.6.0_33\jre\lib\charsets.jar;E:\jdk1.6.0_33\jre\lib\deploy.jar;E:\jdk1.6.0_33\jre\lib\javaws.jar;E:\jdk1.6.0_33\jre\lib\jce.jar;E:\jdk1.6.0_33\jre\lib\jsse.jar;E:\jdk1.6.0_33\jre\lib\management-agent.jar;E:\jdk1.6.0_33\jre\lib\plugin.jar;E:\jdk1.6.0_33\jre\lib\resources.jar;E:\jdk1.6.0_33\jre\lib\rt.jar;E:\jdk1.6.0_33\jre\lib\ext\dnsns.jar;E:\jdk1.6.0_33\jre\lib\ext\localedata.jar;E:\jdk1.6.0_33\jre\lib\ext\sunjce_provider.jar;E:\jdk1.6.0_33\jre\lib\ext\sunmscapi.jar;E:\jdk1.6.0_33\jre\lib\ext\sunpkcs11.jar;G:\testprojects\testpool\out\production\testpool;C:\IDEA11.1.3\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain TaskPool 
pool1:池中的当前线程数getPoolSize()=0 
返回此执行程序使用的任务队列=0 
pool1该加入任务了 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
  pool-1-thread-1:0 
  pool-1-thread-2:1 
  pool-1-thread-5:4 
  pool-1-thread-4:3 
  pool-1-thread-3:2 
返回此执行程序使用的任务队列=0 
pool1该加入任务了 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
  pool-1-thread-1:5 
  pool-1-thread-5:7 
  pool-1-thread-2:6 
  pool-1-thread-3:9 
  pool-1-thread-4:8 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
  pool-1-thread-1:10 
返回此执行程序使用的任务队列=4 
pool1该加入任务了 
返回此执行程序使用的任务队列=14 
  pool-1-thread-5:11 
  pool-1-thread-4:14 
  pool-1-thread-3:13 
  pool-1-thread-2:12 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
  pool-1-thread-1:15 
  pool-1-thread-4:17 
  pool-1-thread-3:18 
  pool-1-thread-2:19 
  pool-1-thread-5:16 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
  pool-1-thread-1:20 
  pool-1-thread-2:23 
  pool-1-thread-5:24 
  pool-1-thread-3:22 
  pool-1-thread-4:21 
返回此执行程序使用的任务队列=0 
pool1该加入任务了 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
  pool-1-thread-1:25 
  pool-1-thread-2:26 
  pool-1-thread-3:28 
  pool-1-thread-5:27 
  pool-1-thread-4:29 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
  pool-1-thread-1:30 
  pool-1-thread-4:34 
  pool-1-thread-3:32 
  pool-1-thread-5:33 
  pool-1-thread-2:31 
返回此执行程序使用的任务队列=0 
pool1该加入任务了 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
  pool-1-thread-1:35 
  pool-1-thread-4:36 
  pool-1-thread-3:37 
  pool-1-thread-2:39 
  pool-1-thread-5:38 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
  pool-1-thread-1:40 
  pool-1-thread-5:44 
  pool-1-thread-2:43 
  pool-1-thread-3:42 
  pool-1-thread-4:41 
返回此执行程序使用的任务队列=0 
pool1该加入任务了 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10 
  pool-1-thread-1:45 
  pool-1-thread-2:47 
  pool-1-thread-3:48 
  pool-1-thread-5:46 
  pool-1-thread-4:49 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
返回此执行程序使用的任务队列=5 
  pool-1-thread-1:50 
返回此执行程序使用的任务队列=4 
pool1该加入任务了 
返回此执行程序使用的任务队列=14 
  pool-1-thread-5:53 
  pool-1-thread-3:52 
  pool-1-thread-4:54 
  pool-1-thread-2:51 
返回此执行程序使用的任务队列=10 
返回此执行程序使用的任务队列=10
。。。。。
 
可以看到,库存永远不会超过14。
 
 
太忙了,没空详细写,如果你有不同的见解,请留下代码,不要对作者本人进行攻击评论。谢谢各位!


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1036186,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java语言编写的生产者消费者进度调度问题的示例代码: ```java public class Main { public static void main(String[] args) { Buffer buffer = new Buffer(); Producer p1 = new Producer(buffer, 1); Producer p2 = new Producer(buffer, 2); Consumer c1 = new Consumer(buffer, 1); Consumer c2 = new Consumer(buffer, 2); p1.start(); p2.start(); c1.start(); c2.start(); } } class Buffer { private int data; private boolean empty; public Buffer() { empty = true; } public synchronized void produce(int newData, int producerId) { while (!empty) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } data = newData; empty = false; System.out.println("Producer " + producerId + " produced " + data); notifyAll(); } public synchronized int consume(int consumerId) { while (empty) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } empty = true; System.out.println("Consumer " + consumerId + " consumed " + data); notifyAll(); return data; } } class Producer extends Thread { private Buffer buffer; private int producerId; public Producer(Buffer buffer, int producerId) { this.buffer = buffer; this.producerId = producerId; } public void run() { for (int i = 0; i < 10; i++) { buffer.produce(i, producerId); try { sleep((int) (Math.random() * 100)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer extends Thread { private Buffer buffer; private int consumerId; public Consumer(Buffer buffer, int consumerId) { this.buffer = buffer; this.consumerId = consumerId; } public void run() { int data; for (int i = 0; i < 10; i++) { data = buffer.consume(consumerId); try { sleep((int) (Math.random() * 100)); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 在这个示例,`Buffer`类表示缓冲区,它具有`produce()`和`consume()`方法用于生产和消费数据。`Producer`和`Consumer`类表示生产者和消费者,它们分别使用`Buffer`对象进行生产和消费操作。 在`Buffer`类,`produce()`方法在缓冲区为空时向其添加数据,`consume()`方法在缓冲区非空时从取出数据。这两个方法都使用`synchronized`关键字实现同步操作,确保在多线程环境下数据的正确性。 在`Producer`类,`run()`方法重复调用`Buffer`对象的`produce()`方法向缓冲区添加数据。在每次添加数据后,线程随机等待一段时间以模拟生产过程的延迟。 在`Consumer`类,`run()`方法重复调用`Buffer`对象的`consume()`方法从缓冲区取出数据。在每次取出数据后,线程随机等待一段时间以模拟消费过程的延迟。 在`Main`类,创建了一个`Buffer`对象和四个线程,其两个是`Producer`对象,另外两个是`Consumer`对象。最后,启动这四个线程以开始生产者和消费者的运行过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值