Java代码

  1. package com.thread.example;
  2.  
  3. import java.util.LinkedList;
  4.  
  5.  
  6.  
  7.  
  8.  
  9. public class Example10 {
  10.  
  11. private LinkedList<Object> myList = new LinkedList<Object>();
  12.  
  13. private int MAX = 10;
  14.  
  15. public Example10() {
  16. super();
  17. }
  18.  
  19. public void start(){
  20. new Producerder().start();
  21. new Consumereder().start();
  22. }
  23.  
  24.  
  25.  
  26. class Producerder extends Thread {
  27.  
  28. @Override
  29. public void run() {
  30. for (int i = 0; i < 15; i++) {
  31. try {
  32. this.sleep(1000);
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. synchronized (myList) {
  37. try {
  38. while(myList.size() == MAX){
  39. System.out.println("warning: it's full!");
  40. myList.wait();
  41. }
  42.  
  43. //*************把这一段注释掉********************
  44. Object o = new Object();
  45. if(myList.add(o)){
  46. System.out.println("Producer: " + o);
  47. myList.notify();
  48. }
  49. //*******************************
  50. //把上面注释掉后,启用下面一行后。你再在Consumereder中分别是用while和if看下输出结果
  51. //myList.notify();
  52. } catch (Exception e) {
  53. System.out.println("producer is interrupted!");
  54. }
  55. }
  56. }
  57. }
  58.  
  59. }
  60.  
  61.  
  62. class Consumereder extends Thread {
  63.  
  64. @Override
  65. public void run() {
  66. for (int i = 0; i < 15; i++) {
  67. try {
  68. this.sleep(100);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72.  
  73. synchronized (myList) {
  74. try {
  75. while(myList.size() == 0){
  76. System.out.println("warning: it's empty!");
  77. myList.wait();
  78. }
  79. System.out.println("oooooooooooooooooo");
  80. Object o = myList.removeLast();
  81. System.out.println("Consumereder" + o);
  82. myList.notify();
  83. } catch (Exception e) {
  84. System.out.println("consumer is interrupted!");
  85. }
  86. }
  87. }
  88. }
  89.  
  90. }
  91.  
  92.  
  93. public static void main(String[] args) {
  94. Example10 ex10 = new Example10();
  95. ex10.start();
  96. }
  97. }
package com.thread.example;import java.util.LinkedList;public class Example10 {	private LinkedList myList = new LinkedList();		private int MAX = 10;		public Example10() {		super();	}	public void start(){		new Producerder().start();		new Consumereder().start();	}	class Producerder extends Thread {				@Override		public void run() {			for (int i = 0; i < 15; i++) {				try {					this.sleep(1000);				} catch (Exception e) {					e.printStackTrace();				}				synchronized (myList) {					try {						while(myList.size() == MAX){							System.out.println("warning: it's full!");							myList.wait();						}												//*************把这一段注释掉********************						Object o = new Object();						if(myList.add(o)){							System.out.println("Producer: " + o);							myList.notify();						}						//*******************************						//把上面注释掉后,启用下面一行后。你再在Consumereder中分别是用while和if看下输出结果						//myList.notify();					} catch (Exception e) {						System.out.println("producer is interrupted!");					}				}			}		}			}			class Consumereder extends Thread {				@Override		public void run() {			for (int i = 0; i < 15; i++) {				try {					this.sleep(100);				} catch (Exception e) {					e.printStackTrace();				}								synchronized (myList) {					try {						while(myList.size() == 0){							System.out.println("warning: it's empty!");							myList.wait();						}						System.out.println("oooooooooooooooooo");						Object o = myList.removeLast();						System.out.println("Consumereder" + o);						myList.notify();					} catch (Exception e) {						   System.out.println("consumer is interrupted!");					}				}			}		}			}		public static void main(String[] args) {		Example10 ex10 = new Example10();		ex10.start();	}}

注:根据代码中注释的操作运行一次,你就明白什么意思了;

如果生产者没有生产,就调用唤醒(notify)。消费者的判断会出问题(如果使用if)

这就是为什么使用while而不是if的原因