Java-等待后的执行顺序 [英] Java - order of execution after wait

查看:136
本文介绍了Java-等待后的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部。我对Java等待通知机制有疑问。答案是是否有保证,线程将按此顺序执行-从最后到第一个,依此类推。结果始终为100、99,...,1?这是代码段:

all. I have a question for Java wait-notify mechanism. The answer is is there a guaranty that the threads will be executed in this order - from last to the first etc. the result always will be 100, 99, ... , 1 ? This is the snippet of code:

公共类Main {

static int counter = 0;
static Object o = new Object();

public static void main(String[] args){

    for(int i = 0; i < 100; ++i){
        new Thread(() -> {
            synchronized (o) {
                try {
                    int c = ++counter;
                    o.wait();
                    System.out.println("" + c);
                    Thread.sleep(100);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    synchronized (o) {
        new Thread(()-> {
            synchronized(o){
                System.out.println("LAsttttttttttttttttt");
            }
        }).start();


        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        o.notifyAll();
    }

}

}

我运行了10次,结果始终是相同的。我在互联网上找不到任何有关此的信息。还有一个问题-当我们有100个线程在等待时,当我们notifyAll时,是否保证将执行第一个等待线程,然后再执行第二个,并且在所有100个等待线程都执行完之后,其他等待方法(在同步块中,但在主体中没有wait())将在此之后执行(在所有100个正在等待的线程执行完之后)。还是notifyAll仅保证所有等待线程将开始与此对象同步的每个方法进行斗争?我想这就是答案:
被唤醒的线程将无法继续执行,直到当前
*线程放弃对该对象的锁定。被唤醒的线程
*将在通常与可能与
*竞争竞争该对象的任何其他线程进行同步的方式;例如,
*唤醒的线程在
中没有可靠的特权或劣势*是下一个线程锁定此对象。

I run it 10 times and the result is always the same. I coudn't find anything about this in internet. And one more question - when we have 100 threads waiting, when we notifyAll, is there a guaranty that first one of waiting threads will be executed, after then a second one and after all 100 waiting threads are executed, other waiting methods(which were in synchronized block, but don't have wait() in their body), will be executed after that (after all 100 threads that were waiting are executed). Or notifyAll only guaranty that all waiting threads will start fighting with every method which is synchronized by this object? I thing that this is the answer : "The awakened threads will not be able to proceed until the current * thread relinquishes the lock on this object. The awakened threads * will compete in the usual manner with any other threads that might * be actively competing to synchronize on this object; for example, * the awakened threads enjoy no reliable privilege or disadvantage in * being the next thread to lock this object."

但是我想确保我了解在等待通知时发生了什么。
预先感谢。

But I want to be sure that I understand what's going on when we have wait-notify. Thanks in advance.

推荐答案

否。 。不能保证一组唤醒线程将以任何特定顺序执行。 (这可能是由于JVM的特定实现或运行程序的计算机的速度或许多其他基于负载的变量而有序发生的。但是,没有语言保证。)

No . .there is no guarantee that a set of awoken threads will be executed in any particular order. (This may happen in order because of a particular implementation of JVM or the speed of the computer the program is run on, or many other load based variables. However, there is no language guarantee.)

这篇关于Java-等待后的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆