A Thread is a small set of instructions designed to be scheduled and executed by the CPU independently of the parent process. To run the process in another thread, you the support of both the operating system and the process. When you create another thread, JVM communicates OS to create a new thread which can then run on the "multi-threaded CPU". Well this article isn't about explain threads, it's about how you can play with them in Java
Threads are everywhere by default
When write and run a simple java program, JVM is creating the main thread for you to print hello world
publicclassSolution {publicstaticvoidmain(String[] args) throwsException {System.out.println("hello world");thrownewException(); }}// output : hello worldException in thread "main"java.lang.Exception at Solution.main(Solution.java:5)
If you read the error message carefully, it says Exception in thread "main" , hence proved ...
How to create a thread ?
Well, cuz Java is an Object-Oriented Language, everything is wrapped in OOP concepts. To run a method of some class in-parallel
Extend the class from the Thread class
Create a method overriding the run() method
This run method is what is executed separately in a thread.
Here is an example :
classHiextendsThread { @Overridepublicvoidrun() {for (int i =1; i <5; i++) {System.out.println("hi");// just sleeping for some timetry {Thread.sleep(400);} catch (Exception e) {} } }}publicclassApp {publicstaticvoidmain(String[] args) {Hi obj1 =newHi();obj1.start(); }}
When you call the .start() method, it will execute the run() method in a seperate thread.
Well becuase java doesnt support mulitple inheritanc, if you ever have to inherit from another class, you wont be able to do it. To overcome this limitation/feature we can implement the Runnable interface
The only difference here is you implement the Runnable interface, create objects of the Thread class passing the objects implementing Runnable, and call the start method on the thread object, which in--return executes the .run() methods.
Properties of a Thread Class
getName() : String gets you the name of the thread
setName(String name) : set name of a thread
isAlive() : Boolean whether the thread is active or not
getId() : Long get the execution id of the thread
getPriority() : get the priority of thread
setPriority(int num) : on a scale of 1-10
Predefined ENUMS
Thread.MAX_PRIORITY = 10
Thread.MIN_PRIORITY = 1
Thread.NORM_PRIORITY = 5
For more, refer to documentation : https://download.java.net/java/early_access/loom/docs/api/java.base/java/lang/Thread.html
Java Thread yield() method : The yield() method of thread class causes the currently executing thread object to temporarily pause and allow other threads to execute.
publicclassMyThreadextendsThread { @Overridepublicvoidrun() {for (int i =0; i <20; i++) {System.out.println("child thread");Thread.yield(); } }}
Inter thread communication methods
wait
notify
notifyAll()
Important Points
using runnable is better because when you extend from thread you cannot inherit from other class
Limitations of using thread and runnable
time consuming : creation and management of thread