简体   繁体   English

在多线程中使用等待和通知方法

[英]Using wait and notify methods in MultiThreading

Now, I started to study Threading and decided to write this class to demonstrate the idea of Multi-Threading but the output wasn't what i want. 现在,我开始学习线程技术,并决定编写此类来演示多线程的思想,但是输出不是我想要的。

i wrote the class as follows : - 我写的课如下:-

import java.util.LinkedList;
class QueueTest extends LinkedList{
    int capacity;
    LinkedList list;
    public QueueTest(int capacity){
        this.capacity = capacity;
        list = new LinkedList();
    }
    public synchronized void addElements(int i)throws InterruptedException{
        if(list.size() == capacity ){
                wait();
        }
        add(i);
        System.out.println("I added : "+i);
        notify();
    }
    public synchronized int getElements() throws InterruptedException{
        if(!isEmpty()){
                wait();
        }
        int i = (Integer) list.remove();
        System.out.println("I got : "+i);
        notify();
        return i;
    }
}
class Add implements Runnable{
    QueueTest t ;
    public Add(QueueTest t){
        this.t = t;
        new Thread(this,"Add").start();
    }
    @Override
    public void run(){
        int i = 0;
        while(t.size() <= t.capacity){
            try{
                t.addElements(i++);} catch(InterruptedException e){}
        }
    }
}
class Remove implements Runnable{
    QueueTest t ;
    public Remove(QueueTest t){
        this.t = t;
        new Thread(this,"Remove").start();
    }
    @Override
    public void run(){
        while(!t.isEmpty()){
            try{
            t.getElements();} catch(InterruptedException e){}
        }
    }
}
public class FullQueue{
    public static void main(String[] args){
        QueueTest t = new QueueTest(5);
        new Add(t);
        new Remove(t);
    }
}

I expected the output to be like that 我期望输出是这样的

i added : 1
i got : 1
i added : 2
i got : 2 

... and so on, but i got that output ...等等,但我得到了输出

I added : 0
I added : 1
I added : 2
I added : 3
I added : 4
I added : 5

Because the two threads are running at once there is no guarantee or control at all as to which runs when or in what order. 由于这两个线程是同时运行的,因此根本无法保证或控制哪个线程何时或以什么顺序运行。

The get could run completely before the set, the set could run completely first, or they could run all mixed up. get可以在set之前完全运行,set可以首先完全运行,也可以混合运行。

An even interleaving like you display above is very unlikely to happen. 像上面显示的那样均匀的交织是极不可能发生的。

In that case the list has been filled fully by one thread, however the empty thread ran first and exited when it discovered there was nothing in the list. 在那种情况下,列表已被一个线程完全填充,但是空线程首先运行并在发现列表中没有内容时退出。

Add diagnostics to show when each thread starts and ends and you should see that. 添加诊断程序以显示每个线程的开始和结束时间,您应该会看到。 The results will vary with every run though. 结果会随每次运行而变化。

At the moment your notify does nothing as you have nothing waiting. 目前,您的notify无所作为,因为您没有任何等待。 To make the threads alternate then after each write or read you should notify and then wait. 要使线程交替,则在每次写或读之后,您应该通知然后等待。 You would need to synchronize the whole block though and essentially you end up with a very complicated way to get the same result as one thread running. 但是,您将需要同步整个块,并且最终您将获得一种非常复杂的方法来获得与一个线程运行相同的结果。

this is loaded with race conditions. 这充满了比赛条件。 Note that the Remove thread will exit immediately if there are no elements. 请注意,如果没有元素,则删除线程将立即退出。

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

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