标签:package object public 消费者 double
package com.yuanzijian01;
/*
* 线程通讯: 一个线程完成了自己的任务时,要通知另外一个线程去完成另外一个任务.
生产者与消费者
wait(): 等待 如果线程执行了wait方法,那么该线程会进入等待的状态,等待状态下的线程必须要被其他线程调用notify方法才能唤醒。
notify(): 唤醒 唤醒线程池等待线程其中的一个。
notifyAll() : 唤醒线程池所有等待 线程。
wait与notify方法要注意的事项:
1. wait方法与notify方法是属于Object对象 的。
2. wait方法与notify方法必须要在同步代码块或者是同步函数中才能 使用。
3. wait方法与notify方法必需要由锁对象调用。
问题一:出现了线程安全问题。 价格错乱了...
*/
//产品类
class Product{
String name;
double price;
boolean flag = false;
}
class Producer extends Thread{
Product p;
public Producer(Product p){
this.p = p;
}
@Override
public void run() {
// TODO 自动生成的方法存根
super.run();
int i = 0;
while(true){
synchronized(p){
if(p.flag==false){
if(i%2==0){
p.name = "苹果";
p.price = 6.5;
}else{
p.name="香蕉";
p.price = 2.0;
}
System.out.println("生产者生产出了:"+ p.name+" 价格是:"+ p.price);
p.flag = true;
i++;
p.notifyAll(); //唤醒消费者去消费
}else{
try{
p.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
}
//消费者
class Customer extends Thread{
Product p;
public Customer(Product p) {
this.p = p;
}
@Override
public void run() {
while(true){
synchronized (p) {
if(p.flag==true){ //产品已经生产完毕
System.out.println("消费者消费了"+p.name+" 价格:"+ p.price);
p.flag = false;
p.notifyAll(); // 唤醒生产者去生产
}else{
//产品还没有生产,应该 等待生产者先生产。
try {
p.wait(); //消费者也等待了...
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class Demo01 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Product p = new Product(); //产品
//创建生产对象
Producer producer = new Producer(p);
//创建消费者
Customer customer = new Customer(p);
//调用start方法开启线程
producer.start();
customer.start();
}
}2.线程的停止
package com.yuanzijian01;
/*
* 线程通讯: 一个线程完成了自己的任务时,要通知另外一个线程去完成另外一个任务.
生产者与消费者
wait(): 等待 如果线程执行了wait方法,那么该线程会进入等待的状态,等待状态下的线程必须要被其他线程调用notify方法才能唤醒。
notify(): 唤醒 唤醒线程池等待线程其中的一个。
notifyAll() : 唤醒线程池所有等待 线程。
wait与notify方法要注意的事项:
1. wait方法与notify方法是属于Object对象 的。
2. wait方法与notify方法必须要在同步代码块或者是同步函数中才能 使用。
3. wait方法与notify方法必需要由锁对象调用。
问题一:出现了线程安全问题。 价格错乱了...
*/
//产品类
class Product{
String name;
double price;
boolean flag = false;
}
class Producer extends Thread{
Product p;
public Producer(Product p){
this.p = p;
}
@Override
public void run() {
// TODO 自动生成的方法存根
super.run();
int i = 0;
while(true){
synchronized(p){
if(p.flag==false){
if(i%2==0){
p.name = "苹果";
p.price = 6.5;
}else{
p.name="香蕉";
p.price = 2.0;
}
System.out.println("生产者生产出了:"+ p.name+" 价格是:"+ p.price);
p.flag = true;
i++;
p.notifyAll(); //唤醒消费者去消费
}else{
try{
p.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
}
//消费者
class Customer extends Thread{
Product p;
public Customer(Product p) {
this.p = p;
}
@Override
public void run() {
while(true){
synchronized (p) {
if(p.flag==true){ //产品已经生产完毕
System.out.println("消费者消费了"+p.name+" 价格:"+ p.price);
p.flag = false;
p.notifyAll(); // 唤醒生产者去生产
}else{
//产品还没有生产,应该 等待生产者先生产。
try {
p.wait(); //消费者也等待了...
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public class Demo01 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Product p = new Product(); //产品
//创建生产对象
Producer producer = new Producer(p);
//创建消费者
Customer customer = new Customer(p);
//调用start方法开启线程
producer.start();
customer.start();
}
}3.守护线程
package com.yuanzijian03;
/*
守护线程(后台线程):在一个进程中如果只剩下 了守护线程,那么守护线程也会死亡。
需求: 模拟QQ下载更新包。
一个线程默认都不是守护线程。
*/
public class Demo03 extends Thread {
public Demo03(String name){
super(name);
}
@Override
public void run() {
for(int i = 1 ; i<=100 ; i++){
System.out.println("更新包目前下载"+i+"%");
if(i==100){
System.out.println("更新包下载完毕,准备安装..");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Demo03 d = new Demo03("后台线程");
d.setDaemon(true); //setDaemon() 设置线程是否为守护线程,true为守护线程, false为非守护线程。
// System.out.println("是守护线程吗?"+ d.isDaemon()); //判断线程是否为守护线程。
d.start();
for(int i = 1 ; i<=100 ; i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}4.join方法加入
package cn.itcast.thread;
/*
join方法。 加入
*/
//老妈
class Mon extends Thread{
public void run() {
System.out.println("妈妈洗菜");
System.out.println("妈妈切菜");
System.out.println("妈妈准备炒菜,发现没有酱油了..");
//叫儿子去打酱油
Son s= new Son();
s.start();
try {
s.join(); //加入。 一个线程如果执行join语句,那么就有新的线程加入,执行该语句的线程必须要让步给新加入的线程先完成任务,然后才能继续执行。
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("妈妈继续炒菜");
System.out.println("全家一起吃饭..");
}
}
class Son extends Thread{
@Override
public void run() {
System.out.println("儿子下楼..");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("儿子一直往前走");
System.out.println("儿子打完酱油了");
System.out.println("上楼,把酱油给老妈");
}
}
public class Demo8 {
public static void main(String[] args) {
Mon m = new Mon();
m.start();
}
}标签:package object public 消费者 double
原文地址:http://hnyuanzijian.blog.51cto.com/8869344/1785318