wait notify - 图1
wait notify - 图2

  1. @Slf4j(topic = "c.TestWaitNotify")
  2. public class TestWaitNotify {
  3. static final Object lock = new Object();
  4. public static void main(String[] args) throws InterruptedException {
  5. new Thread(()->{
  6. log.info("执行。。。。");
  7. synchronized (lock){
  8. try {
  9. lock.wait();
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. },"t1").start();
  15. new Thread(()->{
  16. log.info("执行。。。");
  17. synchronized (lock){
  18. try {
  19. lock.wait();
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. },"t2").start();
  25. TimeUnit.SECONDS.sleep(2);
  26. log.info("main thread 唤醒其他线程");
  27. synchronized (lock){
  28. lock.notify();// 唤醒t1和t2线程中其中一个
  29. // lock.notifyAll();// 唤醒所有synchronized wait 的线程
  30. }
  31. }
  32. }
  1. @Slf4j
  2. public class TestCorrectPostureStep1 {
  3. static final Object room = new Object();
  4. static boolean hasCigarette = false;
  5. static boolean hasTakeout = false;
  6. public static void main(String[] args) {
  7. new Thread(()->{
  8. synchronized (room){
  9. if (!hasCigarette){
  10. try {
  11. log.info("没有烟,我要等一会才能工作");
  12. room.wait();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. if (hasCigarette){
  18. log.debug("有烟,可以干活了");
  19. }
  20. }
  21. },"t1").start();
  22. for (int i = 0;i<5;i++){
  23. new Thread(()->{
  24. synchronized (room){
  25. try {
  26. Thread.sleep(2_000);
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. log.debug("开始干活了");
  31. }
  32. },"其他人"+i).start();
  33. }
  34. try {
  35. Thread.sleep(1_000);
  36. } catch (InterruptedException e) {
  37. e.printStackTrace();
  38. }
  39. new Thread(()->{
  40. synchronized (room) {
  41. hasCigarette = true;
  42. room.notify();
  43. }
  44. },"松烟的").start();
  45. }
  46. }

虚假唤醒

  1. package org.example.jvm;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.concurrent.TimeUnit;
  4. /**
  5. * @author huskyui
  6. */
  7. @Slf4j
  8. public class TestCorrectPostureStep3 {
  9. final static Object lock = new Object();
  10. static boolean hasCigarette = false;
  11. static boolean hasTakeout = false;
  12. public static void main(String[] args) throws InterruptedException {
  13. new Thread(()->{
  14. synchronized (lock){
  15. log.info("有外卖吗 {}",hasTakeout);
  16. try{
  17. lock.wait();
  18. }catch (Exception e){
  19. e.printStackTrace();
  20. }
  21. log.info("有外卖没 {}",hasTakeout);
  22. if (hasTakeout){
  23. log.debug("可以干活了。有外卖了");
  24. }else{
  25. log.info("没干成活。。。");
  26. }
  27. }
  28. },"小女").start();
  29. new Thread(()->{
  30. synchronized (lock){
  31. log.info("有烟吗 {}",hasCigarette);
  32. try{
  33. lock.wait();
  34. }catch (Exception e){
  35. e.printStackTrace();
  36. }
  37. log.info("有烟没 {}",hasCigarette);
  38. if (hasCigarette){
  39. log.debug("可以干活了。有烟了");
  40. }else{
  41. log.info("没干成活。。。");
  42. }
  43. }
  44. },"小南").start();
  45. TimeUnit.SECONDS.sleep(2);
  46. new Thread(()->{
  47. synchronized (lock){
  48. hasCigarette = true;
  49. lock.notifyAll();
  50. }
  51. }).start();
  52. }
  53. public final void dos(){
  54. }
  55. }

使用while,可以防止虚假唤醒

  1. package org.example.jvm;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.concurrent.TimeUnit;
  4. /**
  5. * @author huskyui
  6. */
  7. @Slf4j
  8. public class TestCorrectPostureStep3 {
  9. final static Object lock = new Object();
  10. static boolean hasCigarette = false;
  11. static boolean hasTakeout = false;
  12. public static void main(String[] args) throws InterruptedException {
  13. new Thread(() -> {
  14. synchronized (lock) {
  15. log.info("有外卖吗 {}", hasTakeout);
  16. while (!hasTakeout) {
  17. log.info("没有外卖先歇会");
  18. try {
  19. lock.wait();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. log.info("有外卖没 {}", hasTakeout);
  25. if (hasTakeout) {
  26. log.debug("可以干活了。有外卖了");
  27. } else {
  28. log.info("没干成活。。。");
  29. }
  30. }
  31. }, "小女").start();
  32. new Thread(() -> {
  33. synchronized (lock) {
  34. log.info("有烟吗 {}", hasCigarette);
  35. while (!hasCigarette) {
  36. log.info("没烟先歇会");
  37. try {
  38. lock.wait();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. log.info("有烟没 {}", hasCigarette);
  44. if (hasCigarette) {
  45. log.debug("可以干活了。有烟了");
  46. } else {
  47. log.info("没干成活。。。");
  48. }
  49. }
  50. }, "小南").start();
  51. TimeUnit.SECONDS.sleep(2);
  52. new Thread(() -> {
  53. synchronized (lock) {
  54. hasCigarette = true;
  55. lock.notifyAll();
  56. }
  57. }).start();
  58. }
  59. }

wait notify - 图3