title: incorrect output for multithreading in java
body: <p>why is it that sometimes I get the output:</p>
  
  <p>Patient-4 visits doctor office.</p>
  
  <p>Patient-2 visits doctor office.</p>
  
  <p>Patient-1 visits doctor office.</p>
  
  <p>When I am expecting the output in increasing order, i.e patient 1, then patient 2 or 3, then patient 4 or 5. i.e I can't understand how it can see print 4 before it even got to 0 since the initial for loop in the main class is what sets the patient number, and I instantly start the thread.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public static void main(String[] args)
  {
      NUM_P = 5;//TEMPORARILY HARD CODE, WILL TAKE ARG FROM COMMANDLINE IN THE FUTURE
      NUM_A = 2;
      Patient[] myPatients = new Patient[NUM_P];
      for (int i = 0; i &lt; NUM_P; i++)
      {
           Patient patient = new Patient();
           patient.setPatientNumber(i);
           myPatients[i] = patient;
           myPatients[i].start();
      }
  }
  </code></pre>
  
  <p>In the patient class,</p>
  
  <pre><code>public void run() 
  {
      attendParty();
      visitDoctor();
  }
  
  public void visitDoctor()
  {
      System.out.println("Patient-"+(this.getPatientNumber())+" visits doctor office.");
  }
  
  public void attendParty() throws InterruptedException
  {
      Random randomGen = new Random();
      int val = randomGen.nextInt(101);
      if (val &lt; 20)
      {
          interrupt();
          //NO NEED TO CREATE A PATIENT THREAD, THE PATIENT IS NOT SICK HEHE
      }
      else
      {
          this.setSickness(true);//He is sick
      }
  }
  </code></pre>