240 likes | 701 Views
(**** Java Certification Training: https://www.edureka.co/java-j2ee-soa-training ****) <br>This Edureka tutorial on u201cJava Threadsu201d will talk about one of the core concepts of Java i.e Java Threads. It will give you a complete insight into how to create, work and synchronize with multiple threads. Through this tutorial you will learn the following topics: <br>What is a Java Thread? <br>Thread Lifecycle <br>Creating a Thread <br>Main Thread <br>Multi-Threading <br>Thread Pool <br><br>Check out our Java Tutorial blog series: https://goo.gl/osrGrS <br><br>Check out our complete Youtube playlist here: https://goo.gl/gMFLx3 <br>Follow us to never miss an update in the future. <br><br>Instagram: https://www.instagram.com/edureka_learning/ <br>Facebook: https://www.facebook.com/edurekaIN/ <br>Twitter: https://twitter.com/edurekain <br>LinkedIn: https://www.linkedin.com/company/edureka
E N D
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java String What is a Java Thread? Main Thread 01 05 Thread Lifecycle Multi Threading 02 06 Creating a Thread Thread Pool 03 07 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
What is a Java Thread? JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
What is a Java Thread? Thread is a lightweight sub process It is the smallest independent unit of a program Contains a separate path of execution Every Java program contains at least one thread A thread is created & controlled by the java.lang.Thread class JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Thread Life-Cycle JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Lifecycle New Runnable A Java thread can lie only in Waiting one of the shown states at any point of time Running Terminated JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Lifecycle New A new thread begins its life cycle in this state & remains here until the program starts the thread. It is also known as a born thread. New Runnable Once a newly born thread starts, the thread comes under runnable state. A thread stays in this state is until it is executing its task. Runnable Running Waiting In this state a thread starts executing by entering run() method and the yield() method can send them to go back to the Runnable state. Running Waiting A thread enters this state when it is temporarily in an inactive state i.e it is still alive but is not eligible to run. It is can be in waiting, sleeping or blocked state. Terminated Terminated A runnable thread enters the terminated state when it completes its task or otherwise terminates. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Creating a Thread JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Creating A Thread A thread in Java can be created using two ways Thread Class Runnable Interface public class Thread extends Object implements Runnable public interface Runnable JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Runnable Interface Thread Class 1. Create a Thread class public class MyThread extends Thread { public void run(){ System.out.println(“Edureka’s Thread…"); } public static void main(String[] args) { MyThread obj = new MyThread(); obj.start(); } 2. Override run() method 3. Create object of the class 4. Invoke start() method to execute the custom threads run() JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Runnable Interface Thread Class 1. Create a Thread class implementing Runnable interface public class MyThread implements Runnable { public void run(){ System.out.println(“Edureka’s Thread…"); } public static void main(String[] args) { Thread t = new Thread(new MyThread()); t.start(); } 2. Override run() method 3. Create object of the class 4. Invoke start() method using the object JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
vs Thread Class Runnable Interface JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
vs Thread Class Runnable Interface ✓ Each Thread creates its unique object ✓ Each Thread creates its unique object ✓ More memory consumption ✓ More memory consumption ✓ A class extending Thread class can’t extend any other class ✓ Along with Runnable a class can implement any other interface ✓ Thread class is extended only if there is a need of overriding other methods of it ✓ Runnable is implemented only if there is a need of special run method ✓ Enables tight coupling ✓ Enables loose coupling JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Main Thread JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Main Thread Subtitle for arrow graphic Main thread is the most important thread of a Java Program It is executed whenever a Java program starts Every program must contain this thread for its execution to take place Java main Thread is needed because of the following reasons 1. From this other “child” threads are spawned 2. It must be the last thread to finish execution i.e when the main thread stops program terminates JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Main Thread JVM start() start() Main Thread Other Demon Threads start() start() Child Thread A Child Thread B start() Child Thread C JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Multi Threading In Java JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Multi – Threading Multi threading is the ability of a program to run two or more threads concurrently, where each thread can handle a different task at the same time making optimal use of the available resources Main Thread start() start() start() switching switching Main Thread Main Thread Main Thread JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Pool JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Pool Java thread pool manages the pool of worker threads and contains a queue that keep the tasks waiting to get executed Queue Thread Thread Assignment Task Task Task Task Task New Task Thread Application Task Finished Thread JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Methods Creating Multiple Threads Joining Threads Thread.sleep() Inter Thread Communication Daemon Thread JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training