Table of Contents
Java program to implement multithreading
A Java program to implement multithreading.
We can perform multithreading using following two methods.
- Using Thread Class
- Using Runnable interface
Using Thread Class Write a Program to implement multithreading in Java
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 | class Itv extends Thread { public void run() { for(int i=0; i<=7; i++) { System.out.println(" "+ i + " itvoyagers.in"); try { Thread.sleep(500); } catch(Exception ex){} } } } //itvoyagers.in class Social extends Thread { public void run() { for(int i=0; i<=7; i++) { System.out.println(" "+ i + " Follow Us On Instagram ;)"); try { Thread.sleep(500); } catch(Exception ex){} } } } class MultiThreadDemo { public static void main(String[] args) { Itv itv = new Itv(); Social socl = new Social(); itv.start(); socl.start(); } } |
Output

Using Runnable Interface Write a Program to implement multithreading in Java
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 46 47 48 49 50 51 | class Itv implements Runnable { public void run() { for(int i=0; i<=7; i++) { System.out.println(" "+ i + " itvoyagers.in"); try { Thread.sleep(500); } catch(Exception ex){} } } } //itvoyagers.in class Social implements Runnable { public void run() { for(int i=0; i<=7; i++) { System.out.println(" "+ i + " Follow Us On Instagram ;)"); try { Thread.sleep(500); } catch(Exception ex){} } } } //itvoyagers.in class RunnableDemo { public static void main(String[] args) { Itv itv = new Itv(); Social social = new Social(); /* Runnable itv = new Itv(); // We can also use this method to run thread Runnable social = new Social(); // It will work because Thread() accept runnable */ Thread t1 = new Thread(itv); Thread t2 = new Thread(social); t1.start(); t2.start(); } } |
Output

PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.

ITVoyagers
Author