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. ccConfig(设置一些底层接口状态:是否支持动作叠加 设置fps更新间隔和位置 是否画边框等。。)

    #ifndef __CCCONFIG_H__ #define __CCCONFIG_H__ #include "platform/CCPlatformConfig.h" /** @ ...

  2. Swift2.0 中的String(三):类型转换

    本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...

  3. vi 命令 使用方法

    一.Unix编辑器概述       编辑器是使用计算机的重要工具之中的一个,在各种操作系统中,编辑器都是不可缺少的部件.Unix及其类似的ix 操作系统系列中,为方便各种用户在各个不同的环境中使用,提 ...

  4. JVM系列文章(四):类载入机制

    作为一个程序猿,只知道怎么用是远远不够的. 起码,你须要知道为什么能够这么用.即我们所谓底层的东西. 那究竟什么是底层呢?我认为这不能一概而论.以我如今的知识水平而言:对于Web开发人员,TCP/IP ...

  5. 获取div相对文档的位置

    获取div相对文档的位置,两个方法 经测试 document.getElementById("btn").getBoundingClientRect() 在IE6下有2像素的bug ...

  6. 关于BP算法在DNN中本质问题的几点随笔 [原创 by 白明] 微信号matthew-bai

       随着deep learning的火爆,神经网络(NN)被大家广泛研究使用.但是大部分RD对BP在NN中本质不甚清楚,对于为什这么使用以及国外大牛们是什么原因会想到用dropout/sigmoid ...

  7. Java开发核心技术面试心得分析

    Java的数据结构有哪些?Map与Set的本质区别是什么? 分析:Java常见的数据结构有Collection和Map,其中Collection接口下包括List和Set接口,其下又有多个实现类如Li ...

  8. Android(java)学习笔记267:Android线程池形态

    1. 线程池简介  多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力.     假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...

  9. 【Linux/Ubuntu学习6】unbuntu 下载android源码

    在Windows下安装Cygwin,通过Cygwin也可在Windows里通过本文的下载步骤下载Android源码. 以下为在Ubuntu下下载Google Android4.4源码的步骤: 1. 安 ...

  10. <c:forEach> 详解

    <c:forEach>标签用于通用数据循环,它有以下属性 属 性 描 述 是否必须 缺省值 items 进行循环的项目 否 无 begin 开始条件 否 0 end 结束条件 否 集合中的 ...