Home > DeveloperSection > Forums > How to Create thread in java?
Pawan Shukla
Pawan Shukla

Total Post:29

Points:203
Posted on    October-10-2014 12:29 AM

 Java Java  OOPS 
Ratings:


 1 Reply(s)
 641  View(s)
Rate this:
Thread creating in java


Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-10-2014 12:40 AM

ThreadTest.Java

    public class ThreadTest

    {

        public static void main(String[] args)

        {

            MyThread t1 = new MyThread(0, 3, 300);

            MyThread t2 = new MyThread(1, 3, 300);

            MyThread t3 = new MyThread(2, 3, 300);

 

            t1.start();

            t2.start();

            t3.start();

        }

    }

MyThread.java

public class MyThread extends Thread

{

   private int startIdx, nThreads, maxIdx;

 

   public MyThread(int s, int n, int m)

   {

      this.startIdx = s;

      this.nThreads = n;

      this.maxIdx = m;

   }

 

   @Override

   public void run()

   {

      for(int i = this.startIdx; i < this.maxIdx; i += this.nThreads)

      {

         System.out.println("[ID " + this.getId() + "] " + i);

      }

   }

}



Don't want to miss updates? Please click the below button!

Follow MindStick