Multi Threading Example in Java

Multi threading Can be achieved with the help of ‘Thread Class’ or ‘Runnable Interface’. We have to override the run() method in both the cases and we have to call start() method which automatically calls run method (no need to call run method).

  • If we extend Thread class then we have to call start method with the help of class object.
  • But if you Implement Runnable Interface then we have to explicitly create Thread class object and with that object we have to call start method.
  • If you don’t call start method and simply call run method then it won’t create another thread for you and will be executed by the same thread.
  • If we extend thread class we can not extend another class but if we implement Runnable interface we can extend another class.

Example: In the below example if you  run you can observer that both the threads will be executed in parallel instead one after other as we’ve used the MultiThread concept here with the help of Thread class.

First let’s do an example which extends Thread class to implement MultiThreading. After that we do the same example with the help of Runnable Interface.

package view;

public class MultithreadConcept{
public MultithreadConcept() {
super();
}

public static void main(String[] args) throws InterruptedException {
MultithreadConcept multithreadConcept = new MultithreadConcept();

playSongs ps = new playSongs();
playVideo pv = new playVideo();

ps.start();
pv.start();
}

}
class playSongs extends Thread{
public void run(){
//System.out.println("I am playing song");
for (int i=1; i<=5; i++){
System.out.println("Playing Song:"+i);
try { Thread.sleep(500);} catch (InterruptedException e) {}
}

}
}

class playVideo extends Thread{
public void run(){
// System.out.println("I am playing video");
for (int i=1; i<=5; i++){
System.out.println("Playing Video:"+i);
try { Thread.sleep(500);} catch (InterruptedException e) {}
}
}
}

Output:

Playing Song:1
Playing Video:1
Playing Song:2
Playing Video:2
Playing Song:3
Playing Video:3
Playing Song:4
Playing Video:4
Playing Song:5
Playing Video:5

The same example let’s do it with the help of Runnable Interface.

package view;

public class MultithreadConcept2 {
public MultithreadConcept2() {
super();
}

public static void main(String[] args) {
MultithreadConcept2 multithreadConcept2 = new MultithreadConcept2();

playSongs2 ps = new playSongs2();
playVideo2 pv = new playVideo2();

Thread t1 = new Thread(ps);
Thread t2 = new Thread(pv);
t1.start();
t2.start();
}


}
class playSongs2 implements Runnable{
public void run(){
//System.out.println("I am playing song");
for (int i=1; i<=5; i++){
System.out.println("Playing Song:"+i);
try { Thread.sleep(500);} catch (InterruptedException e) {}
}

}
}

class playVideo2 implements Runnable{
public void run(){
// System.out.println("I am playing video");
for (int i=1; i<=5; i++){
System.out.println("Playing Video:"+i);
try { Thread.sleep(500);} catch (InterruptedException e) {}
}
}
}

Output:

Playing Song:1
Playing Video:1
Playing Song:2
Playing Video:2
Playing Song:3
Playing Video:3
Playing Song:4
Playing Video:4
Playing Song:5
Playing Video:5

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s