Showing posts with label MultiThreading. Show all posts
Showing posts with label MultiThreading. Show all posts

Thursday, 8 March 2012

Thread Groups in Java

•If a program consists of large number of threads, we can divide them into groups and control each group as a whole.
•This can be achieved by using the predefined class in Java namely “ThreadGroup”.
•Every thread by default is assigned to a default group generally the “main” group.
•To create a new thread group, we must use the constructor of the “ThreadGroup” class. It is available in “java.lang” package.
•Following are the constructors in the “Threadgroup” class:
1) ThreadGroup(String Groupname)
2) ThreadGroup(ThreadGroup parent, String name)


•A thread must be assigned to a thread group, before calling the start() method of the thread. Once the start() method has been invoked, we cannot change the thread group.
•After creating a thread group, we can add threads to it by using the following constructors available in the “Thread” class:
1) Thread(ThreadGroup group, Runnable object)
2) Thread(ThreadGroup group, String name)
3) Thread(ThreadGroup group, Runnable object, String name)

Daemon Threads

•In Java, there are two types of threads:
1) User threads
2) Daemon threads

•When a program contains user threads, JVM will not terminate the execution of the program unless all the user threads have completed their execution.
•When a program contains only daemon thread(s) without any user threads, then JVM can terminate the execution of the program eventhough the daemon thread(s) is/are running.
•A daemon thread is a thread which runs in the background continuously.
•A daemon thread provides services to the user threads.


•An user thread can be converted into a daemon thread by using the following method:
public final void setDaemon(boolean true/false)
•We can check whether a thread is a daemon thread or not by using the following method:
public final boolean isDaemon()
•If we create a child thread to a parent thread, and the parent thread is a daemon thread, then the child thread will also be a daemon thread automatically.

Suspending, Resuming and Stopping Threads

•We can suspend, resume and stop threads whenever we want. The way of suspending, resuming and stopping a thread has been changed since version 1.0 and in modern versions beginning with Java 2.
•In version 1.1 and prior to this version three methods were available in the Thread class. They are:
1) final void suspend() – To suspend the thread
2) final void resume() – To resume the thread
3) final void stop() – To terminate the thread


•In Java 2 the suspend() thread was deprecated as it may cause serious problems like deadlocks.
•Since suspend() method is deprecated, without it resume() method cannot be used.
•The stop() method is also deprecated as using it may lead to serious system failures like incomplete updates.
•So all the three methods suspend(), resume() and stop() must not be used in your programs.
•How to come around this problem?
•The solution is to use a flag variable and make the run() method check this flag variable.
•If the variable’s value is “suspend”, the thread will be suspended.
•If the variable’s value is “resume”, the thread will be resumed.

Thread synchronization in Java

•When two or more threads access the same resource, we must ensure that only one thread can access the resource at a time. The process which allows this is known as “synchronaization”.
•Java provides support for synchronization.
•The key concept in synchronization is the “monitor” (also called as a semaphore).
•A monitor is an object that is used as an mutually exclusive lock or “mutex”.
•Only one thread can own a monitor at a time.

•A thread that acquires a lock is said to “enter” the monitor. When a thread enters the monitor, no other thread can enter the monitor. All other threads are said to be “waiting” for the monitor.
•Threads can be synchronized in two ways:
1) Using synchronized methods.
2) Using the synchronized statement.

Using synchronized methods:
synchronized method



Using synchronized block:
synchronized block