Unit 2_2
Unit 2_2
Input / Output
Java Thread
• Earlier we were unable to run more than one task in parallel. It was a
drawback, and to remove that drawback, Thread Concept was
introduced.
• A Thread is a very light-weighted process, or we can say the smallest
part of the process that allows a program to operate more efficiently
by running multiple tasks simultaneously.
• Threads have a separate path of execution.
• All the tasks are executed without affecting the main program.
• In a program or process, all the threads have their own separate path
for execution, so each thread of a process is independent.
• if a thread gets an exception or an error at the time of its execution, it
doesn't affect the execution of the other threads.
Prepared By Joylin Priya Pinto 2
• All the threads share a common memory and have their own stack, local
variables and program counter.
• When multiple threads are executed in parallel at the same time, this process is
known as Multithreading.
Prepared By Joylin Priya Pinto 3
Single and Multithreaded
Processes
4
• A multithreaded program contains two or more parts that can run
concurrently.
• Each part of such a program is called a thread, and each thread
defines a separate path of execution. Multithreading is a
specialized
form of multitasking.
• There are two types of multitasking : process-based, thread-based.
• Processes-based multitasking is the feature that allows your
computer to run two or more programs concurrently.
• Ex : process-based multitasking enables you to run the Java
compiler at the same time when using a text editor.
• In thread-based multitasking a single program can perform two or
more tasks simultaneously.
• Ex : A text editor can format text at the same time that it is
printing. 5
Threads exist in several states:
• A thread can be running. It can be ready to run as
soon as it gets CPU time.
• A running thread can be suspended, which temporarily
suspends its activity.
• A suspended thread can then be resumed, allowing it
to pick up where it left off.
• A thread can be blocked when waiting for a resource.
• At any time, a thread can be terminated, which halts
its execution immediately. Once terminated, a thread
cannot be resumed.
9
The Main Thread
• When a Java program starts up, one thread begins running
immediately. This is usually called the main thread of our
program.
• The main thread is important for two reasons :
• It is the thread from which other “child” threads will be
spawned.
• Often, it must be the last thread to finish execution because it
performs various shutdown actions.
• Main thread can be controlled through a Thread object.
• To do so, we must obtain a reference to it by calling the method
currentThread(), a public static member of Thread.
10
class CurrentThreadDemo {
public static void main(String args[]) {
Changes the name of
Thread t = Thread.currentThread();
the thread from main
System.out.println("Current thread: " + t); to “My Thread”
t.setName("My Thread");
System.out.println("After name change: " + t);
try { 1.sleep method to pause.
for(int n = 5; n > 0; n--) { 2. The argument
System.out.println(n); specifies the delay in
Thread.sleep(1000); milliseconds.
} 3. It might throw an
} catch (InterruptedException e) { interrupted exception.
System.out.println("Main thread interrupted");
}
}
}
11
The name of the thread, its
priority and the name of its
group.
13
Creating Thread by extending Thread Class
import java.io.*;
import java.util.*;
//calling start method to execute the run() method of the Thread class
t1.start();
}
} Prepared By Joylin Priya Pinto 16
start() method
• The method is used for starting a thread that we have newly created.
• It starts a new thread with a new call stack.
• After executing the start() method, the thread changes the state from New
to Runnable.
• It executes the run() method when the thread gets the correct time to
execute it.
NewThread()
{ Starts the
// Create a new, second thread execution of
t = new Thread(this, "Demo Thread"); thread beginning
System.out.println("Child thread: " + t); at the run()
t.start(); method.
}
22
// This is the entry point for the second thread.
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
23
class ThreadDemo {
public static void main(String args[])
{
new NewThread(); // create a new thread
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
} 24
Creating Multiple Threads