Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
等待通知传递空值Java_Java_Multithreading_Synchronization - Fatal编程技术网

等待通知传递空值Java

等待通知传递空值Java,java,multithreading,synchronization,Java,Multithreading,Synchronization,我试图在具有多线程(多个线程通知和等待)的程序中实现wait()和notify() 问题 当两个线程等待(),而我是通知(),它们都会唤醒,这使得只有一个数据,并且两个线程都需要执行,结果是一个线程赢了,丢失的线程将不会收到任何NULL 这只是我的观点,可能与您的情况不同,因为它只发生在多线程环境中 原因 我可能会实现,notify()错误,等等,我可能不知道,它只会发生20次运行,一次错误 代码 //Single Threads /* As seen here I am using sin

我试图在具有多线程(多个线程通知和等待)的程序中实现wait()notify()

问题

当两个线程等待(),而我是通知(),它们都会唤醒,这使得只有一个数据,并且两个线程都需要执行,结果是一个线程赢了,丢失的线程将不会收到任何NULL

这只是我的观点,可能与您的情况不同,因为它只发生在多线程环境中

原因

我可能会实现,notify()错误,等等,我可能不知道,它只会发生20次运行,一次错误

代码

//Single Threads

/*
 As seen here I am using single threads, and 
 if there is error, Depot Enter will shows error, 
 if no error means there is a data
*/

 bus = (Bus) ((LinkedList<?>) Shop.ListBusRamp).poll();
 System.out.println("Depot Enter: " + bus.getBusName());

/*
 In here I want to pass the value into another Linked List
 As earlier there is a value that I just poll.
 I can offer here and pass the value, no NULL occurred yet.    
*/

 ((LinkedList<Bus>) ListBusDepot).offer(bus);

 synchronized (ListBusDepot) {
     ListBusDepot.notify();
 }
//单线程
/*
如图所示,我使用单线程
如果有错误,Depot Enter将显示错误,
如果没有错误,则表示存在数据错误
*/
总线=(总线)((链接列表)Shop.ListBusRamp.poll();
System.out.println(“车辆段输入:+bus.getBusName());
/*
在这里,我想把值传递到另一个链表中
如前所述,我只是轮询了一个值。
我可以在这里提供并传递值,还没有出现NULL。
*/
(LinkedList)ListBusDepot.报价(总线);
已同步(ListBusDepot){
ListBusDepot.notify();
}
如图所示,我想将值从ListBusRamp传递到ListBusDepot,如图所示,尚未出现空值,一切正常

//MULTI THREADS (2 THREADS TO BE EXACT)

/*
  Here I want to wait for the data passed by notify
  After I got the data, I want to show it.

  As for why I check of Cleaners < 2?
  I create so that NOT ALL can enter, only when size is 2
*/

synchronized (ListBusDepot) {
    while (ListBusDepot.size() == 0) {
        try {
            ListBusDepot.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
    }

    //VALUE MUST NOT NULL IF REACHES HERE

    bus = (Bus) ((LinkedList<?>) Shop.ListBusDepot).peek();
    if (counter.getBus_Cur_Cleaners() < 2) {
        counter.remBus_Cur_Depot();
        counter.addBus_Cur_Mechanics();

        //Here is the problem, while accessing data, it will show NULL
        System.out.println("Mechanics Earlier: " + bus.getBusName() + " fixed by" + name);

        bus = (Bus) ((LinkedList<?>) Shop.ListBusDepot).poll();
    }
}
//多线程(确切地说是两个线程)
/*
在这里,我想等待notify传递的数据
在我得到数据后,我想展示它。
至于我为什么检查清洁剂<2?
我创建的目的是,只有当大小为2时,并不是所有人都可以进入
*/
已同步(ListBusDepot){
而(ListBusDepot.size()==0){
试一试{
ListBusDepot.wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
//如果到达此处,则值不得为NULL
总线=(总线)((LinkedList)Shop.ListBusDepot.peek();
if(counter.getBus\u Cur\u Cleaners()<2){
remBus_Cur_Depot()柜台;
counter.addBus_Cur_Mechanics();
//问题是,在访问数据时,它将显示NULL
System.out.println(“前面的技术:“+bus.getBusName()+”由“+name”修复);
总线=(总线)((LinkedList)Shop.ListBusDepot.poll();
}
}
如图所示,出现了空值,如何防止它,我只想如果它仍然为空(空),请不要在等待时退出循环

当两个线程等待()时发生,而我是notify(),这两个线程 唤醒

不,只有一个人会醒来
notifyAll
将同时唤醒这两者


您正在
ListBusDepot

while (ListBusDepot.size() == 0) 
bus = (Bus) ((LinkedList<?>) Shop.ListBusDepot).peek();
但是试着从
Shop.ListBusDepot

while (ListBusDepot.size() == 0) 
bus = (Bus) ((LinkedList<?>) Shop.ListBusDepot).peek();
bus=(总线)((LinkedList)Shop.ListBusDepot.peek();
这就是
bus
为空的原因



考虑使用a,它将为您处理
等待
通知
,因此您可以只关注您的业务登录。

您好,感谢您的回复。Shop.ListBusDepot和ListBusDepot是一样的,它们都在Shop类中,是的,我想使用
阻塞队列
,但这是一个学术目的,我需要使用传统方法。@JoesLie那么你可能想发布完整的代码。这个代码看起来不错。是的,它只是随机出现的,基于运气。这太可怕了。那么,有没有办法检查它是否为空,然后才能继续。对于完整的代码,这是相同的步骤,从一个列表轮询通知到另一个列表。@JoesLie据我所知,您的代码可以确保
bus
不是
null