package com.thread2;
//java多线程模拟生产者消费者问题
//ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品
//Storage仓库
public class ProducerConsumer {
public static void main(String[] args) {
Storage s = new Storage();
Producer p = new Producer(s);
Consumer c = new Consumer(s);
Thread tp = new Thread(p);
Thread tc = new Thread(c);
tp.start();
tc.start();
}
}
class Consumer implements Runnable {// 消费者
Storage s = null;
public Consumer(Storage s) {
this.s = s;
}
public void run() {
for (int i = 0; i < 20; i++) {
Product p = s.pop();// 取出产品
try {
Thread.sleep((int) (Math.random() * 1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable {// 生产者
Storage s = null;
public Producer(Storage s) {
this.s = s;
}
public void run() {
for (int i = 0; i < 20; i++) {
Product p = new Product(i);
s.push(p); // 放入产品
// System.out.println("生产者放入:" + p);
try {
Thread.sleep((int) (Math.random() * 1500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Product {
int id;
public Product(int id) {
this.id = id;
}
public String toString() {// 重写toString方法
return "产品:" + this.id;
}
}
class Storage {
int index = 0;
Product[] products = new Product[5];
public synchronized void push(Product p) {// 放入
while (index == this.products.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.products[index] = p;
System.out.println("生产者放入" + index + "位置:" + p);
index++;
this.notifyAll();
}
public synchronized Product pop() {// 取出
while (this.index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
index--;
this.notifyAll();
System.out.println("消费者从" + index + "位置取出:" + this.products[index]);
return this.products[index];
}
}
wait导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
wait(),notify(),notifyAll()不属于Thread类,而是属于Object基础类,也就是说每个对像都有wait(),notify(),notifyAll()的功能.因为都个对像都有锁,锁是每个对像的基础,当然操作锁的方法也是最基础了.
package com.test.thread;
import java.util.ArrayList;
import java.util.List;
public class CustomerProducer {
public static void main(String args[]){
Storage s = new Storage();
Producer p = new Producer(s);
Customer c = new Customer(s);
Thread tp = new Thread(p);
Thread cp = new Thread(c);
tp.start();
cp.start();
}
}
class Storage{
List<String> storage = new ArrayList<String>();
int maxLength = 10;
int index = 0;
public synchronized void push(String goods){
while(this.index == maxLength){
try{
System.out.println("push wait");
this.wait();
}catch(Exception ex){
ex.printStackTrace();
}
}
storage.add(goods);
System.out.println("producer index:"+(index+1));
index++;
this.notifyAll();
}
public synchronized String pop(){
while(this.index == 0){
try{
System.out.println("pop wait");
this.wait();
}catch(Exception ex){
ex.printStackTrace();
}
}
String tmp = storage.remove(index-1);
System.out.println("customer index:"+index);
index--;
this.notifyAll();
return tmp;
}
}
class Producer implements Runnable{
Storage s = null;
public Producer(Storage s){
this.s = s;
}
public void run(){
for(int i = 0;i<10;i++){
String goods = String.valueOf(i);
s.push(goods);
try{
Thread.sleep((int)(Math.random()*1000));
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class Customer implements Runnable{
Storage s = null;
public Customer(Storage s){
this.s = s;
}
public void run(){
for(int i = 0;i<10;i++){
// String str = s.pop();
// System.out.println(str);
s.pop();
try{
Thread.sleep((int)(Math.random()*3000));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
分享到:
相关推荐
(注意,本资源附带书中源代码可供参考) 多线程与并发处理是程序设计好坏优劣的重要课题,本书通过浅显易懂的文字与实例来介绍Java线程相关的设计模式概念,并且通过实际的Java程序范例和 UML图示来一一解说,书中...
java的多线程示例(生产者消费者问题)
Java多线程模式详解 目录: 一、漫谈UML Java语言的线程 多线程的评量标准 二、 1、Single Threaded Execution ———— 能通过这座桥的,只有一个人 2、Immutable ———— 想破坏它也没办法 3、Guarded ...
利用信号量实现的多线程之间的同步与互斥,详情看博客文章Linux多线程编程(二)---线程之间的同步与互斥进阶实验
Introduction 2 多线程程序的评量标准 第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不...
采用2个producer和一个Consumer把整数放入到(或者从中取出)一个环形缓冲Circle Buffer中,这块连续的存储会被反复使用。一个写指针指向数据写入的位置,如果写到Buffer的末端,则从Buffer的头端开始写。类似的,一...
非常好的程序资源,一秒钟一千多条!
SimpleFork提供一套类似于JAVA多线程的进程控制接口,提供回收、同步、互斥、通信等方案,开发者可以关注业务问题,不需要过多考虑进程控制。 引入 composer require jenner/simple_fork require path/to/...
在工作中,理解并运用生产者消费者模型能在高并发开发中出现更少的问题,本Demo最从简而行,使用Java语言实现一个最简单的生产者消费者模型。博客地址:https://blog.csdn.net/u012552275/article/details/104730175
用java为源代码,解决多线程编程中的经典问题:生产者消费者问题
自己写的tensorflow多线程数据读入,包括使用tf.train.string_input_producer()等。。。。
生产者-消费者模式的 Java 8 变体
Kafka Producer机制优化-提高发送消息可靠性
python 条件同步的使用 条件同步:threading.Condition 多线程同步,python2例程 多线程的同步 多线程情况下最常见的问题之一:数据共享; 当多个线程都要去修改某一个共享数据的时候,需要对数据访问进行同步...
java生产者消费者程序实现 线程,信号量互斥事件 临界资源
ThreadImRunnable.java 继承Runnable接口实现多线程 mulThread.java 创建多个线程对象的类 demoJoin.java 演示使用join()以确保主线程最后结束 clicker.java 一个计数用的线程类 demoPri.java 调用上面这个类...
java中的涉及到生产者 消费者的多线程操作问题
生产者消费者使用java实现经典的并发或线程模式。 在分支“简化”中,代码被简化以使其更具可读性。 在“master”分支中,运行代码,可以很容易地理解真正的过程。
Producer.java
主要介绍了Kafka Java Producer代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下