HOW TO CREATE THREAD IN JAVA?

Pawan Shukla

Total Post:29

Points:203
Posted by  Pawan Shukla
Java  OOPS 
 1252  View(s)
Ratings:
Rate this:
Thread creating in java
  1. Kamlakar Singh

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

          }

       }

    }


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!