JAVA多线程通信
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多线程通信的更多相关文章
- java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)
*java多线程--等待唤醒机制:经典的体现"生产者和消费者模型 *对于此模型,应该明确以下几点: *1.生产者仅仅在仓库未满的时候生产,仓库满了则停止生产. *2.消费者仅仅在有产品的时 ...
- 【Java多线程通信】syncrhoized下wait()/notify()与ReentrantLock下condition的用法比较
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6556925.html 一:syncrhoized使用同一把锁的多个线程用通信实现执行顺序的调度 我们知道,使 ...
- java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)
1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以 ...
- java多线程通信 例子
package com.cl.www.thread; public class NumberHolder { private Integer number = 0; // 增加number publi ...
- Java 多线程通信之多生产者/多消费者
// 以生产和消费烤鸭为例 class Resource { private String name; private int count = 1; // 记录烤鸭的编号 private boolea ...
- java多线程实现TCP网络Socket编程(C/S通信)
目录 开篇必知必会 一.多线程技术 二.实现多线程接收 1.单线程版本 2.多线程版本 三.多线程与进程的关系 四.客户端界面完整代码 五.多线程通信对比 最后 开篇必知必会 在前一篇<Java ...
- java多线程系列6-阻塞队列
这篇文章将使用经典的生产者消费者的例子来进一步巩固java多线程通信,介绍使用阻塞队列来简化程序 下面是一个经典的生产者消费者的例子: 假设使用缓冲区存储整数,缓冲区的大小是受限制的.缓冲区提供wri ...
- 【多线程】java多线程实现生产者消费者模式
思考问题: 1.为什么用wait()+notify()实现生产者消费者模式? wait()方法可以暂停线程,并释放对象锁 notify()方法可以唤醒需要该对象锁的其他线程,并在执行完后续步骤,到了s ...
- java多线程详解(6)-线程间的通信wait及notify方法
Java多线程间的通信 本文提纲 一. 线程的几种状态 二. 线程间的相互作用 三.实例代码分析 一. 线程的几种状态 线程有四种状态,任何一个线程肯定处于这四种状态中的一种:(1). 产生(New) ...
随机推荐
- iOS开发——项目实战OC篇&类QQ黏性按钮(封装)
类QQ粘性按钮(封装) 那个,先来说说原理吧: 这里原理就是,在界面设置两个控件一个按钮在上面,一个View在下面(同样大小),当我们拖动按钮的时候显示下面的View,view不移动,但是会根据按钮中 ...
- demo virdata 虚拟数据
$pageCurr = I('p',1); $start = ($pageCurr-1) * self::PAGE_RECORD_SCAN; $sort = I('sor ...
- Busybox支持中文的解决办法
转载:http://blog.csdn.net/wavemcu/article/details/7202908 ******************************************** ...
- double数值多时系统默认科学计数法解决方法
比如 Double d = new Double("1234567890.12"); System.out.println("d:="+d); java.tex ...
- CALayer实现点击屏幕放大或者缩小的一个圆
#import "ViewController.h" #define WIDTH 50 @interface ViewController () @end @implementat ...
- Java SortedSet接口
该SortedSet的接口扩展集Set并声明了一组按升序排序的行为.除了通过集定义的那些方法中,SortedSet接口声明总结于下表的方法: 有几种方法没有任何条目包含在调用Set的时候抛出一个NoS ...
- Android之EditText文本变化的监听
监听EditText的文本变化需要给EditText控件加一个addTextChangeListener监听器 editText.addTextChangeListener(textWatcher); ...
- jquery plugins —— datatables ajax post更新数据
通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...
- 索引与优化like查询
1. like %keyword 索引失效,使用全表扫描.但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描. 2. like keyword% 索引有 ...
- Node.js学习笔记(1)
Node是用c++语言开发,能运行javascript语言的环境. 使用的时候格式为node helloworld.js,helloword.js为服务器端或者系统级端的javascript代码. N ...