同步代码块实现生产者消费者模式

class Person {
private String name;
private String sex;
private Boolean isEmpty = Boolean.TRUE; // 表示存储区域是否为空 //生产
public void set(String name, String sex) {
synchronized (this) {
// if(isEmpty)
while (!isEmpty.equals(Boolean.TRUE)) {
// 区域不为空,此时生产者应该停下来,等着消费者消费
try {this.wait();// 等待 消费者消费} catch (InterruptedException e) {}
}
this.name = name;
try {Thread.sleep(1); } catch (InterruptedException e) {e.printStackTrace();}
this.sex = sex;
// 生产者生产之后,应该修改存储区域的状态
isEmpty = Boolean.FALSE;// 不为空
this.notifyAll();// 唤醒消费者,起来吃东西了
}
} //消费
public void get() {
synchronized (this) {
// 存储区域为空
while (!isEmpty.equals(Boolean.FALSE)) {
try {this.wait();// 等着生产者去生产} catch (InterruptedException e) {}
}
String name = getName();
String sex = getSex();
System.out.println(name + " --> " + sex);
// 消费完成,应该修改存储区域的状态
isEmpty = Boolean.TRUE;// 空了
this.notifyAll();// 唤醒生产者,
}
}
getter/setter
} //生产者
class Producer implements Runnable {
private Person p;
public Producer(Person p) {
this.p = p;
} public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
p.set("春哥哥", "男");
} else {
p.set("著姐", "女");
}
}
}
}
//消费者
class Consumer implements Runnable {
private Person p;
public Consumer(Person p) {
this.p = p;
}
public void run() {
for (int i = 0; i < 100; i++) {
p.get();
}
}
} public class Producer_ConsumerDemo {
public static void main(String[] args) {
Person p = new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}

同步方法实现生产者消费者模式

class Person {
private String name;
private String sex;
private Boolean isEmpty = Boolean.TRUE; // 表示存储区域是否为空
//生产
public synchronized void set(String name, String sex) {
// if(isEmpty)
while (!isEmpty.equals(Boolean.TRUE)) {
// 区域不为空,此时生产者应该停下来,等着消费者消费
try {this.wait();// 等待 消费者消费} catch (InterruptedException e) {}
}
this.name = name;
try {Thread.sleep(1); } catch (InterruptedException e) {}
this.sex = sex;
// 生产者生产之后,应该修改存储区域的状态
isEmpty = Boolean.FALSE;// 不为空
this.notifyAll();// 唤醒消费者,起来吃东西了
} //消费
public synchronized void get() {
// 存储区域为空
while (!isEmpty.equals(Boolean.FALSE)) {
try {this.wait();// 等着生产者去生产} catch (InterruptedException e) {}
}
String name = getName();
String sex = getSex();
System.out.println(name + " --> " + sex);
// 消费完成,应该修改存储区域的状态
isEmpty = Boolean.TRUE;// 空了
this.notifyAll();// 唤醒生产者,
}
getter/setter
} //生产者
class Producer implements Runnable {
private Person p;
public Producer(Person p) {
this.p = p;
}
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
p.set("春哥哥", "男");
} else {
p.set("著姐", "女");
}
}
}
} class Consumer implements Runnable {
private Person p;
public Consumer(Person p) {
this.p = p;
}
public void run() {
for (int i = 0; i < 100; i++) {
p.get();
}
}
}
public class Producer_ConsumerDemo {
public static void main(String[] args) {
Person p = new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}

可重入锁实现生产者消费者模式

jkd1.5后的另一种同步机制:通过显示定义同步锁对象来实现同步,这种机制,同步锁应该使用Lock对象充当;

在实现线程安全控制中,通常使用ReentrantLock(可重入锁)。使用该对象可以显示地加锁和解锁

具有与使用 synchronized 方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大。

格式:

public class X {
private final ReentrantLock lock = new ReentrantLock();
//定义需要保证线程安全的方法
public void m(){
lock.lock();//加锁
try{
//... method body
}finally{
lock.unlock();//在finally释放锁
}
}
}

可重入锁没有同步监听对象,咋办呢?

Lock 替代了 synchronized 方法和语句的使用,Condition 替代了 Object 监视器方法的使用

class Person {
private final ReentrantLock lock = new ReentrantLock();// 创建可重入锁对象
private final Condition con = lock.newCondition(); private String name; private String sex;
private Boolean isEmpty = Boolean.TRUE; // 表示存储区域是否为空 //生产
public void set(String name, String sex) {
lock.lock();
while(!isEmpty.equals(Boolean.TRUE)){ //表示不空状态
try {con.await();} catch (InterruptedException e) {}}
try {
this.name = name;
Thread.sleep(1);
this.sex = sex;
isEmpty = Boolean.FALSE;
con.signal();

} catch (InterruptedException e) {} finally {
lock.unlock();
}
} //消费
public void get() {
lock.lock();
while(!isEmpty.equals(Boolean.FALSE)){ //存储区域为空,消费者应该等着
try {
con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
String name = getName();
String sex = getSex();
System.out.println(name + " --> " + sex);
//
isEmpty = Boolean.TRUE;
con.signal();

} finally {
lock.unlock();
} }
getter/setter
} //生产者
class Producer implements Runnable {
private Person p;
public Producer(Person p) {
this.p = p;
} public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
p.set("春哥哥", "男");
} else {
p.set("著姐", "女");
}
}
}
} class Consumer implements Runnable {
private Person p;
public Consumer(Person p) {
this.p = p;
}
public void run() {
for (int i = 0; i < 100; i++) {
p.get();
}
}
} public class Producer_ConsumerDemo {
public static void main(String[] args) {
Person p = new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}

同步代码块方式改写卖票程序

class MyRunnable implements Runnable {
private Integer num = 50;
public void run() {
for (int i = 0; i < 200; i++) {
sale();
}
}
private Object o = new Object(); public void sale() {
// synchronized (this) {
// synchronized (MyRunnable.class) {
synchronized (o) {
if (num > 0) {
try {Thread.sleep(1); } catch (InterruptedException e) {}
System.out.println(Thread.currentThread().getName() + "卖出第"
+ num-- + "张");
}
}
}
} public class TicketDemoSyncByBlock {
public static void main(String[] args) {
Runnable target = new MyRunnable();
new Thread(target, "A").start();
new Thread(target, "B").start();
new Thread(target, "C").start();
}
}

实现Runnable接口的方式,使用同步代码块的方式进行同步。

可以取的同步监听对象为:this、当前方法所在类的Class对象、任一不变对象

同步方法方式改写卖票程序

class MyRunnable implements Runnable {
private Integer num = 50;
public void run() {
for (int i = 0; i < 200; i++) {
sale();
}
} synchronized public void sale() {
if (num > 0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出第" + num-- + "张");
}
}
} public class TicketDemoSyncByMethod {
public static void main(String[] args) {
Runnable target = new MyRunnable();
new Thread(target, "A").start();
new Thread(target, "B").start();
new Thread(target, "C").start();
}
}

可重入锁方式改写卖票程序

class MyRunnable implements Runnable {

    private final ReentrantLock lock = new ReentrantLock();
private Integer num = 50; public void run() {
for (int i = 0; i < 200; i++) {
sale();
}
} public void sale() {
lock.lock();
try {
if (num > 0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出第"
+ num-- + "张");
}
} finally {
lock.unlock();
}
}
} public class TicketDemoSyncByReentrantLock {
public static void main(String[] args) {
Runnable target = new MyRunnable();
new Thread(target, "A").start();
new Thread(target, "B").start();
new Thread(target, "C").start();
}
}

jdk线程的生产者消费者问题的更多相关文章

  1. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  2. .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法

    1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...

  3. Java如何使用线程解决生产者消费者问题?

    在Java编程中,如何使用线程解决生产者消费者问题? 以下示例演示如何使用线程解决生产者消费者问题. package com.yiibai; public class ProducerConsumer ...

  4. Java多线程-同步:synchronized 和线程通信:生产者消费者模式

    大家伙周末愉快,小乐又来给大家献上技术大餐.上次是说到了Java多线程的创建和状态|乐字节,接下来,我们再来接着说Java多线程-同步:synchronized 和线程通信:生产者消费者模式. 一.同 ...

  5. java线程之生产者消费者

    看了毕向东老师的生产者消费者,就照着视频参考运行了一下,感觉还好 这个值得学习的是条理特别清晰: ProducterConsumerDemo.java中,一个资源类Resources,生产者消费者都可 ...

  6. 线程锁,threadinglocal,线程池,生产者消费者模型

    1.线程锁 1.锁Lock(只能锁一次) import threading import time v = [] lock = threading.Lock() def func(arg): lock ...

  7. Java线程通信-生产者消费者问题

    线程通信示例——生产者消费者问题 这类问题描述了一种情况,假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库中的产品取走消费.假设仓库中没有产品,则生产者可以将 产品放入仓库,有 ...

  8. Java 线程池 +生产者消费者+MySQL读取300 万条数据

    1.1需求 数据库300 万条用户数据 ,遍历获取所有用户, 各种组合关联, 获取到一个新的json ,存到redis 上. 1.2 难点 数据库比较多, 不可能单线程查询所有的数据到内存. 1.3解 ...

  9. Python自动化--语言基础7--操作日志、加密、发送邮件、线程、生产者消费者

    1.操作日志 logging.basicConfig:日志的统一处理器,对日志的输出格式和方式做配置日志级别等级CRITICAL > ERROR > WARNING > INFO & ...

随机推荐

  1. R(七): R开发实例-map热力图

    第四章通过REmap包完成基于map分布图示例,前面提到REmap基于Echart2.0, 一方面在移动终端适应效果差,另一方面REmap提供的热力图仅支持全国及省市大版块map,基于上面的原因,参考 ...

  2. jQuery validate基本原则

    Markup recommendations Each input has a label associated with it: The for-attribute of the label ref ...

  3. HTML 编辑器

    使用 Notepad 或 TextEdit 来编写 HTML 可以使用专业的 HTML 编辑器来编辑 HTML: Adobe Dreamweaver Microsoft Expression Web ...

  4. API文档中,<E>、<T>、<?>分别代表什么意思

    although they are all type parameters, the convention is:E ElementT or S TypeK Key, as in <K, V&g ...

  5. Linux下生成patch和打patch

    转自:http://blog.csdn.net/dl0914791011/article/details/17299103 通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, ...

  6. 简单的缓存代理HTTP服务器

        http, 演示客户与服务器套接字. http只支持GET操作及硬编码的MIME类型的一小部分(MIME类型是多媒体内容的类型描述符).代理HTTP服务器是单线程的,该线程中每一个请求依次被处 ...

  7. PLSQL_性能优化系列10_Oracle Array数据组优化

    2014-09-25 Created By BaoXinjian

  8. Form_通过Trace分析Concurrent和Form性能和异常详解(案例)

    2014-06-21 Created By BaoXinjian

  9. 【Java】图片高质量缩放类

    package com.test; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg. ...

  10. GDI+ 中发生一般性错误。

    GDI+ 中发生一般性错误. “/wechat”应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及 ...