java多线程2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程2相关的知识,希望对你有一定的参考价值。
今日大部分时间花在了C语言的链表上了,以下是我今日所学习的java多线程内容,今天学习的是多线程里的其他命令,wait,notify,nofityAll,分别是等待,唤醒,全部唤醒。
附今日敲的代码:
1 package com.wsy.product; 2 3 public class Product { 4 private int id; 5 private String name; 6 public Product(int id, String name) { 7 this.id = id; 8 this.name = name; 9 } 10 @Override 11 public String toString() 12 { 13 return "("+"产品id"+id+")"+" "+"("+"产品名:"+name+")"; 14 } 15 public int getId() { 16 return id; 17 } 18 public void setId(int id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 29 30 }
1 package com.wsy.product; 2 3 public class Storage { 4 //仓库的总容量为10 5 private Product[] products = new Product[10]; 6 private int count = 0; 7 //生产者将产品放入仓库 8 public synchronized void push(Product product) 9 { 10 //如果条件满足表示仓库已经满了 11 while(count == products.length) 12 { 13 try { 14 System.out.println("producer wait!"); 15 wait(); 16 } catch (InterruptedException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 } 21 //将产品放入仓库 22 products[count++] = product ; 23 System.out.println(Thread.currentThread().getName()+" "+"生产了"+product); 24 //将所有线程唤醒 25 System.out.println("notifyAll!"); 26 notifyAll(); 27 } 28 //消费者将产品拿出仓库 29 public synchronized void pop() 30 { 31 //count等于0代表仓库现在为空 需要等待生产者生产 32 while(count == 0 ) 33 { 34 try { 35 System.out.println("consumer wait!"); 36 wait(); 37 } catch (InterruptedException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 count--; 43 Product product = products[count]; 44 products[count] = null; 45 System.out.println(Thread.currentThread().getName()+" "+"消费了"+product); 46 //将所有线程唤醒 47 System.out.println("notifyAll!"); 48 notifyAll(); 49 } 50 }
1 package com.wsy.product; 2 3 public class Consumer implements Runnable { 4 private Storage storage; 5 public Consumer(Storage storage) 6 { 7 this.storage = storage; 8 } 9 public void run() 10 { 11 int i = 0; 12 while(i<10) 13 { 14 i++; 15 storage.pop(); 16 17 } 18 } 19 }
1 package com.wsy.product; 2 3 import java.util.Random; 4 5 public class Producer implements Runnable { 6 private Storage storage; 7 public Producer(Storage storage) 8 { 9 this.storage = storage; 10 } 11 @Override 12 public void run() 13 { 14 int i =0; 15 Random r = new Random(); 16 while(i<10) 17 { 18 i++; 19 Product product = new Product(i , "序号"+Integer.toString(r.nextInt(100))); 20 storage.push(product); 21 } 22 } 23 24 25 }
1 package com.wsy.product; 2 /** 3 * 经典生产者与消费者问题 4 * 生产者不断的往仓库中存放产品,消费者从仓库中消费产品。 5 * 其中生产者和消费者都可以有若干个。 6 * 仓库规则:容量有限,库满时不能存放,库空时不能取产品 。 7 */ 8 public class ProductTest { 9 10 public static void main(String[] args) { 11 Storage storage = new Storage(); 12 Thread consumer1 = new Thread(new Consumer(storage)); 13 consumer1.setName("消费者1"); 14 Thread consumer2 = new Thread(new Consumer(storage)); 15 consumer2.setName("消费者2"); 16 Thread producer1 = new Thread(new Producer(storage)); 17 producer1.setName("生产者1"); 18 Thread producer2 = new Thread(new Producer(storage)); 19 producer2.setName("生产者2"); 20 producer1.start(); 21 producer2.start(); 22 try { 23 Thread.sleep(100); 24 } catch (InterruptedException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 consumer1.start(); 29 consumer2.start(); 30 31 } 32 33 }
以上是关于java多线程2的主要内容,如果未能解决你的问题,请参考以下文章