title: Java Thread Creates extra Threads for unclear reasons
body: <p>Java Thread Creates extra Threads I'm not sure why this happens! I'm still a noob at java if its very obvious I'm sorry but please help me out </p>
  
  <p>Well I created 2 threads to do something but there are extra threads created </p>
  
  <p>here's the code     </p>
  
  <pre><code>import java.util.*;    
  import java.util.Arrays;
  
  class consuming_producing extends Thread {
     static int arrX[] = new int[300];
      int y;
      static int var=0;
  
      int Request_type;
  
      consuming_producing(int x, int Request) {
          y = x;
          Request_type = Request;
  
      }
      int thread_number;
      consuming_producing(int x, int Request ,int thread) {
          y = x;
          thread_number=thread;
          Request_type = Request;
  
      }
  
  
  
      public synchronized void run() {
  
          switch (Request_type) {
              case 1: {                                                  
              // start producing the data and store it to the array
                  for (int i = y; i &lt; 300; i = i + 3) {
                      arrX[i] = (int) (Math.random() * 10 + 1);
                      System.out.println("produced " + arrX[i]);
                  }
              }
  
              // *******************
              case 2: {                  // consuming the data
                  for (int ww = y; ww &lt; y+150;ww++) {
                      int sum = Arrays.stream(arrX).sum();
                      System.out.println("Sum  =" +var+" by thread " + thread_number);
                      //Arrays.fill(arrX,0);
                      var+=1;
                  }
              }    
          }
      }
  }
  </code></pre>
  
  <p></p>
  
  <pre><code>class consumer_producer {
      public static void main(String[] args){
          consuming_producing arr_Producer_obj[] = new consuming_producing[3];   
            // create a [enter image description here][1]thread for producing
          for (int i=0;i&lt;3;i++){
              int x  =0;
              arr_Producer_obj[i]=new consuming_producing(x,1);
              arr_Producer_obj[i].start();
              x+=1;
          }
  
          // ***************
          consuming_producing arr_Consumer_obj[]=new consuming_producing[2];
          int thread=1;  // this is the name of thread im creating for ny refrence
          int x=0;
          for (int i=0;i&lt;=1;i++){
              arr_Consumer_obj[i]=new consuming_producing(x,2,  thread);
              arr_Consumer_obj[i].start();
              x+=150;
              thread+=1;**
          }
      }
  }
  </code></pre>
  
  <p>here's the image of output please check this </p>
  
  <p><a href="https://i.stack.imgur.com/xRlLp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xRlLp.png" alt="https://i.stack.imgur.com/xRlLp.png"></a>  </p>