Multithreading in Java
Multithreading is a process of running two parts of
the same program to run concurrently or simultaneously . It is a technique that
allows a program or a process to execute many tasks concurrently (at the same
time and parallel). It allows a process to run its tasks in parallel mode on a
single processor system
In the multithreading concept, several multiple
lightweight processes are run in a single process/task or program by a single
processor. For Example, When you use a word processor you perform a
many different tasks such as printing, spell checking and so on.
Multithreaded software treats each process as a separate program.
In Java, the Java Virtual
Machine (JVM) allows an application to have multiple threads of
execution running concurrently. It allows a program to be more responsible to
the user. When a program contains multiple threads then the CPU can switch
between the two threads to execute them at the same time.
Types of Multitasking:
1.
Process Based
2.
Thread Based
A Process Based multitasking is the feature that
allows the computer to run 2 or more programs concurrently. Process-based
multitasking has a larger overhead than thread-based multitasking. In
process-based multitasking, each process requires its own address space in
memory. The operating system requires a significant amount of CPU time to
switch from one process to another process. Programmers call this context
switching, where each process (program) is a context. Additional resources are
needed for each process to communicate with each other.
In a Thread Based multitasking environment, the
thread is the smallest unit of dispatch able code. A single program can perform
2 or more tasks simultaneously. the
threads in thread-based multitasking share the same address space in memory
because they share the same program. This also has an impact on context
switching, because switching from one part of the program to another happens
within the same address space in memory. Likewise, communication among parts of
the program happens within the same memory location.
Multithreading enables to write very efficient
programs that make maximum use of the CPU.
States of a Thread:
1. New
Born or Ready to run
2. Runnable
3. Blocked
4. Resumed
5. Terminated
Thread Priorities:
A thread can voluntarily relinquish control- examine
all threads & the highest priority thread is given to the CPU.
A thread can be preempted by a higher-priority
thread- When the higher-priority thread wants to run, it is executed.
Java priorities are in the range between
MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default,
every thread is given priority NORM_PRIORITY (a constant of 5).
Creating a
Thread
1. Create
a class that implements Runnable Interface
2. Create
a class as a subclass of Thread
Creating
a thread using runnable interface:
The easiest way to
create a thread is to create a class that implements the Runnable interface.
while implementing Runnable, a class need to implement only a single method called run( ),
which is declared like this:
Syntax:
Class classname implements Runnable{
Public void run(){
//Body of the thread code
}}
After creating a class that implements Runnable, we
will instantiate an object of type Thread from within that class. Thread
defines several constructors.
The one that we will use is shown here:
Thread(Runnable threadObject, String threadName);
Here threadObject is an instance of a class that implements the
Runnable interface and the name of the new thread is specified by threadName.
After the new thread is created, it will not start
running until we call its start( ) method, which is declared within Thread.
The start( ) method is shown here:
void start( );
Creating
a thread using Thread Class:
The another way to create a thread is to create a class
that extends the Thread class.
Syntax:
Class classname extends Thread{
Public void run(){
//Body of the thread code
}}
Simple Thread Example:
class MyThread extends Thread {
private
String name, msg;
public
MyThread(String name, String msg) {
this.name = name;
this.msg = msg;
}
public
void run() {
System.out.println(name + " starts its execution");
for
(int i = 0; i < 5; i++) {
System.out.println(name + " says: " + msg);
try { Thread.sleep(500); } catch
(InterruptedException ie) {}
}
System.out.println(name + " finished execution");
}}
public class Threadprogram {
public
static void main(String[] args) {
MyThread
mt1 = new MyThread("thread1", "ping");
MyThread
mt2 = new MyThread("thread2", "pong");
mt1.start();
mt2.start();
for(int
k=1;k<=10;k++)
System.out.println("ok");
}
}
Visit : www.sssedu.in\
No comments:
Post a Comment