简体   繁体   English

Integer.MAX_VALUE是否有任何并行问题

[英]Are there any parallel issues with Integer.MAX_VALUE

I have the following code: 我有以下代码:

if (maxLength>=0 && (++totalLength > maxLength))
    throw new IllegalStateException("Form too large");

in a loop where bytes are read from byte array input stream. 从字节数组输入流中读取字节的循环中。 The maxLength is set to Integer.MAX_VALUE so I think that the condition could never be true (and I'm not talking about the size of the byte array in input stream which I'm absolutely sure is not long enough). maxLength设置为Integer.MAX_VALUE因此我认为条件永远不可能成立(并且我并不是在谈论输入流中字节数组的大小,我绝对确定这还不够长)。 But I get the IllegalStateException thrown from that line!!! 但是我从该行抛出了IllegalStateException !!! Now the real bummer is that when I put a breakpoint on that throw line, everything is ok. 现在真正的遗憾是,当我在罚球线上放置一个断点时,一切正常。 How the hell is this possible? 这怎么可能?

EDIT : 编辑

  • both variables are of type int 这两个变量的类型均为int
  • totalLength is a local variable, maxLength is a parameter totalLength是局部变量,maxLength是参数
  • the debugger don't stop there AND the exception is not thrown at all, when there is a breakpoint on that throw line. 调试器不会在那里停下来,并且在该抛出行上有断点时根本不会抛出异常。
  • I actualy don't know why I'm suspecting parallelism, it's just because it's web application 我实际上不知道为什么我怀疑并行性,这仅仅是因为它是Web应用程序
  • I admit that using MAX_VALUE is very risky (in the next step I will try to decrease this limit), but I would expect some other execption than that in the success branch of if statement. 我承认使用MAX_VALUE的风险很大(在下一步中,我将尝试降低此限制),但是我希望除了if语句的成功分支外,还能执行其他一些操作。 And moreover that byte array used in input stream is really not long enough. 而且,输入流中使用的字节数组实际上还不够长。 This should be plainly impossible situation in JVM:-). 在JVM中这绝对是不可能的情况:-)。
  • The code above is in jetty-util-7.1.5.v20100705.jar in the class UrlEncoded and I'm using it by calling 上面的代码在UrlEncoded类的jetty-util-7.1.5.v20100705.jar中,我通过调用来使用它

     byte[] decodedBytes; byte[] encodedBytes; // v pripade url encoded requestu je potreba pouze odriznout // jmeno falesneho parametru nesouciho kodovany blok encodedBytes = Arrays.copyOfRange(content, "encdata=".length(), content.length); decodedBytes = decodeBytes(request, encodedBytes); // priprav desifrovany text jako vstupni proud decodedInputStream = new ByteArrayInputStream(decodedBytes); // pokud je request url encoded je potreba jej manualne parsovat // pro potreby funkci vracejicich parametry UrlEncoded.decodeTo(decodedInputStream, parameters, request.getCharacterEncoding(), Integer.MAX_VALUE); 

Are there any parallel issues with Integer.MAX_VALUE Integer.MAX_VALUE是否有任何并行问题

No, this is most likely not due to any race-conditions (unless you have other threads modifying maxLength or so). 不,这很可能不是由于任何竞争条件引起的(除非您有其他线程在修改maxLength左右)。

According to the JLS on integer is larger than Integer.MAX_VALUE so this is either due to a bug in your VM or based on the false assumption that maxLength is indeed Integer.MAX_VALUE . 根据JLS,整数大于Integer.MAX_VALUE因此这是由于您的VM中存在错误,或者是基于对maxLength实际上是Integer.MAX_VALUE的错误假设。

The maxLength is set to Integer.MAX_VALUE so I think that the condition could never be true maxLength设置为Integer.MAX_VALUE,所以我认为条件永远不会为真

Make sure totalLength is not a long . 确保totalLengthlong

(The snippet below always throws the exception.) (以下代码段始终会引发异常。)

int maxLength = Integer.MAX_VALUE;

long totalLength = 2147483647;

if (maxLength>=0 && (++totalLength > maxLength))
    throw new IllegalStateException("Form too large");

If it's not reproducible with the debugger, just give a more informative error message to the exception, for instance: "Form too large: " + totalLength + " is larger than " + maxLength 如果调试器无法再现该错误,则只需为该异常提供更多信息,例如: "Form too large: " + totalLength + " is larger than " + maxLength

Are you sure you want to increment totalLength before the comparison, rather than after? 您确定要在比较之前而不是之后增加totalLength吗? If your totalLength going into that statement is equal to maxLength, the exception will be thrown... is that a possible scenario? 如果进入该语句的totalLength等于maxLength,则将引发异常……这是否可能?

Try totalLength++ instead. 请尝试使用totalLength ++。

Just a thought. 只是一个想法。 Hope this helps. 希望这可以帮助。

Now the real bummer is that when I put a breakpoint on that throw line, everything is ok 现在真正的遗憾是,当我在该罚球线上放置一个断点时,一切正常

Do you mean the debugger doesn't stop there? 您的意思是调试器不会在那里停止吗?

From your question title it seems you suspect a racing condition (multithreading) issue. 从问题标题看来,您似乎怀疑赛车状况(多线程)问题。 How are the variables defined? 变量如何定义? Could you post some more code? 您可以再发布一些代码吗?

Could maxLength be modified elsewhere? 可以在其他地方修改maxLength吗? Or is it final? 还是最终的?

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

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