wait和notify简单应用1

偏执的太偏执、 2022-05-25 12:08 220阅读 0赞
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. //一个线程去读取List的长度,如果List为空的时候,线程就wait,如果List中有值了,就notify去获取它的长度
  5. public class ListAwit {
  6. private static List<Integer> list = new ArrayList<Integer>();
  7. final static Random rand = new Random();
  8. public static void main(String[] args) {
  9. final ListAwit la = new ListAwit();
  10. new Thread(new Runnable(){
  11. public void run() {
  12. while(true){
  13. if(list.size() > 0){
  14. synchronized(la){
  15. la.notify();
  16. }
  17. }
  18. sleep();
  19. }
  20. }})
  21. .start();
  22. while(true){
  23. try {
  24. System.out.println(Thread.currentThread().getName()+"等待.........");
  25. synchronized(la){
  26. la.wait();
  27. }
  28. sleep();
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. }
  32. System.out.println(Thread.currentThread().getName()+"##长度:"+list.size());
  33. System.out.println(list);
  34. }
  35. }
  36. private static void sleep(){
  37. try {
  38. int randint = rand.nextInt(2000);
  39. Thread.sleep(randint);
  40. list.add(randint);
  41. if(rand.nextInt(10)==5){
  42. list.clear();
  43. }
  44. } catch (InterruptedException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. /**
  49. * 运行结果:
  50. main等待.........
  51. main##长度:4
  52. [289, 887, 41, 1192]
  53. main等待.........
  54. main##长度:8
  55. [289, 887, 41, 1192, 1322, 238, 641, 1608]
  56. main等待.........
  57. */
  58. }

实现一种类似信号控制的程序

  1. public class SynchronizedMutex {
  2. private Thread curOwner = null;
  3. public synchronized void acquire() throws InterruptedException{
  4. System.out.println("当前线程:"+Thread.currentThread().getName());
  5. while(curOwner != null){
  6. this.wait();
  7. }
  8. this.curOwner = Thread.currentThread();
  9. }
  10. public synchronized void release() throws InterruptedException{
  11. System.out.println("当前线程:"+Thread.currentThread().getName());
  12. while(curOwner == Thread.currentThread()){
  13. this.curOwner = null;
  14. this.notify();
  15. }
  16. }
  17. }
  18. import java.util.Random;
  19. import java.util.concurrent.ExecutorService;
  20. import java.util.concurrent.Executors;
  21. import java.util.concurrent.Semaphore;
  22. import java.util.concurrent.TimeUnit;
  23. public class MutexTest{
  24. // private Semaphore mutex = new Semaphore(1);
  25. private SynchronizedMutex mutex = new SynchronizedMutex();
  26. public static void main(String[] args) {
  27. final MutexTest test = new MutexTest();
  28. Runnable task = new Runnable(){
  29. @Override
  30. public void run() {
  31. while(true){
  32. int i = new Random().nextInt(3);
  33. switch(i){
  34. case 0:test.add();
  35. case 1:test.add1();
  36. case 2:test.del();
  37. default:test.add();
  38. }
  39. }
  40. }
  41. };
  42. ExecutorService exec = Executors.newCachedThreadPool();
  43. for(int i=0; i<5; i++){
  44. exec.execute(task);
  45. }
  46. }
  47. private static int value;
  48. public void add(){
  49. try {
  50. mutex.acquire();
  51. TimeUnit.SECONDS.sleep(1);
  52. System.out.println(Thread.currentThread().getName()+"增一:"+ (++value));
  53. mutex.release();
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. public void add1(){
  59. try {
  60. mutex.acquire();
  61. TimeUnit.SECONDS.sleep(1);
  62. value = value + 2;
  63. System.out.println(Thread.currentThread().getName()+"增二:"+ value);
  64. mutex.release();
  65. } catch (InterruptedException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. public void del(){
  70. try {
  71. mutex.acquire();
  72. TimeUnit.SECONDS.sleep(1);
  73. System.out.println(Thread.currentThread().getName()+"减一:"+ (--value));
  74. mutex.release();
  75. } catch (InterruptedException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. /**
  80. * 运行结果:
  81. * 当前线程:pool-1-thread-2
  82. 当前线程:pool-1-thread-4
  83. 当前线程:pool-1-thread-5
  84. 当前线程:pool-1-thread-3
  85. 当前线程:pool-1-thread-1
  86. pool-1-thread-2减一:-1
  87. 当前线程:pool-1-thread-2
  88. 当前线程:pool-1-thread-2
  89. pool-1-thread-4增一:0
  90. 当前线程:pool-1-thread-4
  91. 当前线程:pool-1-thread-4
  92. pool-1-thread-4增二:2
  93. 当前线程:pool-1-thread-4
  94. 当前线程:pool-1-thread-4
  95. pool-1-thread-4减一:1
  96. 当前线程:pool-1-thread-4
  97. 当前线程:pool-1-thread-4
  98. pool-1-thread-4增一:2
  99. 当前线程:pool-1-thread-4
  100. 当前线程:pool-1-thread-4
  101. pool-1-thread-4减一:1
  102. 当前线程:pool-1-thread-4
  103. 当前线程:pool-1-thread-4
  104. */
  105. }

发表评论

表情:
评论列表 (有 0 条评论,220人围观)

还没有评论,来说两句吧...

相关阅读