简体   繁体   English

关于线程等待/通知

[英]Regarding threading wait/notify

I am a beginner in threading concepts in java. 我是Java线程概念的初学者。 I have done a small program by implementing wait and notify mechanism and the coding is as follows:- 我通过实现等待和通知机制完成了一个小程序,其编码如下:-

Example.java 范例.java

import comp.samplecheck;

public class Example extends Thread {
public static void main(String[] args) {
    samplecheck sample = new samplecheck("First");
    samplecheck sample1 = new samplecheck("Second");
    samplecheck sample2 = new samplecheck("Third");
    samplecheck sample4 = new samplecheck("Fourth");
    samplecheck sample5 = new samplecheck("Fifth");

    sample.initiate(sample);
    sample1.initiate(sample1);
    sample2.initiate(sample2);

    try {
        Thread.sleep(1000);
        sample4.samplefunc(sample4);
    }
    catch(InterruptedException e) {
        System.out.println("Exception "+e);
    }
}
}

samplecheck.java samplecheck.java

package comp;

import java.util.List;
import java.util.ArrayList;

public class samplecheck extends Thread {
String get_text;
samplecheck objs;
static List arr = new ArrayList();

public samplecheck(String str) {
    this.get_text = str;
}

public void run() {     
    synchronized(arr) {
        System.out.println(objs.getName()+" Started Processing");

        try {
            arr.wait();
        }
        catch(InterruptedException e) {
            System.out.println("Exception "+e);
        }

        System.out.println("Initial Array Elements Size "+arr.size());
        for(int i=0; i<100 ; i++){
            arr.add(i);
        }

        System.out.println("After Array Elements Size "+arr.size());
        System.out.println("An Object Is Notified");            
    }
}

public void initiate(samplecheck obj) {
    objs = obj;
    objs.start();
}

public void samplefunc(samplecheck obj) {
    objs = obj;

    synchronized(arr) {
        System.out.println("Notification Process");
        System.out.println("After Array Elements Size "+arr.size());
        arr.notify();
    }
}   
}

In the samplefunc function in samplecheck.java if i give arr.notify() i am able to get the correct output as 在samplecheck.java中的samplefunc函数中,如果我给arr.notify(),我可以得到正确的输出为

    Thread-0 Started Processing
    Thread-1 Started Processing
    Thread-2 Started Processing
    Notification Process
    After Array Elements Size 0
    Initial Array Elements Size 0
    After Array Elements Size 100
    An Object Is Notified

But if arr.notifyAll() instead of arr.notify() i get the output as 但是如果用arr.notifyAll()而不是arr.notify(),我得到的输出为

    Thread-0 Started Processing
    Thread-2 Started Processing
    Thread-1 Started Processing
    Notification Process
    After Array Elements Size 0
    Initial Array Elements Size 0
    After Array Elements Size 100
    An Object Is Notified
    Initial Array Elements Size 100
    After Array Elements Size 200
    An Object Is Notified
    Initial Array Elements Size 200
    After Array Elements Size 300
    An Object Is Notified

But according to the coding perspective even though if i give notifyAll only one object which gains lock on the array 'arr' should get executed and add elements in the array and the notification message ie) An Object is Notified should be displayed. 但是根据编码的观点,即使我给notifyAll,也只能执行在数组“ arr”上获得锁定的一个对象,并在该数组和通知消息中添加元素,即应该显示一个对象。 But here all the three objects ie) sample, sample1, sample2 are getting notified and adding elements in the array. 但是这里所有三个对象(即sample,sample1,sample2)都得到通知并在数组中添加元素。 I dont know why it is executing in this manner. 我不知道为什么它会以这种方式执行。 If i had called the notifyAll method three times then the output should be as in the above format.I want the output as same as the previous case(the output which i get using arr.notify() method). 如果我已经三遍调用notifyAll方法,那么输出应该与上述格式相同。我希望输出与前一种情况相同(我使用arr.notify()方法获得的输出)。

Can anyone please help me in this issue.... 谁能帮我解决这个问题...。

The second output is correct. 第二个输出正确。 Your understanding of that is going on is wrong. 您对此进行的理解是错误的。

3 objects are waiting on arr , not only one. 3个对象正在等待arr ,而不仅仅是一个。 I think this explains the output when notifyAll is called. 我认为这解释了调用notifyAll时的输出。

You might ask where this waits come from? 您可能会问这种等待来自何处?

In every sample, sample1, sample2 thread you have a synchronized block acquiring lock on arr . 在每个样本sample1,sample2线程中,您都有一个同步块来获取对arr锁定。 So the block should be executed only one at time, what is what you expected when you acquire lock. 因此,该块一次只能执行一个,这就是您获得锁时的期望。 But when you call wait the lock on arr is released. 但是,当您致电wait ,将释放对arr的锁定。 So after sample-1 released lock, then sample-2 enters synchronized block. 因此,在sample-1释放锁定之后,sample-2进入同步块。

From the wait documentation: wait文档中:

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. 此方法使当前线程(称为T)将自己置于该对象的等待集中,然后放弃对该对象的任何和所有同步声明。 Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens: 出于线程调度目的,线程T被禁用,并且在发生以下四种情况之一之前处于休眠状态:

  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened. 其他一些线程为此对象调用notify方法,并且线程T偶然被选择为要唤醒的线程。
  • Some other thread invokes the notifyAll method for this object. 其他一些线程为此对象调用notifyAll方法。
  • Some other thread interrupts thread T. 其他一些线程中断线程T。
  • The specified amount of real time has elapsed, more or less. 指定的实时量或多或少已经过去。 If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified. 但是,如果超时为零,则不考虑实时,线程只是等待直到通知。

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. 然后将线程T从该对象的等待集中删除,并重新启用线程调度。 It then competes in the usual manner with other threads for the right to synchronize on the object; 然后,它以通常的方式与其他线程竞争在对象上进行同步的权利。 once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. 一旦它获得了对象的控制权,它对对象的所有同步声明就会恢复到原样-即,恢复到调用wait方法时的情况。 Thread T then returns from the invocation of the wait method. 然后,线程T从调用wait方法返回。 Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked. 因此,从wait方法返回时,对象和线程T的同步状态与调用wait方法时的状态完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM