current position:Home>On the problem of multi-threaded ball taking and putting (language Java)

On the problem of multi-threaded ball taking and putting (language Java)

2022-02-02 23:41:11 CSDN Q & A

When learning multithreading , Want to write a code for taking and putting the ball , Then I want to loop this operation 10 Time , But in the end, only 1 Time .

With i++ Simulated release ,i-- Simulated ball taking

public class SyncTest {    private static int i=0;    static class Thread1 implements Runnable{        @Override        public  void run() {            for (int a=0;a<10;a++){                synchronized(this){                    if (i > 0) {                        try {                            wait();                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                    i++;                    notify();                    System.out.println(i);            }        }        }    }    static class Thread2 implements Runnable{        @Override        public  void run() {            for (int a=0;a<10;a++) {                synchronized (this) {                    if (i == 0) {                        try {                            wait();                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                    i--;                    notify();                    System.out.println(i);                }            }        }    }    public static void main(String[] args) {        new Thread(new Thread1()).start();        new Thread(new Thread2()).start();    }}

img

img

debug It was found that both threads wait 了 . If not, these two threads end up looping , Instead, create a thread in the loop of the main thread to achieve the effect , Thread creation and destruction are always bad .

Is there any better way




Refer to the answer 1:

package com;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**

  • @Description:

  • @ClassName: test3

  • @Author: yokna

  • @Date: 2021/12/17 14:59

  • @Version: 1.0

  • /
    public class test3 {
    private static int i=0;
    private static String lock = "";
    static class Thread1 implements Runnable{

      @Override  public void run() {      for (int a=0;a<10;a++){          synchronized(lock){              if (i > 0) {                  try {                      lock.wait();                  } catch (InterruptedException e) {                      e.printStackTrace();                  }              }              i++;              lock.notify();              System.out.println(i);          }      }  }

    }
    static class Thread2 implements Runnable{

      @Override  public void run() {      for (int a=0;a<10;a++) {          synchronized (lock) {              if (i == 0) {                  try {                      lock.wait();                  } catch (InterruptedException e) {                      e.printStackTrace();                  }              }              i--;              lock.notify();              System.out.println(i);          }      }  }

    }
    public static void main(String[] args) {

      new Thread(new Thread1()).start();  new Thread(new Thread2()).start();

    }

}




Refer to the answer 2:



Refer to the answer 3:

You don't seem to understand the inner class this Keywords mean , Two this Representing different objects ,sync It's the object of the lock , Equivalent to two locks , Every time it reaches wait When , The thread hangs , Can't wait for other threads to wake up , Because the lock object is different , So only once .




Refer to the answer 4:



Refer to the answer 5:

img


Explain it like this , You'll see it intuitively .




Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022341095585.html

Random recommended