主要理解了两个问题

1.线程数据同步的问题

2.线程交替运行的方式

package ThreadDemo;

/**
* 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题)
* @author lile
* 同步的问题(synchronized 知识点)
* 交替执行的问题(notify ,wait, 线程等待)
*/
public class ThreadDemo {
public static void main(String[] args) {
Food food = new Food();
//生产者
Producter p = new Producter(food);

Consumer c = new Consumer(food);
new Thread(p).start();
new Thread(c).start();

}
}

/**
* 生产者的类
* @author lile
*
*/
class Producter implements Runnable{
private Food food;
public Producter(Food food){
this.food = food;
}
@Override
public void run() {
// TODO Auto-generated method stub

for (int i = 0; i < 100; i++) {
if(i%2==0){
food.set("韭菜炒鸡蛋", "韭菜炒蛋");
}else{
food.set("腰花", "腰花");
}
}
}

}

/**
* 消费这的对象
* @author lile
*
*/
class Consumer implements Runnable{

private Food food;
public Consumer(Food food){
this.food = food;
}

@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i <100; i++) {
food.get();
}

}

}

/**
* 产品对象
* @author lile
*
*/
class Food{
private Boolean flag = true; //true表示可以生产
private String name;
private String content;
public Food(){

}
public Food(String name,String content){
this.name = name;
this.content = content;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}

//制作产品
public synchronized void set(String name,String content){
//两个线程交替运行
if(!flag){
//当前线程等待,没有指定时间,需要其他线程唤醒,释放对象所,让出cpu,不能生产
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

this.setName(name);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setContent(content);
this.flag = false; //表示可以消费
this.notify(); //在监视器唤醒该线程
}

//消费
public synchronized void get(){
//两个线程交替运行
if(flag){
//当前线程等待,没有指定时间,需要其他线程唤醒,释放对象所,让出cpu,不能生产
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.flag = true; //表示可以消费
this.notify(); //在监视器唤醒该线程
System.out.println(this.getName()+this.getContent());
}

}

最新文章

  1. JavaScript(四)——DOM操作——Window.document对象
  2. 【wikioi】1033 蚯蚓的游戏问题(费用流)
  3. Java日期处理类的lenient属性
  4. 工作点滴积累(1)---MD5和编码
  5. servlet 项目
  6. 总结 IOS 7 内存管理
  7. librtmp推流使用aac编码音频的html5和flash播放问题
  8. CodeChef November Challenge 2014
  9. 【转载】逃离adapter的地狱-针对多个View type的组合实现方案
  10. DOS环境下MySQL使用及不同字符集之间的转换
  11. 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
  12. 安装64位Oracle 10g超详细教程
  13. 学习python的*args和 **kwargs
  14. 骗子网站,X毛都没有,骗我九十九
  15. Linux定时任务深入学习
  16. clang++ 链接问题 和 VS Code
  17. Source Code Review
  18. Date中before和after方法的使用
  19. Dart 创建List
  20. sed 的|

热门文章

  1. java-cef嵌入基于Chrome内核浏览器,做页面爬虫(可以尽在ajax异步请求数据)
  2. Guvav:Options使用和避免NULL
  3. JAVA引用和垃圾回收
  4. KafKa介绍(分布式架构)
  5. java多线程面试总结
  6. node使用消息队列RabbitMQ一
  7. NodeJS在线聊天室(NodeJS &amp; SocketIO &amp; Express &amp; EJS &amp; MongoDB &amp; Gulp)
  8. 矢量量化(VQ)
  9. Vulkan Tutorial 07 Window surface
  10. python脚本 随机定位坐标