多线程—生产者于消费者实例
来源:互联网 发布:淘宝买的基金如何赎回 编辑:程序博客网 时间:2024/06/06 02:18
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
只函数Main
public static void main(String[] args)
{// TODO Auto-generated method stub
ZiYuan zy = new ZiYuan();
Shengchan sc = new Shengchan(zy);
XiaoFei xf = new XiaoFei(zy);
Thread th1 = new Thread(sc);
Thread th2 = new Thread(xf);
Thread th3 = new Thread(sc);
Thread th4 = new Thread(xf);
th1.start();
th2.start();
th3.start();
th4.start();
}
}
共享资源
class ZiYuan
{
private String name ;
private int sex = 1 ;
private boolean flse = false;
//生成方法
public synchronized void Shengchan(String name)
{
while (flse)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name+"......"+sex++ ;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flse = true ;
this.notifyAll();
}
//消费者方法
public synchronized void Xiaofei()
{
while (!flse)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+".....消费者....."+this.name);
flse = false;
this.notifyAll();
}
}
//生产者对象
class Shengchan implements Runnable
{
private ZiYuan zy ;
public Shengchan(ZiYuan zy)
{
this.zy = zy ;
}
@Override
public void run()
{
while (true)
{
zy.Shengchan("烤鸭");
}
}
}
//消费者
class XiaoFei implements Runnable
{
private ZiYuan zy ;
public XiaoFei(ZiYuan zy)
{
this.zy = zy ;
}
@Override
public void run()
{
while (true)
{
zy.Xiaofei();
}
}
}
- 多线程—生产者于消费者实例
- 多线程:生产者消费者实例
- C#多线程(二)——生产者和消费者实例
- 经典多线程Java实例 生产者与消费者
- JAVA多线程实例(生产者与消费者问题)
- 经典多线程实例:生产者消费者问题
- 多线程经典实例;生产者和消费者
- Java多线程之生产者消费者实例
- 多线程经典的 生产者-消费者 实例
- 关于Java多线程生产者消费者实例
- 多线程——生产者消费者
- linux下C语言实现多线程通信—环形缓冲区,可用于生产者(producer)/消费者(consumer)
- java多线程——生产者、消费者
- java多线程应用——生产者消费者
- 多线程——生产者/消费者问题
- JAVA多线程实现生产者——消费者
- -java多线程——生产者和消费者
- Java多线程——生产者消费者模式
- java备份MySQL (windows linux)
- android adb 命令发送 keyevent
- 职业规划
- 破解Xcode实现无证书真机调试(支持Xcode4.6.2)
- poj 2239 二分 最大匹配
- 多线程—生产者于消费者实例
- JS “缺少标识符 字符串或数字”错误
- 控制台下的扫雷
- Sqoop使用和参数简介
- Android分享介绍
- 错题集1.
- Django Ubuntu:Database returned an invalid value in QuerySet.dates(). 错误的解决方法
- 【成佩涛】纯属扯淡,常人别进
- MyEclipse 使用JPA方式逆向生成实体类POJO以及DAO类