简体   繁体   English

具有等待和通知功能的Java多线程

[英]Java multithreading with wait and notify

I have this piece of code 我有这段代码

public MultiThreadedSum(ArrayBuffer ArrayBufferInst)
{
    this.ArrayBufferInst = ArrayBufferInst;
    Sum = 0;
    Flag = false;
    StopFlag = false;
}

public synchronized void Sum2Elements()
{

    while(Flag)
    {
        try {wait();}
        catch (InterruptedException e){}
    }

    Flag = true;

    if (StopFlag)
    {
        notifyAll();
        return;
    }

    System.out.println("Removing and adding 2 elements.");

    Sum = ArrayBufferInst.Sum2Elements();

    notifyAll();
}

public synchronized void InsertElement()
{

    while(!Flag)
    {
        try {wait();}
        catch (InterruptedException e){}
    }

    Flag = false;

    if (StopFlag)
    {
        notifyAll();
        return;
    }

    System.out.println("Inserting the sum.");

    ArrayBufferInst.InsertElement(Sum);

    if (ArrayBufferInst.RetunrSize() == 1)
    {
        StopFlag = true;
    }

    System.out.println(ArrayBufferInst);

    notifyAll();
}

As you can see, I set the Flag to be false first so one of the threads can enter the Sum2Elements method and change it to true and by that, making everyone wait. 如您所见,我首先将Flag设置为false,以便其中一个线程可以输入Sum2Elements方法并将其更改为true,这样,每个人都可以等待。

I know that in synchronized code, only one thread can do its thing, well here I have two synchronized methods, does it mean that 2 threads are trying to conduct this methods after each notifyall? 我知道在同步代码中,只有一个线程可以执行其操作,在这里我有两个同步方法,这是否意味着2个线程在每个notifyall之后都尝试执行此方法?

And if so, is it not possible for one thread to enter Sum2Elements, change the flag to true before the other thread enters InsertElement, and by that skipping the while loop? 如果是这样,那么一个线程是否不可能输入Sum2Elements,在另一线程进入InsertElement之前将标志更改为true,从而跳过while循环?

Thanks 谢谢

Only one thread can hold the lock of the object. 只有一个线程可以持有对象的锁。 And then it's only that thread that can enter the synchronized methods on that object. 然后只有那个线程可以在该对象上输入同步方法。

The thread can however release the lock without returning from the method, by calling Object.wait(). 但是,线程可以通过调用Object.wait()来释放锁而无需从方法中返回。

So your code looks good! 因此您的代码看起来不错!

does it mean that 2 threads are trying to conduct this methods after each notifyall? 
Ans : It is very much possible for two threads to be in two of your synchronized methods since you are calling wait().

is it not possible for one thread to enter Sum2Elements, change the flag to true before the other thread enters InsertElement, and by that skipping the while loop?
Ans : Yes this is possible again for the same reason specified above.

Only one thread can execute one of two method at a time because both are synchronized though order is undefined

就像我说的那样,一个方法只能一次由一个线程执行,除非执行线程通过调用wait方法释放lock ,而另一个线程获得该lock并执行其他synchronized方法则使这两个语句成为可能

Locks are obtained on objects of a class & not on any particular synchronized method. 锁是在类的对象上获得的,而不是在任何特定的同步方法上获得的。 Both the methods are instance methods. 这两种方法都是实例方法。 So if one of the threads have entered any synchronized method for an object, A say, then any other thread cant enter any synchronized method for that object until the running thread doesnt call notifyAll() method. 因此,如果一个线程输入了对象的任何同步方法(A说),那么任何其他线程都无法输入该对象的任何同步方法,直到运行线程未调用notifyAll()方法为止。 At that stage all the waiting threads compete to become active but it depends on the thread scheduler to choose a thread which is to become active. 在那个阶段,所有等待线程竞争成为活动线程,但是取决于线程调度器选择要成为活动线程的线程。

If you want that two different threads should access these synchronized methods simultaneously then the 2 threads should operate on 2 different objects of the class. 如果希望两个不同的线程同时访问这些同步方法,则这两个线程应在该类的2个不同对象上操作。

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

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