Take instructor-led Live class on Java Tutorial at :
http://www.edureka.in/java-course
The following topics were covered in this Java Tutorial:
Threads, Thread Lifecycle, Runnable, Thread Class, Synchronization, Deadlock, Examples
Thread:
A thread is a part of execution in a program. The JVM (Java Virtual Machine) allows an application to have multiple threads execution (running) concurrently. So that you can get the multi tasking feature available in your system. Hence multiple threads will be working in the back ground. Threads are lighter than processes i.e threads consume less memory as well as execution is faster.
E.g. of thread in our day to day life in computer: suppose you have a computer. Now in that computer you are playing game and got other tasks like notepad opened up, playing music, downloading files, browse internet and so on. You are using all these things simultaneously i.e. performing multitask all together. This is done with the help of threads. All these tasks are treated as threads and executing simultaneously.
Why do we use threads?
Thread is a light weight process as it shares the same memory and the resources of the process in which it is created. It shares the memory and resources
It promotes multi threading also.
Life --Cycle of a Thread:
When we start a program create a thread it enters into NEW state. The thread will enter into RUNNABLE state when we start the thread. You can make the thread WAIT using wait() available and later thread will be unlocked. You can make the thread SLEEP i.e thread will sleep for some time and again start executing. So you can stop executing by using sleep() available. Finally when the task is completed it gets TERMINATED and ends after that.
Thread -- Methods:
You create a new thread then the thread will be in New state. When you have to start the thread we use t.start() where t is the thread. It will come to Ready state indicate that the thread is ready to execute. After starting the thread you will be running the thread. It comes to the Running state. It completes the task and terminates i.e in Terminated state. Or else you can send the thread to Blocked state by using t.sleep() and t.wait().in the blocked state the thread will be blocked and do not do anything. After that when it is coming back to Ready state it notify the other threads that I am coming back by using notify() and notifyAll(). After that the thread will move from Ready state to Running state and when the execution is completed the thread will terminate. The thread will continue executing this life cycle until it completes its execution completely.
What is the difference between sleep() and wait()? -- both are used to block the threads. But the difference is that consider there is a thread using a resource and another thread is waiting for that resource. So the second thread has to wait until the resource gets free. When resource is free the first thread will notify the other threads which are waiting for the resource where as sleep() will wait for a particular time but do not get notify.
Thread Priorities:
When multiple threads are running, the order of execution of threads depend on priority given to thread. Priority values range from 1 to 10. If you do not set the priority to any thread then its priority will taken as 5 which is default priority. We use setPriority(priority_value) to set the priority to thread.
Threads can be created in two ways:
1. Implementing Runnable interface
2. Extending Thread class
Thread t= new Class1();
t.start();
These statements will also work when you are creating a thread for a class that extends Thread class.
Synchronization:
E.g: consider I have two friends and a resource like pen. Both of them wants the same pen. So what I have to do is to synchronise the object(pen) i.e when first one is using, the second one cannot use it until the pen is released by first one and when the second one is using the pen then first one cannot use it until the pen is released by second one.
Inter-Thread Communication:
e.g.: suppose you went to buy a movie ticket and stand in the queue. The first person standing in the queue will get the ticket and ask the second person to wait. When the first person comes out he sends notify to second. Now the second will get the ticket and ask the third person to wait. This process will continue until either the tickets are finished or no persons left in the queue.The methods used for communication between threads are: wait(), notify(), notifyAll()
Please write back to us at support@edureka.in or call us at +91-8880862004 for more information.
http://www.edureka.in
Visit - http://www.edureka.in/java-course
In questa pagina del sito puoi guardare il video online Java Tutorial - 6 | Edureka della durata di ore minuti seconda in buona qualità , che l'utente ha caricato edureka! 21 marzo 2013, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 30,943 volte e gli è piaciuto 38 spettatori. Buona visione!