一、使用wait和notify方法

image.png

  1. package wait.notify;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /*
  5. 使用wait方法和notify方法实现“生产者消费者模式”
  6. */
  7. public class ThreadTest16 {
  8. public static void main(String[] args) {
  9. List list = new ArrayList();
  10. Thread t1 = new Thread(new Producer(list));
  11. Thread t2 = new Thread(new Consumer(list));
  12. t1.setName("生产者线程");
  13. t2.setName("消费者线程");
  14. t1.start();
  15. t2.start();
  16. }
  17. }
  18. //生产线程
  19. class Producer implements Runnable{
  20. //仓库
  21. private List list;
  22. public Producer(List list) {
  23. this.list = list;
  24. }
  25. @Override
  26. public void run() {
  27. //一直生产
  28. while (true){
  29. synchronized (list){
  30. if (list.size() >0){
  31. //等待
  32. try {
  33. list.wait();//当前线程进入等待状态,并且释放锁
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. //仓库空,可以生产
  39. Object obj = new Object();
  40. list.add(obj);
  41. System.out.println(Thread.currentThread().getName()+"--->"+obj);
  42. list.notifyAll();
  43. }
  44. }
  45. }
  46. }
  47. //消费线程
  48. class Consumer implements Runnable{
  49. private List list;
  50. public Consumer(List list) {
  51. this.list = list;
  52. }
  53. @Override
  54. public void run() {
  55. //一直消费
  56. while (true){
  57. synchronized (list){
  58. if (list.size() == 0){
  59. try {
  60. list.wait();
  61. } catch (InterruptedException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. //仓库中有数据,可进行消费
  66. Object obj = list.remove(0);
  67. System.out.println(Thread.currentThread().getName()+"--->"+obj);
  68. //唤醒生产者生产
  69. list.notifyAll();
  70. }
  71. }
  72. }
  73. }

2.实现奇偶输出

  1. package wait.notify;
  2. public class jiou {
  3. public static void main(String[] args) {
  4. Num num = new Num(1);
  5. Thread t1 = new Thread(new ji(num));
  6. Thread t2 = new Thread(new ou(num));
  7. t1.setName("t1");
  8. t2.setName("t2");
  9. t1.start();
  10. t2.start();
  11. }
  12. }
  13. class Num{
  14. int i;
  15. public Num(int i) {
  16. this.i = i;
  17. }
  18. }
  19. class ji implements Runnable{
  20. private Num num;
  21. public ji(Num num) {
  22. this.num = num;
  23. }
  24. @Override
  25. public void run() {
  26. for (int j=0;j<10;j++){
  27. synchronized (num){
  28. if (num.i%2 == 0){
  29. try {
  30. num.wait();
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. System.out.println(Thread.currentThread().getName()+"--->"+(num.i));
  36. num.i++;
  37. num.notifyAll();
  38. }
  39. }
  40. }
  41. }
  42. class ou implements Runnable{
  43. private Num num;
  44. public ou(Num num) {
  45. this.num = num;
  46. }
  47. @Override
  48. public void run() {
  49. for (int j = 0 ;j<10;j++){
  50. synchronized (num){
  51. if (num.i%2 !=0){
  52. try {
  53. num.wait();
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. System.out.println(Thread.currentThread().getName()+"--->"+(num.i)++);
  59. num.notifyAll();
  60. }
  61. }
  62. }
  63. }