简体   繁体   中英

Java using wait/notify methods on threads that do same thing

I was trying to understand the monitor on Java and the question that came to me is how to make the threads that run the same synchronized method to wait? I was trying to make a simple program that would make 3 threads to use the same method to add to N element 1 for total of 10 000 times and I was wondering how to make other threads to wait when one is doing adding method and notifyAll after it is done if I would start all of them at the same time.

Here is my program that I wrote without wait/notify functions :

class Swapper implements Runnable{
    int number;
    Swapper(int number){
        this.number=number;
    }
    @Override
    public void run() {
        while (mainClass.counter>0){
            mainClass.incArrayElement(number);
        }
    }
}
public class mainClass {
    public static volatile int counter = 10000;
    public static volatile int[] testArray = new int[]{0,0,0};
    public static synchronized void incArrayElement(int index){
        if (counter>0) {
            testArray[index - 1]++;
            counter--;
        }
        else {
            return;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Swapper(1));
        Thread thread2 = new Thread(new Swapper(2));
        Thread thread3 = new Thread(new Swapper(3));
        thread1.start();
        thread2.start();
        thread3.start();
        thread1.join();
        thread2.join();
        thread3.join();
        int checkSum = 0;
        for (int i = 0; i < testArray.length; i++) {
            System.out.println(testArray[i]);
            checkSum+=testArray[i];
        }
        System.out.println(checkSum);
    }
}

When a thread calls the synchronized method 'incArrayElement' of your class it acquires the lock of that object, any new thread cannot call ANY synchronized method of the same object as long as previous thread which had acquired the lock does not release the lock. Hence all other threads will be blocked until the execution is complete.

So why do you need to have the threads to call wait() as they are blocked already and waiting.

Unfortunately your example is not well chosen.

The method declared synchronized is controlled in a way that other threads cannot call it unless it has finished execution. Then one of the threads calls this method again. 'Which thread' cannot really be told because you have no control over it. Using wait and notify functions will not give you control over this neither. So if that is what you are looking for, you cannot achieve what you want. It will remain indeterministic for you.

If simply assuring that the method is called by only one thread at a time, then you already have that behavior, no need for wait or notify .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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