Is there a sequence in which threads compete for resources?

suppose that thread 1 executes wait (), thread 2 to acquire lock resources, while thread 3 starts to execute synchronized to compete for lock resources, and thread 2 uses notifyAll () to wake up thread 1 before ending, so which thread 1 or thread 3 can acquire the lock at random?
in my understanding, it is random, but now the actual situation is that thread 1 will get the resource
I understand it wrong, please explain.

and after thread 1 executes wait, thread 4 first acquires the lock resource, but also executes wait, and then continues as above, why thread 1 and thread 4 are executed when thread 2 ends.

public class ThreadTest {
    public static void main(String[] args) {
        Object co = new Object();
        System.out.println(co);

        for (int i = 0; i < 10; iPP) {
            MyThread t = new MyThread("Thread" + i, co);
            t.start();
        }

        try {
            TimeUnit.SECONDS.sleep(2);
            System.out.println("-----Main Thread notify-----");
            synchronized (co) {
                co.notify();
            }
            TimeUnit.SECONDS.sleep(2);
            System.out.println("Main Thread is end.");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class MyThread extends Thread {
        private String name;
        private Object co;

        public MyThread(String name, Object o) {
            this.name = name;
            this.co = o;
        }

        @Override
        public void run() {
            System.out.println(name + " is waiting.");
            try {
                synchronized (co) {
                    co.wait();
                }
                System.out.println(name + " has been notified.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

for example, this code, regardless of whether it will cause a deadlock, the problem is that the notify released in main must be the first to execute wait, isn"t it random?

Mar.26,2021

I tested this
changed your co.notify () to notifyAll (), and it turns out that every time Thread-0 is awakened first, and then everything else is random.
currently I can only think of it as compiler optimization.
if you want to wake up randomly, take a nap to relieve your sorrows

        @Override
        public void run() {
            System.out.println(name + " is waiting. theThreadName :"+Thread.currentThread().getName());
            try {
                Thread.currentThread().sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                synchronized (co) {
                    co.wait();
                }
                System.out.println(name + " has been notified theThreadName :"+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

clipboard.png


I have found the answer on stackoverflow. If you are interested, please take a look at
https://stackoverflow.com/que.

.

I found a new answer
https://createchance.github.io/post/java-%E5%B9%B6%E5 % 8F%91%E4%B9%8B%E5%9F%BA%E7%9F%B3%E7%AF%87/-sharp%E5%94%A4%E9%86%92%E7%AD%96%E7%95%A5-0

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e83b6e-46a56.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e83b6e-46a56.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?