Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Thread introduction - Multithreading in java: Thread: A thread is a single flow of control like simple program. A unique property of java is multithreading only because java supports multithreading. More than one thread (program) run simultaneously is known as multithreading (multiprogramming). In multithreading java interpreter handles the switching of control between the threads in such a way that it looks like they are running concurrently. Multithreading is useful in a number of ways. We can divide a long program into number of threads and executes them in parallel. We are going to learn so many useful things in thread lets start with single main thread. Thread Life Cycle: Thread has many different state throughout its life. 1 Newborn State 2 Runnable State 3 Running State 4 Blocked State 5 Dead State Thread should be in any one state of above and it can be move from one state to another by different methods and ways. 1 Newborn State When we create a thread it will be in Newborn State. The thread is just created still its not running. We can move it to running mode by invoking the start() method and it can be killed by using stop() method. 2 Runnable State It means that thread is now ready for running and its waiting to give control. We can move control to another thread by yield() method. 3 Running State It means thread is in its execution mode becaause the control of cpu is given to that particular thread. It can be move in three different situation from running mode. These all are different methods which can be apply on running thread and how the state is changing and how we can come in our original previous state using different methods are shown in above figure. 4 Blocked State A thread is called in Blocked State when it is not allowed to entering in Runnable State or Running State. It happens when thread is in waiting mode, suspended or in sleeping mode. 5 Dead State When a thread is completed executing its run() method the life cycle of that particular thread is end. We can kill thread by invoking stop() method for that particular thread and send it to be in Dead State. Create Thread: We can create thread in java with two different ways. 1. Extending the Thread Class. 2. Implements runnable interface. lets try to understand step by step both of this. 1. Extending the Thread class: In this method one normal class extends the inbuilt class thread and override its run() method with the code required by that particular thread. Here we are going to extend class java.lang.Thread. so we can access all the methods of thread class. Create one class which extends the thread class. Override the run method and put lines of code inside thread method that will be perform by thread. Create an object of class which we created with extending the thread class and call the start() method to execute the thread. Class MyThread extends Thread { ……………….. ……………….. } So now we have one thread named MyThread. Implementing the run() method Public void run() { ……… // Thread code here } Starting new Thread MyThread aTh = new MyThread(); // it instantiates a new object of class MyThread aTh.start(); // invokes run() method EX: class A extends Thread { public void run() { System.out.println("Thread A"); for(int i=1;i<=5;i++) { System.out.println("From thread A i = " + i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { System.out.println("Thread B"); for(int i=1;i<=5;i++) { System.out.println("From thread B i = " + i); } System.out.println("Exit from B"); } } public class Thread_Class { public static void main(String[] args) { new A().start(); //creating A class thread object and calling run method new B().start(); //creating B class thread object and calling run method System.out.println("End of main thread"); } } Output 1: Thread A From thread A i = 1 From thread A i = 2 From thread A i = 3 From thread A i = 4 From thread A i = 5 Exit from A End of main thread Thread B From thread B i = 1 From thread B i = 2 From thread B i = 3 From thread B i = 4 From thread B i = 5 Exit from B Output 2: Thread A From thread A i = 1 From thread A i = 2 End of main thread From thread A i = 3 From thread A i = 4 Thread B From thread A i = 5 From thread B i = 1 Exit from A From thread B i = 2 From thread B i = 3 From thread B i = 4 From thread B i = 5 Exit from B Here you can see that both outputs are different though our program code is same. It happens in thread program because they are running concurrently on their own. Threads are running independently of one another and each executes whenever it has a chance. 2. Implementing the Runnable interface: In this method we have one interface named runnable and we implements this interface for implement a thread. Create one class which implements runnable interface. Override the run() method and put some line of code for that particular thread. Now create an object of inbuilt thread class and create an object of class that implements runnable interface. Give the reference of object to thread object by passing an argument (argument must be the object of class which implements the runnable interface) while creating a thread object. Call the start() method to run the thread. EX: public class Thread_Interface { public static void main(String[] args) { X x1 = new X(); //class object Thread xthread = new Thread(x1); //creating thread object and giving reference of class object to thread object xthread.start(); System.out.println("End of main Thread"); } } class X implements Runnable { public void run() { System.out.println("Inside X thread"); for(int i=1;i<=10;i++) { System.out.println("From xthread i = " +i); } System.out.println("Exit from X"); } } Output : End of main Thread Inside X thread From xthread i = 1 From xthread i = 2 From xthread i = 3 From xthread i = 4 From xthread i = 5 From xthread i = 6 From xthread i = 7 From xthread i = 8 From xthread i = 9 From xthread i = 10 Exit from X Java Thread Priority: Each java thread has its own priority which decides the order of thread to be schedule. The threads of equal priority will be given same treatment by java scheduler. And they will follow the FCFS (First Come First Serve) algorithm. User can also set the priority of thread by using the setPriority() method as follow: ThreadName.setPriority(int Number); Here the number is integer value between 1 to 10, Here 1 is minimum priority 10 is maximum priority. The Thread class defines few priority constants: MIN_PRIORITY = 1 NORM_PRIORITY = 5 MAX_PRIORITY = 10 In any Thread the default priority is NORM_PRIORITY In multithreading by assigning priority we can answer an input as quickly as we want. Whenever more than one threads are ready to run java system select the highest priority thread and execute it If another thread of higher priority comes the running thread will be preempted by the incoming thread and current thread will move to runnable state. EX: class A extends Thread { public void run() { System.out.println("ThreadA strated"); for(int i = 1; i<=5; i++) { System.out.println("\t From ThreadA i = " +i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { System.out.println("ThreadB strated"); for(int j = 1; j<=5; j++) { System.out.println("\t From ThreadB j = " +j); } System.out.println("Exit from B"); } } public class Thread_Priority { public static void main(String[] args) { A threadA = new A(); B threadB = new B(); threadA.setPriority(Thread.MIN_PRIORITY); threadB.setPriority(threadA.getPriority()+3); System.out.println("Start Thread A"); threadA.start(); System.out.println("Start Thread B"); threadB.start(); System.out.println("End of main Thread"); } } Output: Start Thread A Start Thread B End of main Thread ThreadB strated From ThreadB j = 1 From ThreadB j = 2 From ThreadB j = 3 From ThreadB j = 4 From ThreadB j = 5 Exit from B ThreadA strated From ThreadA i = 1 From ThreadA i = 2 From ThreadA i = 3 From ThreadA i = 4 From ThreadA i = 5 Exit from A Here You can see that we start ThreadA first but than also ThreadB completed its task first because of higher priority. But again its multithreading so we all know that output may be vary each time you run the program.