Java——线程通信

来源:互联网 发布:淘宝u站九块邮 编辑:程序博客网 时间:2024/06/07 22:56

Java——线程通信

线程通信中的三个方法:
  • wait()   将当前进程挂起,释放CPU,进入等待队列
  • notify()   将线程等待队列中优先级最高的线程唤醒
  • notifyAll()  将等待队列中的所有线程唤醒
注°只能在同步代码块或者同步方法中使用
1、使用线程通信使得两个账户交替存钱
package com.kexin.study2;public class TestCommunication {public static void main(String[] args) {Account acc = new Account();User user1 = new User(acc);User user2 = new User(acc);user1.setName("甲");user2.setName("乙");user1.start();user2.start();}}class Account{private double money;public synchronized void setMoney(double money){//将线程唤醒notify();this.money += money;System.out.println(Thread.currentThread().getName()+"---->"+this.money);try {//将线程挂起wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}class User extends Thread{private Account acc;public User(Account acc){this.acc = acc;}@Overridepublic void run() {for(int i =0 ;i<3;i++)acc.setMoney(1000);}}
2、生产者/消费者问题
生产者/消费者问题是讲解线程必提的一个例子,意思是生产者和消费者两个线程共享一个buffer,生产者往里生产东西,消费者从里面取东西,而两者不可同时访问这个buffer
package com.kexin.study2;public class TestProductConsume {public static void main(String[] args) {ProductBuffer pb = new ProductBuffer();Product p = new Product(pb);Product p1 = new Product(pb);Consume c = new Consume(pb);p.setName("生产者");p1.setName("生产者1");c.setName("消费者");p.start();p1.start();c.start();}}//产品BUfferclass ProductBuffer{int product_nums = 0;//现有的产品数private final int max_buffer = 5;//设置缓冲池大小//生产产品public synchronized void setProduct(){if(product_nums>=max_buffer){try {wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{product_nums++;System.out.println(Thread.currentThread().getName()+"生产了第"+product_nums+"个产品");notifyAll();}}//消费产品public synchronized void getProduct(){if(product_nums<=0){try {wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{product_nums--;System.out.println(Thread.currentThread().getName()+"消费了第"+product_nums+"个产品");notifyAll();}}}//生产者线程class Product extends Thread{ProductBuffer buffer;public Product(ProductBuffer buffer){this.buffer = buffer;}@Overridepublic void run() {//生产者生产产品System.out.println("生产者生产产品...");while(true){buffer.setProduct();}}}//消费者线程class Consume extends Thread{ProductBuffer buffer;public Consume(ProductBuffer buffer){this.buffer = buffer;}@Overridepublic void run() {//消费者消费产品try {Thread.currentThread().sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("消费者消费产品...");while(true){buffer.getProduct();}}}
线程over了大笑
0 0
原创粉丝点击