Threads are lightweight processes. Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files.
Multithreaded execution is an essential feature of the Java platform.
Showing posts with label Threads. Show all posts
Showing posts with label Threads. Show all posts
Sunday, July 4, 2010
Creating a Thread
Java defines two ways in which threads can be created:
1. Implement the Runnable interface.
2. Extend the Thread class.
Implement the Runnable interface:
Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable.
1. To implement Runnable, a class need only implement a single method called run( ). Inside run( ), you will define the code that constitutes the new thread.
2. After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class.
NewThread=new Thread(Runnable threadOb, String threadName)
/* threadOb is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. threadName is the name of the new thread */
3. After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread. In essence, start( ) executes a call to run( ).
class MyThread implements Runnable
{
Thread t;
MyThread()
{
t = new Thread(this, "Demo Thread");
t.start();
}
public void run()
{
Thread.sleep(500);
}
}
Extend the Thread class:
The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread.
class NewThread extends Thread
{
NewThread()
{
super("Demo Thread");
start();
}
public void run()
{
Thread.sleep(500);
}
}
1. Implement the Runnable interface.
2. Extend the Thread class.
Implement the Runnable interface:
Runnable abstracts a unit of executable code. You can construct a thread on any object that implements Runnable.
1. To implement Runnable, a class need only implement a single method called run( ). Inside run( ), you will define the code that constitutes the new thread.
2. After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class.
NewThread=new Thread(Runnable threadOb, String threadName)
/* threadOb is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. threadName is the name of the new thread */
3. After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread. In essence, start( ) executes a call to run( ).
class MyThread implements Runnable
{
Thread t;
MyThread()
{
t = new Thread(this, "Demo Thread");
t.start();
}
public void run()
{
Thread.sleep(500);
}
}
Extend the Thread class:
The second way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run( ) method, which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread.
class NewThread extends Thread
{
NewThread()
{
super("Demo Thread");
start();
}
public void run()
{
Thread.sleep(500);
}
}
Thread Priority
Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread’s priority is used to decide when to context switch from one running thread to the next.
ThreadName.setPriority(int number);
/* number from 1 to 10, 1 =MIN_PRORITY 5=NORM_PRIORITY 10=MAX_PRIORITY */
A thread can voluntarily relinquish control by using yield(). A thread can be preempted by a higher-priority thread.
ThreadName.setPriority(int number);
/* number from 1 to 10, 1 =MIN_PRORITY 5=NORM_PRIORITY 10=MAX_PRIORITY */
A thread can voluntarily relinquish control by using yield(). A thread can be preempted by a higher-priority thread.
Monitor
A monitor is a control mechanism that is used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a given time. When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor. These other threads are said to be waiting for the monitor. A thread that owns a monitor can re-enter the same monitor if it so desires.
Synchronized keyword
Synchronization
When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization.
Synchronization can be achieved in two ways:
Using Synchronized Methods
synchronized return_type methodname()
{
//method body
}
Using the Synchronized Statement on an Object of a Class
synchronized(object)
{
// statements to be synchronized
}
When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization.
Synchronization can be achieved in two ways:
Using Synchronized Methods
synchronized return_type methodname()
{
//method body
}
Using the Synchronized Statement on an Object of a Class
synchronized(object)
{
// statements to be synchronized
}
Inter-thread Communication
Java includes an interprocess communication mechanism via the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized context.
• wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
• notify( ) wakes up the first thread that called wait( ) on the same object.
• notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.
• wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
• notify( ) wakes up the first thread that called wait( ) on the same object.
• notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.
Deadlock
A deadlock is a special type of error which occurs when two or more threads have a circular dependency on synchronized objects.
Subscribe to:
Posts (Atom)
