Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

HOME

java certification tutorial

chapter1 | chapter2 | chapter3 | chapter4 | chapter5 | chapter6 |

chapter7 | chapter8 | chapter9 | chapter10 | chapter11 |

 

Chapter6: Threads

 

  • A Thread is a single sequential flow of control within a program.
  • Java Virtual Machine allows an application to have multiple threads of execution running concurrently .

*There are two ways by which a thread can be created.

  1. One is to declare a class to be a subclass of class Thread.

class MyThread extends Thread{

public void run() {

//your code here

}

}

  • Every subclass should override the run() method of the class Thread.
  • To start a Thread call the start() method of the new subclass of Thread.

*The other way to create a thread is to declare a class that implements the Runnable interface.

  • This class then implements the run method.

ex:public class somethread implements Runnable{

public void run() {

//some code here

}

}

  • This way of implementing a runnable interface is very useful if the class we want to run in a thread already extends some other class.
  • A new thread instance when created is put into new thread state. It gets into runnable state when its start() method gets invoked.
  • Any number of threads can be attached to a single Runnable object.

*When a Thread start() method is called, the run method is executed.

*Every thread has a priority. Threads with a higher priority are executed in preference to threads with lower priority(not always).

*A new thread starts with the same priority as the thread that created it

*The Thread class defines int constants for thread priority

*Thread.MIN_PRIORITY - 1 least priority

*Thread.NORM_PRIORITY - 5 default

*Thread.MAX_PRIORITY - 10 highest priority

*There is no guarantee that a high priority Thread, will always get higher priority.

 

There are four stages in a thread life cycle.

i) new: From the period when the thread is created to the calling of thread's start() method the thread is said to be in a new state.

ii) Runnable: After a thread is started by involving its start() method the thread is said to enter into Runnable state .

iii)Blocked: A thread is blocked when it is waiting for some specific event to occur and it can't run. Threads that are sleeping or waiting on an object Lock are also considered blocked.

*A thread is put into a waiting state by calling the wait() method. This method called can only be made in a synchronized method or block. A latter call in the synchronized code to notify() tells the thread (or not by All() Tells the threads waiting on this object) the lock for this object has become available ,and when the method obtains lock , it goes into the Ready state.

*The sleep() method puts the thread into a sleeping state for a specified amount of time, after which it enters a Ready state. this method throws InterruptedException.

*A thread is put into a suspended state by a call to its suspend() method. it will go back to a ready state when its resume() method is called by another thread.

iv)Dead: A thread becomes dead when it exits the run method that is started.Adead thread cannot be restarted.

 

*JVM categories Threads into two groups user and daemon.

*Each thread may or maynot be marked as daemon.They may be tagged as daemon Threads by the SetDaemon method.

*Program is shutdown if only daemon Threads are left.

*daemon threads are important to perform same background task that can be terminated whenever the main application is finished.

*daemon flag must be set before the Thread is started.

*if a daemon Thread creates a new Thread, it automatically starts life as a daemon.

 

Synchronization:

*To prevent multiple threads modifying the same object, java uses process of synchronization.

*synchronized keyword is used to make a block of code synchronized.

*The code segments within a program that access the same object from seperate,concurrent threads are called critical sections.

*If a thread enters the synchronized block of code it acquires a lock on the block which avoids other threads to enter the block until the object is unlocked.

*The synchronized keyword can be used in two contexts

a).It can be applied to a method, all the code inside the method is protected

b).It can be used as a statement with a specific object tobe locked and an associated block of code.

*Synchronizing threads have a noticeable effect on a program's speed due to extra CPU cycle involved in obtaining and releasing a lock.

*No matter how the thread exits a block of synchronized code, the lock will be released.

*If you override a synchronized method the overriding method is not automatically synchronized.

*Its good to use synchronized code to read and store long and double values because they are not atomic operators.

*java.util.vector class,java.util.Random class protects many of its methods with synchronized.

*Local variables need not to be synchronized because each Thread executing a method has its own copy of a local variable.

*A static method or class can also be synchronized

ex: java.lang.math.random method is a synchronized static method.

 

*wait and notify:wait and notify methods provides more control on Thread objects.

*Inter thread communication is achieved by wait, notify and notifyall methods.

*These methods are final methods.

*wait() method tells the correct thread to give up the monitor and go to sleep until some other thread enters the same method and calls notify.

*The wait method has two forms that specify a time delay.

*public void wait(long milliseconds) waits for notification or until the time at period has elapsed.

*public void wait(long milliseconds,int nanoseconds);

*Besides wait() method, you can use sleep method, but you can easily wakeup a wait with a notify but a sleeping thread cannot be awoken prematurely.

*notify method wokes up a single thread that is waiting on this object's monitor.

*if there are any threads waiting when a notify method is called the awakened thread is choosen orbitrarily.

*notifyAll wakes up all the threads that call wait on same object.the highest priority thread that wakes up will run first.

*wait() method returns InterruptedException.

*yield() method causes currently executing thread object to temporarily pause and allow other threads to execute.

*interrupt method:This method allows one thread to interrupt another thread, it may throw a InterruptedException.

*join method: this method provides a mechanism by which one thread can wait for another to terminate.

*A given thread can join any number of other Threads.

*stop():This method is used to stop the currently running thread. This method is deprecated and is inherently unsafe.

*sleep(), yield(), stop() and wait() methods will stop a thread from executing.

*Deadlock: A deadlock situation occurs when two Threads are trying to gain control of objects,and each has a lock on a resource that the other needs to process.

*Java provides no mechanism to detect or control deadlock situations.

*A thread gives a runnable to a dead state when it exits the run method .

*A Thread may die as a result of either a normal exit from the run method or if an uncaught exception or error such as the ThreadDeath error.

 

 

 

 

take an exam on this topic here
Please submit your ideas on this topic here
sign my guest book here

 

Copyrights anilbachi.8m.com All rights reserved

for queries contact anil_kuchana@yahoo.com