How to Create thread in java?

Total Post:29

Points:203
Java  OOPS 
 1919  View(s)
Ratings:
Rate this:
Thread creating in java
  1. Post:194

    Points:1396
    Re: How to Create thread in java?

    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);
          }
       }
    }


      Modified On Apr-05-2018 11:28:06 PM

Answer