title: Java Multi-Threading with global variables
body: <p>Apologies in advance, since this seems to be a pretty common question, but none of the answers I've found so far were able to help me. (Not even sure if I'm searching for the right thing, to be honest)</p>
  <p>I'm writing a program which is supposed to update Firmware for Servers (and their components)
  These servers have Mellanox-Cards built in (ConnectX5 for anyone that might know these) which need to have their firmware up/down-graded. In order to speed up this process, I've decided to put this update process into multiple threads, so I can start them at the same time and only continue with the program until all threads have completed their work.</p>
  <p>Here is my code so far:</p>
  <pre><code>final CyclicBarrier gate = new CyclicBarrier(mellanox.size()+1);
  ArrayList&lt;Thread&gt; threads = new ArrayList&lt;&gt;();
  CountDownLatch latch = new CountDownLatch(mellanox.size());
  for (int index = 0; index &lt; mellanox.size(); index++) {
      String mellanoxCard = mellanox.get(index);
      String ip = ipAddresses.get(index);
      Thread t = new Thread(() -&gt; {
          try {
              if (isWindows) {
                  builder.command(flint, &quot;-d&quot;, mellanoxCard, &quot;-i&quot;, mellanoxFWPath, &quot;-y&quot;, &quot;burn&quot;);
                  setupLoading.setProgressInfo(&quot;Updating Mellanox FW for System &quot; + ip, &quot;calibri&quot;);
                  gate.await();
                  startProcess();
                  latch.countDown();
                  setupLoading.setProgressInfo(streamGobbler.getResult(), &quot;consolas&quot;);
              }
          } catch (InterruptedException | BrokenBarrierException e) {
              setupLoading.setProgressInfo(&quot;An error has occured during Mellanox Firmware update!&quot;, &quot;warning&quot;);
              writeErrorLog(e.getMessage());
          }
      });
      threads.add(t);
  }
  for(Thread t : threads) t.start();
  try {
      gate.await();
      latch.await();
  } catch (InterruptedException | BrokenBarrierException e) {
      writeErrorLog(e.getMessage());
  }
  </code></pre>
  <p>Since it's probably hard to create a minimal reproducible example (at least not without buying a Server with a mellanox card) I'll try and break this down as best as possible.</p>
  <p>The problem I'm having is the following: When testing this with 4 systems, flint (the software used to do the firmware upgrade) responds with:</p>
  <pre><code>-E- Cannot open Device: 192.168.4.150#23108,mt4119pciconf0. No such device MFE_CR_ERROR
  -E- Cannot open Device: 192.168.4.150#23108,mt4119pciconf0. No such device MFE_CR_ERROR
  -E- Cannot open Device: 192.168.4.150#23108,mt4119pciconf0. No such device MFE_CR_ERROR
  </code></pre>
  <p>But not a fourth time. I'm almost 99% certain that for some reason, the threads all take on the same values, and flint tries to do the update 4 times on the same system, instead of once on each system, causing this &quot;no such device&quot; error to appear, since the device is already in use.</p>
  <p>So, in order to test this theory, I started debugging. After all threads were created, I checked the arguments for each of them, and every thread had a different <code>mellanoxCard</code>.
  Since I wasn't able to find the full <code>builder.command</code> I also double checked if the <code>ip</code> was a different one for each thread as well - which it was. It also printed out the messages fine, and each of them had a different <code>ip</code>.
  But after letting the threads proceed, I still got the same error 3 times.</p>
  <p>I am honestly stumped as to what might be the problem here, or google for, so any pointers in any direction are greatly appreciated. I suspect that it might have something to do with the way I create the Threads/handle the variables, hence the Question title, but as far as I know it could be anything.</p>
  <p>Thanks! ♥</p>