本文介绍了同步并通知执行顺序和作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)在该线程同步代码块内调用notify时,或者一旦退出同步代码块后,线程是否会放弃其锁定?

1) Does a thread relinquish it's lock as soon as notify is invoked within that threads synchronized code block, or once the synchronized code block is exited?

例如,似乎没有指定线程优先级,我的Process类将自上而下执行. Produce将首先被调用,它将执行操作,然后是wait();消费将运行,然后命中notify(),它将显示"Done"或将首先延迟5秒,然后将打印"Done"?

For instance, it seems like without specifying thread priority, my Process class will execute from top to bottom. Produce will be called first, it will do stuff, then wait(); consume will run, and then it hits notify(), which will print "Done" or will there be a 5 second delay first, then "Done" will be printed?

2)另外,如果我有第三个同步方法/线程,它不等待也不通知,而只是运行,我可以先验地预测我的操作执行的顺序吗?

2) Also, if I had a third synchronized method/thread, that does not wait or notify and just runs, can I predict the order in which my operations are executed a priori?

3)最后,notify()如何知道要唤醒"的线程?例如,假设我有多个进程,每个进程同时执行2个线程,并且每个进程A,B和C都调用notify().每个notify()是否在每个Process本地?换句话说,如果进程A调用notify(),是否可以唤醒进程B中的等待线程?到目前为止,我已经看到了同步调用(this),这使我认为它是指特定的对象类,这反过来又使我认为,同步块中所有notify()的调用都受sync()中的此对象约束.这是指,因此不会有交叉.

3) Finally, how does notify() know what thread to 'wake up'? Like, let's say I have multiple Processes each executing 2 threads simultaneously, and each Process A, B, and C evoke notify( ). Is each notify( ) local to each Process? In other words, if Process A calls notify( ), can that wake up a waiting thread in Process B? So far I've seen synchronized called on (this), which makes me think its referring to a particular object class, which in turn makes me think that all notify( )'s called in synchronized blocks are bound by whatever this in synchronized(this) is referring to, so there would be no cross-over.

public class Process {

public void produce( ) {
  synchronized(this) {
   // do stuff
    wait( );
     System.out.println("Done");
    }
}


public void consume( ) {
    synchronized(this) {
    // stuff
    notify();
    Thread.sleep(5000);
    }
}
}

推荐答案

您可以编写一个简单的程序对此进行测试

You can write a simple program to test this

public static void main(String[] args) throws Exception {
    final Process e = new Process();
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(10);
                e.consume();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    t.start();
    e.produce();
}

该程序等待5000毫秒并打印

This program waits 5000 milliseconds and prints

Done

关于您的问题

notify()状态的Javadoc

The javadoc of notify() states

唤醒的线程将无法继续执行,直到当前 线程放弃对此对象的锁定

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object

因此,不,线程在调用notify()时不会放弃对锁的控制.当代码退出synchronized块时,就会发生这种情况.

So, no, the thread does not relinquish control of the lock when calling notify(). That will happen when code exits the synchronized block.

您可以估计,但不能确定.执行是由线程调度程序决定的.

You can estimate, but you can't be certain. Execution happens at the discretion of the Thread scheduler.

该呼叫不知道.线程调度程序知道并选择.您无法控制它.

The call doesn't know. The Thread scheduler knows and chooses. You cannot control this.

用于wait()notify()状态的Javadoc

The javadoc for wait() and notify() state

因此,只有在对象上已同步的synchronized块中时,才可以在对象上调用它们.

So you can only call them on objects when you are inside a synchronized block that is synchronized on that object.

在您的代码示例中,调用

In your code example, the call

wait();

暗中做

this.wait();

这篇关于同步并通知执行顺序和作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:27
查看更多