Java — Defining a Thread by Extending Thread Class | Code Factory

package com.example.thread;public class ThreadTest {
public static void main(String... args) {
MyThread t = new MyThread(); // Thread instantiation
t.start(); // starting a Thread
// executed by Main Thread (line no. 8 to 10)
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
}
}
// Define a Thread (line no. 15 to 22)
class MyThread extends Thread {
public void run() {
// job of Thread, executed by Child Thread (line no. 18 to 20)
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
}
}
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
start() {
1. Register this thread with thread schedular
2. Perform all other mandatory activities
3. Invoke run()
}
Thread t = new Thread();
t.start();
.
.
.
t.start();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store