This class presents the different ways to implement threads : extending the Thread class, implementing the Runnable interface anonymously and using lambda expression.
Get the sources of the example from the following GitHub url : https://github.com/javaspringexamples/ThreadCreationExamples.git.
Or Download a .zip file : https://github.com/javaspringexamples/ThreadCreationExamples/archive/master.zip.
package javaspringexamples.ThreadExamples;
/**
* This class presents how to implement a thread by extending the Thread class.
*
* @author mounir.sahrani@gmail.com
*
*/
public class ThreadByExtension extends Thread {
@Override
public void run() {
System.out.println("Running " + this.getClass());
}
}
package javaspringexamples.ThreadExamples;
/**
* This class presents how to implement a thread by implementing the Runnable
* interface.
*
* @author mounir.sahrani@gmail.com
*
*/
public class ThreadByImplementation implements Runnable {
@Override
public void run() {
System.out.println("Running " + this.getClass().getName());
}
}
package javaspringexamples.ThreadExamples;
/**
* This class presents the different ways to implement threads.
*
* @author mounir.sahrani@gmail.com
*
*/
public class TestThread {
public static void main(String[] args) {
// Thread by extending the Thread class.
ThreadByExtension t1 = new ThreadByExtension();
t1.start();
// Thread by implementing the Runnable interface.
Thread t2 = new Thread(new ThreadByImplementation());
t2.start();
// Thread by implementing the Runnable interface anonymously.
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Running Anonymous object runnable.");
}
};
Thread t3 = new Thread(r1);
t3.start();
// Thread by implementing the Runnable interface anonymously using
// lambda expression.
Runnable r2 = () -> System.out.println("Running runnable object using lambda expression Runnable.");
Thread t4 = new Thread(r2);
t4.start();
}
}
