1.What is Thread?
A thread in Java is nothing but an instance of class which is running independently to complete its job.
Thread is created in Java with the help of java.lang.Thread class, and hence it is controlled using the same class.
Threads executes parallel without waiting for main program/each other and completes its job, unless we make it interact with each other for some logical purpose or sharing some common resources.
Multiple threads running in an application and interacts with each other to complete a common goal is called multi-threading.
When threads are running in parallel, thread executes on priority. Every thread has its own priority. High priority threads will be executed before the low priority threads.
Threads can be created in Java in 2 ways.
- By extending the Thread class
- By Implementing Runnable Interface
1.1 Extending Thread Class:
Anybody can create their own thread class by extending this Thread.java class.
The run() method of Thread class needs to be overridden to implement our functionality of the Thread.
Hence user can implement the own functionality of thread by writing the logic in the overridden run() method.
Thread can be executed by calling the start() method in the Thread which is present in the Thread.java class.
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 |
package my.tutorialflow.thread; public class ThreadExtendingExample { public static void main(String [] args) { ReadThread readThread1 = new ReadThread(); ReadThread readThread2 = new ReadThread(); ReadThread readThread3 = new ReadThread(); readThread1.start(); readThread2.start(); readThread3.start(); WriteThread writeThread1 = new WriteThread(); WriteThread writeThread2 = new WriteThread(); WriteThread writeThread3 = new WriteThread(); writeThread1.start(); writeThread2.start(); writeThread3.start(); } } class ReadThread extends Thread { @Override public void run() { System.out.println("Read Thread started..." + this.getName()); System.out.println(this.getName() + " - Executing core function of Reading a File or Stream..."); // Making the Thread sleeping for sometime, looks like its processing something... try { Thread.sleep(1000); }catch(InterruptedException e) {} System.out.println("Read Thread Ended.."); } } class WriteThread extends Thread { @Override public void run() { System.out.println("Write Thread started..." + this.getName()); System.out.println(this.getName() + " - Executing core function of Write into a File or Stream..."); // Do Something -- Just Iterating for some time .. faking some process... for(int i=0;i<99999999;i++) for(int j=0;j<9999999;j++); System.out.println("Write Thread Ended.."); } } |
We could in the example, threads are executing parallel of its own independently not waiting for each other. Hence the result is printed whenever thread is called and executed., and not waiting for other.
Result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Read Thread started...Thread-1 Read Thread started...Thread-0 Read Thread started...Thread-2 Thread-0 - Executing core function of Reading a File or Stream... Thread-1 - Executing core function of Reading a File or Stream... Thread-2 - Executing core function of Reading a File or Stream... Write Thread started...Thread-4 Write Thread started...Thread-5 Write Thread started...Thread-3 Thread-5 - Executing core function of Write into a File or Stream... Thread-4 - Executing core function of Write into a File or Stream... Thread-3 - Executing core function of Write into a File or Stream... Write Thread Ended.. Write Thread Ended.. Write Thread Ended.. Read Thread Ended.. Read Thread Ended.. Read Thread Ended.. |
1.2 Implementing Runnable Interface:
Thread can also created by implementing Runnable interface.
Runnable interface run() method is called inside a run() method of thread.
Hence object implements Runnable interface will be passed as a argument to a Thread object.
When the Thread object run() method is invoked, the run() method we implemented for the Runnable interface in our object will be invoked. Hence thread will be started.
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 52 53 54 55 56 |
package my.tutorialflow.thread; public class RunnableThreadExample { public static void main(String[] args) { Thread readThread1 = new Thread(new ReaderThread()); Thread readThread2 = new Thread(new ReaderThread()); Thread readThread3 = new Thread(new ReaderThread()); Thread writeThread1 = new Thread(new WriterThread()); Thread writeThread2 = new Thread(new WriterThread()); Thread writeThread3 = new Thread(new WriterThread()); readThread1.start(); readThread2.start(); readThread3.start(); writeThread1.start(); writeThread2.start(); writeThread3.start(); } } class ReaderThread implements Runnable { @Override public void run() { System.out.println("Reader Thread started..." + Thread.currentThread().getName()); System.out.println(Thread.currentThread().getName() + " - Executing core function of Reading a File or Stream..."); // Making the Thread sleeping for sometime, looks like its processing something... try { Thread.sleep(1000); }catch(InterruptedException e) {} System.out.println("Reader Thread Ended.."); } } class WriterThread implements Runnable { @Override public void run() { System.out.println("Writer Thread started..." + Thread.currentThread().getName()); System.out.println(Thread.currentThread().getName() + " - Executing core function of Write into a File or Stream..."); // Do Something -- Just Iterating for some time .. faking some process... for(int i=0;i<99999999;i++) for(int j=0;j<9999999;j++); System.out.println("Writee Thread Ended.."); } } |
We could in the example, threads are executing parallel of its own independently not waiting for each other. Hence the result is printed whenever thread is called and executed., and not waiting for other.
Result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Reader Thread started...Thread-0 Writer Thread started...Thread-4 Writer Thread started...Thread-5 Writer Thread started...Thread-3 Reader Thread started...Thread-2 Reader Thread started...Thread-1 Thread-2 - Executing core function of Reading a File or Stream... Thread-3 - Executing core function of Write into a File or Stream... Thread-5 - Executing core function of Write into a File or Stream... Thread-4 - Executing core function of Write into a File or Stream... Thread-0 - Executing core function of Reading a File or Stream... Thread-1 - Executing core function of Reading a File or Stream... Writee Thread Ended.. Writee Thread Ended.. Writee Thread Ended.. Reader Thread Ended.. Reader Thread Ended.. Reader Thread Ended.. |