title: LWJGL glGenVertexArrays() is blocking the execution
body: <p>I recently asked a question on the forum on why the ExecutorService was blocking my program after getting the futures:
  <a href="https://stackoverflow.com/questions/63860543/java-executorservice-and-future-are-blocking-the-main-thread">ExecutorService and Future are blocking the main thread</a></p>
  <p>The thing is that after a lot of debugging, the guilty line wasn't the future.get() but <code>int vaoID = GL30.glGenVertexArrays();</code></p>
  <p>I'll sum up what I'm trying to do, and then post the code. Basically, I'm generating terrains as the player moves. To avoid freezing the game whenever the generation happens, I implemented multithreading with callables and ExecutorService and Futures. So when I have to generate terrains I'm submitting new tasks to the ExecutorService (the task is to calculate all the vertices/normals/colors of the terrain), and then for each future, if it is done I'm getting the result and generate the model of the terrain. But at each frame, the first future that finishes is causing a freeze. Actually, it's not the future itself that is causing it but the creation of the model, and more precisely the <code>int vaoID = GL30.glGenVertexArrays();</code> method.</p>
  <p>To sum up, I think that glGenVertexArrays(); is blocking the thread, waiting for maybe the other threads to finish, and I don't have a single clue of what is going on. I am using LWJGL 2 and OpenGL 3.0+.</p>
  <p><strong>Here is the code that handles the multithreading:</strong></p>
  <pre><code>
  //method called each frame
  private void checkForFutures(List&lt;Future&lt;SomeClass&gt;&gt; terrainsInCreation) {
          try
          {
              List&lt;Future&lt;SomeClass&gt;&gt; futuresToRemove = new ArrayList&lt;Future&lt;SomeClass&gt;&gt;();
              for(Future&lt;SomeClass&gt; future:terrainsInCreation) {
                  if(future.isDone()) {
                      float time = DisplayManager.getCurrentTime();
                      try {
                          SomeClass t = future.get();
                          Key key = new Key(t.terrain.getChunkX(),t.terrain.getChunkZ());
                          t.terrain.generateTerrain(loader, t.vertices, t.indices,t.colors, t.normals); // Guilty line !
                          futuresToRemove.add(future);
                      } catch (ExecutionException e) {
                          e.printStackTrace();
                          Thread.sleep(1000000);
                      }
                  }
              }
              terrainsInCreation.removeAll(futuresToRemove);
          }
          catch (InterruptedException ie)
          {
              System.err.println(&quot;BIG PROBLEM ON TERRAIN MANAGER&quot;);          
          }
      }
  
  //method called when I have terrains to generate
      private void generate(List&lt;Callable&lt;SomeClass&gt;&gt; terrainsToCreate) {
  
          for(int i = 0; i &lt; terrainsToCreate.size();i++) {
              terrainsInCreation.add(executor.submit(terrainsToCreate.get(i)));
          } 
      }
  
  </code></pre>
  <p><strong>Here is the generateTerrain() method:</strong></p>
  <pre><code>        int vaoID = createVAO();
          bindIndicesBuffer(indices);
          storeDataInAttributeList(0,3, positions);
          storeDataInAttributeList(1,3, colors);
          storeDataInAttributeList(2, 3, normals);
  
          unbindVAO();
          
          return new RawModel(vaoID, indices.length);
  
  
  </code></pre>
  <p>(I simplified the code)</p>
  <p><strong>And here is the createVao() method:</strong></p>
  <pre><code>
  private int createVAO() {
          long start = DisplayManager.getCurrentTime();
          int vaoID = GL30.glGenVertexArrays(); // The first of each frame takes ~40-200ms
          long glGen = DisplayManager.getCurrentTime();
  
          vaos.add(vaoID);
          long add = DisplayManager.getCurrentTime();
  
          GL30.glBindVertexArray(vaoID);
          long bind = DisplayManager.getCurrentTime();
          
          System.out.println(String.format(&quot;Vao: gen: %d + add: %d + bind: %d = %d&quot;, 
                  (glGen-start),
                  (add-glGen),
                  (bind-add),
                  (DisplayManager.getCurrentTime()-start)));
          return vaoID;
      }
  
  </code></pre>
  <p><strong>And finally here is the print output in a regular generation:</strong></p>
  <pre><code>
  NEW CHECK
  NEW CHECK
  Vao: gen: 7 + add: 0 + bind: 0 = 7
  Vao: gen: 1 + add: 0 + bind: 0 = 1
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  NEW CHECK
  Vao: gen: 209 + add: 0 + bind: 0 = 209 (why????)
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 1 + add: 0 + bind: 0 = 1
  Vao: gen: 1 + add: 0 + bind: 0 = 1
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 1 = 1
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  NEW CHECK
  Vao: gen: 4 + add: 0 + bind: 0 = 4
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  Vao: gen: 0 + add: 0 + bind: 0 = 0
  NEW CHECK
  NEW CHECK
  
  </code></pre>
  <p>Each frame I'm printing &quot;NEW CHECK&quot; and as you can see it is not exactly the first of each frame sometimes it is not but when a generation event occurs the first is always super slow.</p>
  <p>My question is what is going on here and what can I do / test / update ?</p>
  <p>Thanks for your help.</p>
  <p><strong>EDIT</strong></p>
  <p>I must precise that GL30.glGenVertexArrays() is called in the main thread.
  Here is the context code executed before anything else at the start of the program:</p>
  <pre><code>
  ContextAttribs attribs = new ContextAttribs(3,2)
          .withForwardCompatible(true)
          .withProfileCore(true);
          Mouse.setGrabbed(true);
  
          try {
              DisplayMode displayMode = new DisplayMode(WIDTH, HEIGHT);
              Display.setDisplayMode(displayMode);
              Display.create(new PixelFormat(),attribs);
              Display.setTitle(&quot;CubeLand&quot;);
          } catch (LWJGLException e) {
              e.printStackTrace();
          }
  
  </code></pre>
  <p><strong>EDIT 2</strong></p>
  <p>I removed the multithreading, by doing everything in the main thread, and about the same freeze happens when generating, which is normal because the calculations are huge, <strong>BUT</strong> the GL30.glGenVertexArrays() is not taking any time to compute.
  This proves that this method is waiting for all the other threads to end, but why and how to avoid that??</p>
  <pre><code>
  NEW CHECK
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  Vao gen: 0
  NEW CHECK
  
  </code></pre>
  <p><strong>EDIT 3</strong>
  Some news, when I unplug my laptop, the graphic card is turned off and the motherboard graphic chipset replace it. Suddenly, the issue is gone. Off course I still have freezes because now that my performance are cut off, the processor struggles to generate the terrain even with multithreading, but the vao gen time is back to 0. This is confusing.</p>