Create Multiple Thread in Java

Category: Java   Tags: Java, Java Thread, Java Multithreading

Java allow you to create as many as threads your program needs. Here is a simple Java program to create multiple thread in Java:

JavaMultiThreadExample.java

                          package com.tutorial.javabasic;

                          class MyThread implements Runnable {
                              String threadName;

                              MyThread(String name) {
                                  threadName = name;
                                  Thread thread = new Thread(this, name);
                                  thread.start();
                              }

                              @Override
                              public void run() {
                                  for(int i = 0; i<5; i++) {
                                      try {
                                          System.out.println("[ " + threadName + " ] : " + i);
                                          Thread.sleep(1000);
                                      } catch (InterruptedException ex) {
                                          System.out.println("[ " + threadName + " ] Interrupted!");
                                      }
                                  }
                              }
                          }

                          public class JavaMultiThreadExample {
                              public static void main(String s[]) {
                                  System.out.println("Main thread");
                                  new MyThread("Thread-1");
                                  new MyThread("Thread-2");
                                  new MyThread("Thread-3");
                                  new MyThread("Thread-4");
                              }
                          }
                        

Output:

                            Main thread
                            [ Thread-1 ] : 0
                            [ Thread-3 ] : 0
                            [ Thread-2 ] : 0
                            [ Thread-4 ] : 0
                            [ Thread-1 ] : 1
                            [ Thread-3 ] : 1
                            [ Thread-2 ] : 1
                            [ Thread-4 ] : 1
                            [ Thread-1 ] : 2
                            [ Thread-3 ] : 2
                            [ Thread-2 ] : 2
                            [ Thread-4 ] : 2
                            [ Thread-1 ] : 3
                            [ Thread-3 ] : 3
                            [ Thread-2 ] : 3
                            [ Thread-4 ] : 3
                            [ Thread-1 ] : 4
                            [ Thread-3 ] : 4
                            [ Thread-2 ] : 4
                            [ Thread-4 ] : 4
                          

Here we are creating four thread by calling the below statements four times.

                            Thread thread = new Thread(this, name);
                            thread.start();
                          

Calling Thread.sleep(1000) causes the current thread from which it is called to suspend execution for the specified period of milliseconds (one second in this case). During its suspension period other thread get a chance to execute and therefore the output of this program will not be same always.