简体   繁体   English

Java & 执行顺序

[英]Java & Order of Execution

I released my first Android app and I'm attempting to troubleshoot some bugs that have resulted in crashes (as identified via the Google Play Console).我发布了我的第一个 Android 应用程序,我正在尝试解决一些导致崩溃的错误(通过 Google Play 控制台识别)。

The code simplified the code as much as possible - the following method is called:代码尽可能地简化了代码——调用了以下方法:

private void gameMethod(int switchCase, ArrayList gameArray) {


boolean magicBoolean = false;
ArrayList magicList = new ArrayList<>();

switch(switchCase) {
    case 1:
          for(int i = 0; i < 4; i++) {
   
              if(gameArray.get(i) == 2) {
         
                  magicBoolean = true;
              } 

              if(magicBoolean) {
                  magicList.add(gameArray.get(i));
              } 
           }
           break;

     case 2:
          // basic repeat of case 1
          break;
     case 3:
          // basic repeat of case 1
          break;
                                                        
}


newMagicMethod(ArrayList magicList);

As you can see, the 2nd method (newMagicMethod) uses the magicList which is populated via the switch.如您所见,第二种方法 (newMagicMethod) 使用通过开关填充的 magicList。 The magicList size will never be 0, the first method is only called when the array has more than 1 item in it. magicList 的大小永远不会为 0,只有当数组中的项目超过 1 时才会调用第一个方法。

In my crash report it is telling me the following:在我的崩溃报告中,它告诉我以下内容:

Exception java.lang.IndexOutOfBoundsException: Index: 0, Size: 0异常 java.lang.IndexOutOfBoundsException:索引:0,大小:0

So I conclude that somehow the 2nd method is attempting to use the magicList (eg find and use the 1st item on the list) and then crashes when the list is empty.所以我得出结论,第二种方法以某种方式尝试使用 magicList(例如,查找并使用列表中的第一项),然后在列表为空时崩溃。

My question would be is there any way that the Java code would attempt to execute the call to the newMagicMethod() FIRST before the switch statement is executed.我的问题是,在执行 switch 语句之前,Java 代码是否会尝试首先执行对 newMagicMethod() 的调用。 Obviously is this is the case this would explain how the arrayList could be empty and would cause a crash.显然是这种情况,这将解释 arrayList 如何为空并导致崩溃。 Otherwise I have look for other reasons as to why the arrayList could be empty.否则我会寻找其他原因来解释为什么 arrayList 可能是空的。

If I could confirm if code like this can execute out of order, I would figure out a way to resolve/define the order.如果我可以确认这样的代码是否可以乱序执行,我会想出一种方法来解决/定义顺序。

The short answer is NO — the switch statement itself is not asynchronous.简短的回答是否定的switch语句本身不是异步的。 It will be fully executed, and only after that will your newMagicMethod() be called.它将被完全执行,只有在那之后你的newMagicMethod()才会被调用。

However, it seems that the code you provided is pseudo-code, since it wouldn't compile.但是,您提供的代码似乎是伪代码,因为它无法编译。 For example, you can't declare an ArrayList inline within a method call, as you currently do here, where you presumably meant to use the magicList variable declared further up within gameMethod() :例如,您不能在方法调用中声明 ArrayList 内联,就像您目前在这里所做的那样,您可能打算使用在gameMethod()中进一步声明的magicList变量:

newMagicMethod(ArrayList magicList)

Given that you've only provided pseudo-code, here are a couple of things to consider:鉴于您只提供了伪代码,这里有几件事需要考虑:

  • Do you have any asynchronous calls in your case blocks?您的case块中是否有任何异步调用? If so, you may have introduced a race condition where there's no guarantee that your collection will be populated before the call to newMagicMethod() .如果是这样,您可能引入了竞争条件,无法保证在调用newMagicMethod()之前填充您的集合。
  • Are you sure the logic in your case blocks guarantees that at least one object will end up in your collection?您确定case块中的逻辑保证至少有一个 object 最终会出现在您的收藏中吗? The most likely explanation here is that you failed to account for a scenario whereby your calls to add() will be bypassed.这里最可能的解释是您没有考虑到您对add()的调用将被绕过的情况。 You can use breakpoints or logging to catch such unexpected scenarios.您可以使用断点或日志记录来捕获此类意外情况。
  • Do you have a default block, where you log the fact that your collection will be empty, or maybe throw an exception?您是否有一个default块,您可以在其中记录您的集合将为空的事实,或者可能会抛出异常? It's all-too-common for developers to assume they've covered all cases in a switch statement, when in fact there are one or more cases for which they forgot to account.开发人员常常认为他们已经在switch语句中涵盖了所有情况,而实际上他们忘记了考虑一个或多个情况。

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

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