Today, i walk you through the Java Thread Example. Every java application has at least one thread or main thread. Multithreading refers to two or more threads executing concurrently in a single program. A computer single core processor can execute only one thread at a time and time slicing is the OS feature to share processor time between different processes and threads.
Why we use Java Thread
a. Java Threads are lightweight compared to processes, it takes less time and resource to create a thread.
b. Threads share their parent process data and code
c. Context switching between threads is usually less expensive than between processes.
d. Thread intercommunication is relatively easy than process communication.
Java provides two ways to create a thread programmatically
1) Implementing Runnable interface.
To convert a class to runnable, you are able to implement java.lang.Runnable interface and do implementation in public void run() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.javabycode.multithreads; public class MultiThreadExample implements Runnable { @Override public void run() { System.out.println("Start "+Thread.currentThread().getName()); try { Thread.sleep(5000); doSomething(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("End "+Thread.currentThread().getName()); } private void doSomething() throws InterruptedException { //Do stuffs,e.g getting database connection, CRUD operations } } |
2) Extending the java.lang.Thread class.
We can extend java.lang.Thread class to create our own java thread class and override run() method.
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 |
package com.javabycode.multithreads; public class SimpleThread extends Thread { public SimpleThread(String name) { super(name); } @Override public void run() { System.out.println("Using Thread - START "+Thread.currentThread().getName()); try { Thread.sleep(5000); doSomething(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Using Thread - END "+Thread.currentThread().getName()); } private void doSomething() throws InterruptedException { //Do stuffs,e.g getting database connection, CRUD operations } } |
Here is a demo program that shows how to execute our java thread above
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.javabycode.multithreads; public class MultiThreadDemo { public static void main(String[] args){ Thread thread1 = new Thread(new MultiThreadExample(), "thread 1"); Thread thread2 = new Thread(new MultiThreadExample(), "thread 2"); System.out.println("Starting Runnable threads"); thread1.start(); thread2.start(); System.out.println("Runnable Threads has been started"); Thread thread3 = new SimpleThread("thread 3"); Thread thread4 = new SimpleThread("thread 4"); System.out.println("Starting SimpleThread"); thread3.start(); thread4.start(); System.out.println("SimpleThread has been started"); } } |
Output of the above java thread example program like this
Runnable vs Thread
If a class only goal is to run as Thread, It should be extend from Thread class. Whereas, if a class provides more functiionality (e.g implementing multiple interfaces) rather than just running as Thread, it should be implemented Runnable interface.
Note: In Java 8, Runnable is a functional interface and we can use lambda expressions to provide it’s implementation rather than using anonymous class above. The above code can be rewrite like this:
1 2 3 4 |
// Lambda Runnable Runnable thread = () -> { System.out.println("Lambda thread is running"); }; // start the thread new Thread(thread).start(); |
That’s all about Java Thread Example. Happy learning.
References
Processes and Threads
Download the complete source code, click link below
Sample.zip (117 downloads)