除非打印输出,否则值不会在while循环中更新 [英] Value does not update in while loop unless printed out

查看:74
本文介绍了除非打印输出,否则值不会在while循环中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个监视线程来检查ArrayList的大小,并在该大小大于某个数字后执行一些操作.我现在遇到的问题是,除非我的循环中有打印语句,否则大小值永远不会更新.这是一些代码来显示我到底要干什么.

Ok, so I have a monitoring thread that checks a ArrayList size and does something after that size goes greater than a certain number. The problem I am having right now is the size value is never updated unless I have a print statement in my loop. Here is some code to show what exactly I have going.

while(working) {
    // Get size function just returns the size of my list in my t class
    int size = t.getSize();
    if (size >= 10) {
        //DO STUFF
    }
}

以上代码无效.它永远不会进入if语句.但是,这可以正常工作:

This above code does not work. It never goes into the if statement. However, this works fine:

while(working) {
    // Get size function just returns the size of my list in my t class
    int size = t.getSize();
    System.out.println(size);
    if (size >= 10) {
        //DO STUFF
    }
}

getSize()代码:

getSize() code:

public ArrayList<byte[]> myQueue = new ArrayList<byte[]>();

public int getSize() {
    return myQueue.size();      
}

注意:我正在运行另一个线程,该线程正在更新并添加到我的t类中的列表中.

NOTE: I have another thread running that is updating and adding to my list in my t class.

有帮助吗?当我尝试在控制台中调试时,让它吐出数字确实很烦人.

Any help? this is really annoying to have it spitting out numbers when I am trying to debug in the console.

推荐答案

如果在工作代码和非工作代码之间唯一改变的是println语句,那么您几乎肯定会遇到线程问题. System.out.println()语句添加了一个小的暂停,这可能会偶然导致它的行为,但不能解决问题.您可以执行以下操作:

If the only thing changing between your working and non working code is the println statement, then you almost certainly have a threading issue. The System.out.println() statement adds a small pause which may coincidentally cause it to behave, but is not solving the issue. You could do something like:

try {
    Thread.sleep(10);
} catch (InterruptedException ex) {
}

...代替它并检查相同的行为,如果确实相同,则这几乎可以确认线程问题.但是,正如下面指出的,println()还会做其他一些事情,例如引起内存障碍,因此这不是万无一失的测试.另一个也许更好的检查方法是暂时将ArrayList换成Vector,这是线程安全的-尽管这是一个旧式集合,所以不建议在最终代码中使用.

...in place of this and check for the same behaviour, if indeed it is the same then this pretty much confirms the threading issue. However, as pointed out below println() also does a few other things such as causing a memory barrier, so this isn't a foolproof test. Another, perhaps better check could be to temporarily swap out ArrayList for Vector, which is thread safe - though this is a legacy collection so not recommended for use in the final code.

如果是这种情况,听起来您没有正确同步ArrayList调用-ArrayList不是线程安全集合.每当您读取或写入列表时,都应在同步块内进行操作,如下所示,在列表上进行同步:

If this is the case, it sounds like you're not synchronising on ArrayList calls properly - ArrayList is not a thread safe collection. Whenever you read or write to the list, do it inside a synchronized block like so, synchronizing on the list:

synchronized(list) {
    list.whatever();
}

...这将确保只有一个线程可以一次访问ArrayList,有望解决线程问题.

...which will ensure that only one thread can access the ArrayList at once, hopefully solving the threading issue.

这篇关于除非打印输出,否则值不会在while循环中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆