JAVA多线程通信

package com.frank.thread;
/**
* author:pengyan
* date:Jun 16, 2011
* file:ProducerAndCustomerTest.java
*/
public class ProducerAndCustomerTest {
public static void main(String[] args) {
//create an object
Queue q=new Queue();
Productor p=new Productor(q);
Customer c=new Customer(q);
//p and c shared q
p.start();
c.start();
}
}
class Customer extends Thread{
Queue q;
public Customer(Queue q) {
this.q=q;
}
@Override
public void run() {
while (q.value<10) {//get the value
System.out.println("Customer get "+q.get());
}
}
}
class Productor extends Thread{
Queue q;
public Productor(Queue q) {
this.q=q;
}
@Override
public void run() {
for (int i = 1; i <=10; i++) {
q.put(i);//product and show info
System.out.println("Productor put "+i);
}
}
}
class Queue{
int value;//count the mumber
boolean bFull=false;//whether the cup is full
public synchronized void put(int i){
if (!bFull) {
value=i;//fill the cup
bFull=true;//it is full
notifyAll();//notify other thread
try {
wait();//wait.......
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized int get(){
if (!bFull) {
try {
wait();//if not full,wait until full
} catch (InterruptedException e) {
e.printStackTrace();
}
}
bFull=false;//after got the cup is empty
notifyAll();//notify the Productor to put
return value;//return the value
}
}

 

控制台打印:

Productor put 1

Customer get 1

Customer get 2

Productor put 2

Customer get 3

Productor put 3

Customer get 4

Productor put 4

Customer get 5

Productor put 5

Productor put 6

Customer get 6

Productor put 7

Customer get 7

Customer get 8

Productor put 8

Customer get 9

Productor put 9

Customer get 10

Productor put 10


JAVA多线程通信的更多相关文章

  1. java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)

     *java多线程--等待唤醒机制:经典的体现"生产者和消费者模型 *对于此模型,应该明确以下几点: *1.生产者仅仅在仓库未满的时候生产,仓库满了则停止生产. *2.消费者仅仅在有产品的时 ...

  2. 【Java多线程通信】syncrhoized下wait()/notify()与ReentrantLock下condition的用法比较

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6556925.html  一:syncrhoized使用同一把锁的多个线程用通信实现执行顺序的调度 我们知道,使 ...

  3. java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)

    1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以 ...

  4. java多线程通信 例子

    package com.cl.www.thread; public class NumberHolder { private Integer number = 0; // 增加number publi ...

  5. Java 多线程通信之多生产者/多消费者

    // 以生产和消费烤鸭为例 class Resource { private String name; private int count = 1; // 记录烤鸭的编号 private boolea ...

  6. java多线程实现TCP网络Socket编程(C/S通信)

    目录 开篇必知必会 一.多线程技术 二.实现多线程接收 1.单线程版本 2.多线程版本 三.多线程与进程的关系 四.客户端界面完整代码 五.多线程通信对比 最后 开篇必知必会 在前一篇<Java ...

  7. java多线程系列6-阻塞队列

    这篇文章将使用经典的生产者消费者的例子来进一步巩固java多线程通信,介绍使用阻塞队列来简化程序 下面是一个经典的生产者消费者的例子: 假设使用缓冲区存储整数,缓冲区的大小是受限制的.缓冲区提供wri ...

  8. 【多线程】java多线程实现生产者消费者模式

    思考问题: 1.为什么用wait()+notify()实现生产者消费者模式? wait()方法可以暂停线程,并释放对象锁 notify()方法可以唤醒需要该对象锁的其他线程,并在执行完后续步骤,到了s ...

  9. java多线程详解(6)-线程间的通信wait及notify方法

    Java多线程间的通信 本文提纲 一. 线程的几种状态 二. 线程间的相互作用 三.实例代码分析 一. 线程的几种状态 线程有四种状态,任何一个线程肯定处于这四种状态中的一种:(1). 产生(New) ...

随机推荐

  1. except ShortInputException,x中逗号

    class ShortInputException(Exception): def __init__(self, length, atleast): Exception.__init__(self) ...

  2. PHP函数spl_autoload_register()用法和__autoload()介绍(转)

    详细出处参考:http://www.jb51.net/article/29624.htm 又是框架冲突导致__autoload()失效,用spl_autoload_register()重构一下,问题解 ...

  3. Media Queries详细

    @media only screen and (max-device-width: 480px) { //页面最大宽度480px } <link rel="stylesheet&quo ...

  4. mysql sort 性能优化

    http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html 这段时间mysql 数据库的性能明显降低,iowait达到了30, ...

  5. 表ADT

    表一般不用简单数组来实现,通常将其实现为链表.在链表中要不要使用表头则属于个人兴趣问题.在下面的例程中我们都使用表头. 按照C的约定,作为类型的List(表)和Position(位置)以及函数的原型都 ...

  6. c#给用户控件添加事件处理程序

    1.首先在usercontrol后台添加如下代码: public partial class MyControl: UserControl { //添加事件代理       public event ...

  7. Golang学习 - sync 包

    ------------------------------------------------------------ 临时对象池 Pool 用于存储临时对象,它将使用完毕的对象存入对象池中,在需要 ...

  8. 小白日记52:kali渗透测试之Web渗透-HTTPS攻击(Openssl、sslscan、sslyze、检查SSL的网站)

    HTTPS攻击 全站HTTPS正策划稿那位潮流趋势 如:百度.阿里 HTTPS的作用 CIA 解决的是信息传输过程中数据被篡改.窃取 [从中注入恶意代码,多为链路劫持] 加密:对称.非对称.单向 HT ...

  9. js数组的管理[增,删,改,查]

    今天在设计表单的时候遇到对数组的一些处理的问题,比如说怎么创建一个数组,然后牵扯到数组的增删改查的方法.请看API FF: Firefox, N: Netscape, IE: Internet Exp ...

  10. 基于python3的手机号生成脚本

    今天利用业余,自己突发想法来写个手机号码生成的脚本,其实自己用的方法很简单,想必肯定又不少人写的比我的好,我只是自己闲来无聊搞一下, #作者:雷子 #qq:952943386 #日期:2016年7月1 ...