title: Java thread executing remainder operation in a loop blocks all other threads
body: <p>The following code snippet executes two threads, one is a simple timer logging every second, the second is an infinite loop that executes a remainder operation:</p>
  
  <pre><code>public class TestBlockingThread {
      private static final Logger LOGGER = LoggerFactory.getLogger(TestBlockingThread.class);
  
      public static final void main(String[] args) throws InterruptedException {
          Runnable task = () -&gt; {
              int i = 0;
              while (true) {
                  i++;
                  if (i != 0) {
                      boolean b = 1 % i == 0;
                  }
              }
          };
  
          new Thread(new LogTimer()).start();
          Thread.sleep(2000);
          new Thread(task).start();
      }
  
      public static class LogTimer implements Runnable {
          @Override
          public void run() {
              while (true) {
                  long start = System.currentTimeMillis();
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      // do nothing
                  }
                  LOGGER.info("timeElapsed={}", System.currentTimeMillis() - start);
              }
          }
      }
  }
  </code></pre>
  
  <p>This gives the following result:</p>
  
  <pre><code>[Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=1004
  [Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=1003
  [Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=13331
  [Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=1006
  [Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=1003
  [Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=1004
  [Thread-0] INFO  c.m.c.concurrent.TestBlockingThread - timeElapsed=1004
  </code></pre>
  
  <p>I don't understand why the infinite task blocks all other threads for 13.3 seconds. I tried to change thread priorities and other settings, nothing worked.</p>
  
  <p>If you have any suggestions to fix this (including tweaking OS context switching settings) please let me know. </p>