Creating threads by extending the thread class
- Eclipse: Oxygen
- Java: 1.8
This tutorial offers a basic introduction to multithreading. This tutorial shows the basic execution of the program with multiple threads. The threads that have several unique properties, such as parallel execution, are shown here.
In the following program, we Extend “Thread class”. Then override the public void run() method which will be executed when the thread is started.
In for loop, we write the logic (looping from 1 to 9 to display numbers from 1 to 9). To start the thread, we create an object of the above class.
Call the start()method on the object created. Â Now thread will start its execution in parallel.
We can call the run() method instead of the start () method, but it would behave like a normal method. This method should be called by JVM. It specified that if we call run() method of these threads directly then the execution of all of them is being handled by the same current thread and no multithreading will take place.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package org.studyeasy; class MyCounter extends Thread{ private int threadNo; public MyCounter(int threadNo) { this.threadNo = threadNo; } @Override public void run() { countMe(); } public void countMe(){ for(int i=1; i<=9;i++){ try { sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("The value if i is: "+i+" and the thread number is: "+threadNo); } } } public class App { public static void main(String[] args) throws InterruptedException { MyCounter counter1 = new MyCounter(1); MyCounter counter2 = new MyCounter(2); long startTime = System.currentTimeMillis(); counter1.run(); //Run should be called by JVM System.out.println("**************************"); counter2.run(); //Run should be called by JVM long endTime = System.currentTimeMillis(); System.out.println("Total time required to process: "+(endTime-startTime)); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
The value if i is: 1 and the thread number is: 1 The value if i is: 2 and the thread number is: 1 The value if i is: 3 and the thread number is: 1 The value if i is: 4 and the thread number is: 1 The value if i is: 5 and the thread number is: 1 The value if i is: 6 and the thread number is: 1 The value if i is: 7 and the thread number is: 1 The value if i is: 8 and the thread number is: 1 The value if i is: 9 and the thread number is: 1 ************************** The value if i is: 1 and the thread number is: 2 The value if i is: 2 and the thread number is: 2 The value if i is: 3 and the thread number is: 2 The value if i is: 4 and the thread number is: 2 The value if i is: 5 and the thread number is: 2 The value if i is: 6 and the thread number is: 2 The value if i is: 7 and the thread number is: 2 The value if i is: 8 and the thread number is: 2 The value if i is: 9 and the thread number is: 2 Total time required to process: 9000 |
In the following program, multithreading takes place and the output reflects simultaneous execution of threads.
When the execution method is called through the start () method, a new independent thread is assigned to execute the execution method, so if more than one thread calls the start method () it means that its execution method is run through separate threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package org.studyeasy; class MyCounter extends Thread{ private int threadNo; public MyCounter(int threadNo) { this.threadNo = threadNo; } @Override public void run() { countMe(); } public void countMe(){ for(int i=1; i<=9;i++){ try { sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("The value if i is: "+i+" and the thread number is: "+threadNo); } } } public class App { public static void main(String[] args) throws InterruptedException { MyCounter counter1 = new MyCounter(1); MyCounter counter2 = new MyCounter(2); long startTime = System.currentTimeMillis(); counter1.start(); //Run should be called by JVM System.out.println("**************************"); counter2.start(); //Run should be called by JVM Thread.sleep(4505); long endTime = System.currentTimeMillis(); System.out.println("Total time required to process: "+(endTime-startTime)); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
The value if i is: 1 and the thread number is: 1 The value if i is: 1 and the thread number is: 2 The value if i is: 2 and the thread number is: 1 The value if i is: 2 and the thread number is: 2 The value if i is: 3 and the thread number is: 1 The value if i is: 3 and the thread number is: 2 The value if i is: 4 and the thread number is: 1 The value if i is: 4 and the thread number is: 2 The value if i is: 5 and the thread number is: 1 The value if i is: 5 and the thread number is: 2 The value if i is: 6 and the thread number is: 1 The value if i is: 6 and the thread number is: 2 The value if i is: 7 and the thread number is: 1 The value if i is: 7 and the thread number is: 2 The value if i is: 8 and the thread number is: 1 The value if i is: 8 and the thread number is: 2 The value if i is: 9 and the thread number is: 1 Total time required to process: 4506 The value if i is: 9 and the thread number is: 2 |
Contributed by: Poonam Tomar