[go: up one dir, main page]

0% found this document useful (0 votes)
8 views63 pages

Chapter 7

The document provides an overview of multithreaded programming in Java, explaining the concepts of multithreading, multitasking, and multiprocessing. It details how to create threads using the Thread class and Runnable interface, as well as the various states of a thread and thread control methods. Additionally, it discusses thread scheduling, including user and daemon threads, and the differences between non-preemptive and preemptive scheduling.

Uploaded by

2370197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views63 pages

Chapter 7

The document provides an overview of multithreaded programming in Java, explaining the concepts of multithreading, multitasking, and multiprocessing. It details how to create threads using the Thread class and Runnable interface, as well as the various states of a thread and thread control methods. Additionally, it discusses thread scheduling, including user and daemon threads, and the differences between non-preemptive and preemptive scheduling.

Uploaded by

2370197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

OBJECT ORIENTED PROGRAMMING WITH JAVA

Multithreaded Programming in Java – I

Debasis Samanta
Department of Computer Science & Engineering
Indian Institute of Technology Kharagpur
What is Multithreading?
A single threaded program

begin
class ABC
{

public void main(..) body
{


}
}
end
A multithreaded program

Main thread

Threads may switch or exchange


data/ results among them

Thread Thread Thread


A B C
A multithreaded program
main
main
public class X {
main () { read() read()
. read()
{ ...};
. sort() sort()

{...};
sort() calculate() search()
. calculate() calculate()
{...};
. search()
{...}; search()

. print()
{...}; print() print()
}
The concept

Multiple tasks in computer How does computer do everything at once?

• Draw and display images on screen • Multitasking


• Check keyboard and mouse input • Multiprocessing
• Send and receive data on network
• Read and write files to disk
• Perform useful computation (editor,
browser, game)
Multitasking (time-sharing)
• Approach
Computer does some work on a task
Computer then quickly switch to next task
Tasks managed by operating system (scheduler)

• Computer seems to work on tasks concurrently


• Can improve performance by reducing waiting
Multitasking can improve performance

Single task →

Two tasks →
Multiprocessing (multi-threading)
• Multiple processing units (multiprocessor).
• Computer works on several tasks in parallel.
• Performance can be improved.

Dual-core AMD 4096 processor


Athlon X2 Cray X1
32 processor
Pentium Xeon
Perform multiple tasks using…
Process

• Definition – executable program loaded in memory


• Has own address space : Variables and data structures (in memory)
• Each process may execute a different program
• Communicate via operating system, files, network
• May contain multiple threads

Thread

• Definition – sequentially executed stream of instructions


• Shares address space with other threads
• Has own execution context : Program counter, call stack (local variables)
• Communicate via shared access to data
• Multiple threads in process execute same program
• Also, known as “lightweight process”
Why Multithreading?
Motivation for multithreading
1. Captures logical structure of problem
 May have concurrent interacting components
 Can handle each component using separate thread
 Simplifies programming for problem

Example

Web server uses Multiple simultaneous


threads to handle … web browser requests
Motivation for multithreading
2. Better utilize hardware resources
 When a thread is delayed, compute other threads
 Given extra hardware, compute threads in parallel
 Reduce overall execution time

Example
Web/ Internet applications
PC client

Internet
Server

Local Area Network

PDA
Serving many users simultaneously
Multithreaded server

Client 1 Process Server process

Server
Threads
Internet

Client 2 Process

Serving multiple clients concurrently


Modern applications need threads

Printing
thread

Editing
thread

“Editing” and “Printing” documents are in background


Multithreaded/ Parallel file copy
writer() reader()
{ {
buff[0]
- - - - - - - - - - - - - - - - - -
lock(buff[i]); lock(buff[i]);
write(src,buff[i] buff[1]
read(src,buff[i]);
); unlock(buff[i]);
unlock(buff[i]); - - - - - - - - -
- - - - - - - - - }
}

Cooperative parallel synchronized


threads
How Multithreading?
Levels of parallelism
Code-granularity
Task i-l Task i Task i+1 Code Item
Sockets Large grain
(task level)
Program
func1 ( ) func2 ( ) func3 ( )
{ { {
.... .... Medium grain
Threads ....
....
.... .... (control level)
} } }
Function (thread)

a ( 0 ) =.. a ( 1 )=.. a ( 2 )=.. Fine grain


b ( 0 ) =.. b ( 1 )=.. b ( 2 )=.. (data level)
Compilers Loop (Compiler)

Very fine grain


+ Load (multiple issue)
CPU * With hardware
Single and multithreaded processes
Single-threaded process Multiplethreaded process
Threads of
Threads are light- execution
weight processes
within a process

Single instruction stream Multiple instruction stream


Common
Address Space
A thread is …
• A piece of code that runs concurrently with other threads.

• Each thread is a statically ordered sequence of instructions.

• Threads are being extensively used to express concurrency on both


single and multiprocessor machines.

• Programming a task having multiple threads of control


Multithreading or multithreaded programming.
Multithreading in Java
Java threads

• Java has built in support for multithreading.


Synchronization
currentThread start setPriority
Thread scheduling
yield run getPriority
Inter-thread communication: sleep stop suspend
resume

• Java Garbage Collector is a low-priority thread.


Everything about thread is readily defined in the package java.lang and in a class Thread and
interface Runnable in it.
Running a thread in Java
Note:
Thread starts executing only if start() is called

Inactive Dead

terminates
Start() run()
Alive
Runnable is interface
• So it can be multiply inherited
• Required for multithreading in applets
Running a thread in Java
Creating Threads with Threa
Creating threads in Java
There are two ways to create and run a thread:
Thread class
public class Thread extends Object { …
}

Runnable interface

public interface Runnable


{
public void run( ); //work⇒ thread
}
Thread class

public class Thread extends Object implements Runnable {


public Thread();
public Thread (String name); //Thread name
public Thread (Runnable R); //Thread R.run()
public Thread (Runnable R, String name);
public void run(); //if no R, work for thread
public void start(); //begin thread execution
...
}
More Thread class methods
public class Thread extends Object {

public static Thread currentThread();
public String getName();
public void interrupt();
public boolean isAlive();
public void join();
public void setDaemon();
public void setName();
public void setPriority();
public static void sleep();
public static void yield();
}
Creating thread in Java programs
1. Thread class
 Extend Thread class and override the run method
Example:

public class MyT extends Thread {


public void run() {
… // work for thread
}
}
MyT T = new MyT () ; // create thread
T.start(); // begin running thread
… // thread executing in parallel
Creating thread : An example
class ThreadC extends Thread
class ThreadA extends Thread
{

{
public void run( ) {
for(int i = 1; i <= 5; i++) {

}class ThreadA extends Thread{ public void run( ) {


System.out.println("From Thread A with i = "+ -1*i);

public void run( ) { for(int k = 1; k <= 5; k++) {


System.out.println("Exiting from Thread A ...");
}
}
for(int i = 1; i <= 5; i++) {
class ThreadB extends Thread System.out.println("From Thread C with k = "+ 2*k-1);
}
{

System.out.println("From Thread
public void run( ) {
for(int j = 1; j <= 5; j++) { A with i = "+ -1*i);
System.out.println("Exiting from Thread C ...");
}
System.out.println("From Thread B with j = "+2* j);

}
}
System.out.println("Exiting from Thread B ..."); }

System.out.println("Exiting
} from Thread A ...");
} class MultiThreadClass
{
class ThreadC extends Thread
} {

public static void main(String args[]) {


public void run( ) {

class ThreadB extends Thread {


for(int k = 1; k <= 5; k++) {

ThreadA a = new ThreadA();


System.out.println("From Thread C with k = "+

public void run( ) {


2*k-1);

ThreadB b = new ThreadB();


}
System.out.println("Exiting from Thread C

for(int j = 1; j <= 5; j++) { ThreadC c = new ThreadC();


...");
}
}
System.out.println("From Thread B with j= "+2* j);
a.start(); class MultiThreadClass
{
} b.start(); public static void main(String args[]) {
ThreadA a = new ThreadA();
c.start();
System.out.println("Exiting fromSystem.out.println("...
Thread B ..."); ThreadB b = new ThreadB();
ThreadC c = new ThreadC();
Multithreadinga.start();
is over ");
} }
b.start();
c.start();

}
System.out.println("... Multithreading is over ");
}
}
OBJECT ORIENTED PROGRAMMING WITH JAVA
Multithreaded Programming in Java – II

Debasis Samanta
Department of Computer Science & Engineering
Indian Institute of Technology Kharagpur
Creating Threads with Runnable
Creating thread in Java
2. Runnable interface
 Create object implementing Runnable interface
 Pass it to Thread object via Thread constructor
Example

public class MyT implements Runnable {


public void run() {
… // work for thread
}
}
Thread T = new Thread(new MyT); // create thread
T.start(); // begin running thread
… // thread executing in parallel
Creating thread : Example
class ThreadZ implements Runnable
class ThreadX implements Runnable {
class ThreadX implements Runnable
{
public void run( ) {
{
for(int i = 1; i <= 5; i++) { public void run( ) {
System.out.println("Thread X with i = "+ -1*i);
}
public void run( ) { for(int k = 1; k <= 5; k++) {
System.out.println("Exiting Thread X ...");
}
for(int i = 1; i <= 5; i++) { System.out.println("Thread Z with k = "+ 2*k-1);
}
}
System.out.println("Thread X with i = "+ -1*i);
class ThreadY implements Runnable {
public void run( ) {
System.out.println("Exiting Thread Z ...");
}
for(int j = 1; j <= 5; j++) {

}
System.out.println("Thread Y with j = "+ 2*j);
}

}}
System.out.println("Exiting Thread
}
System.out.println("Exiting Thread Y ..."); X ...");
} class MultiThreadRunnable {
} public static void main(String args[]) {
class ThreadZ implements Runnable
class ThreadY implements Runnable { ThreadX x = new ThreadX(); {
public void run( ) {
public void run( ) { Thread t1 = new Thread(x); for(int k = 1; k <= 5; k++) {
System.out.println("Thread Z with k = "+ 2*k-1);
for(int j = 1; j <= 5; j++) { ThreadY y = new ThreadY(); }
System.out.println("Exiting Thread Z ...");
Threadj t2
System.out.println("Thread Y with = new
= "+ Thread(y);
2*j); }
}

} Thread t3 = new Thread (new ThreadZ);


class MultiThreadRunnable {
public static void main(String args[]) {
t1.start();
System.out.println("Exiting Thread Y ...");
ThreadX x = new ThreadX();
Thread t1 = new Thread(x);
t2.start(); ThreadY y = new ThreadY();
} Thread t2 = new Thread(y);
t3.start(); Thread t3 = new Thread (new ThreadZ);
} System.out.println("... Multithreading
t1.start();
t2.start();
is over ");
t3.start();
} }
System.out.println("... Multithreading is over ");

} }
States of a Thread
Threads : Thread states of a thread
Java thread can be in one of these states :
New – thread allocated & waiting for start()
Runnable – thread can begin execution
Running – thread currently executing
Blocked – thread waiting for event (I/O, etc.)
Dead – thread finished
Transitions between states caused by
Invoking methods in class Thread
 new(), start(), yield(), sleep(), wait(), notify()…
Other (external) events
 Scheduler, I/O, returning from run()…
Thread States
State diagram
new start notify, notifyAll,
new runnable IO complete,
sleep expired,
yield, join complete
scheduler time resume
slice
running blocked
IO, sleep,
terminate wait, join,
suspend
dead
Thread control methods
start () :→ A newborn thread with this method enter into Runnable state and Java run time create a system thread
context and starts it running. This method for a thread object can be called once only

suspend() :→ This method is different from stop( ) method. It takes the thread and causes it to stop running and later on
can be restored (by resume() )

resume() :→ This method is used to revive a suspended thread. There is no gurantee that the thread will start running
right way, since there might be a higher priority thread running already, but, resume() causes the thread to
become eligible for running

sleep(int n):→ This method causes the run time to put the current thread to sleep for n milliseconds

yield() :→ This method causes the run time to switch the context from the current thread to the next available runnable
thread. This is one way to ensure that the threads at lower priority do not get started
Scheduling of Threads
Daemon threads
Java threads types
 User
 Daemon
− Provide general services.
− Typically never terminate.
− Call setDaemon() before start().
Program termination
 All user threads finish.
 Daemon threads are terminated by JVM.
 Main program finishes.
Threads : Scheduling
Scheduler
Determines which runnable threads to run.
Can be based on thread priority.
Part of OS or Java Virtual Machine (JVM) .

Scheduling policy
Nonpreemptive (cooperative) scheduling.
Preemptive scheduling.
Threads: Non-preemptive scheduling
Threads continue execution until
Thread terminates.
Executes instruction causing wait (e.g., IO).
Thread volunteering to stop (invoking yield or sleep).
Threads: Preemptive scheduling
Threads continue execution until
Same reasons as non-preemptive scheduling.
Preempted by scheduler.
Java thread : An example
public class ThreadExample extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
try {
sleep ((int)(Math.random() * 5000)); // 5 secs
}
catch (InterruptedException e) {
System.out.println (i);
}
}
public static void main(String[ ] args) {
new ThreadExample().start();
new ThreadExample().start();
System.out.println ("Done");
}
}
Java Thread Example
Possible outputs
0,1,2,0,1,2,Done // thread 1, thread 2, main()
0,1,2,Done,0,1,2 // thread 1, main(), thread 2
Done,0,1,2,0,1,2 // main(), thread 1, thread 2
0,0,1,1,2,Done,2 // main() & threads interleaved
Data races
public class DataRace extends Thread {
static int x;
public void run() {
for (int i = 0; i < 100000; i++) {
x = x + 1;
x = x – 1;
}
}
public static void main(String[] args) {
x = 0;
for (int i = 0; i < 100000; i++)
new DataRace().start();
System.out.println(x); // x not always 0!
}
}
Thread scheduling observations

The order in which threads


Thread can block indefinitely Thread scheduling may
are selected for execution is Synchronization
(starvation). cause data races.
indeterminate.
• Depends on scheduler. • If other threads always • Modifying same data from • Control thread execution
execute first. multiple threads. order.
• Result depends on thread • Eliminate data races.
execution order.
Priority of Threads
Thread priority
• In Java, each thread is assigned priority, which affects the order in which
it is scheduled for running.
• The threads so far had same default priority (NORM_PRIORITY) and
they are served using FCFS policy.
Java allows users to change priority:
ThreadName.setPriority (int Number)
MIN_PRIORITY =1
NORM_PRIORITY = 5
MAX_PRIORITY = 10
Thread priority : An example
class A extends Thread class B extends Thread class CC extends
class extends Thread
Thread
class A extends Thread
{ { {{
{
public void run() public void run() public
public void
void run()
run()
public void run()
{ { {{
{
System.out.println ("Thread A System.out.println ("Thread B started"); System.out.println ("Thread
System.out.println ("Thread CC started");
System.out.println ("Thread A started");
started");
started"); for (int j=1;j<=4;j++) for
for (int
(int k=1;k<=4;k++)
k=1;k<=4;k++)
for (int i=1;i<=4;i++) { {{
for (int i=1;i<=4;i++)
{ System.out.println ("\t From System.out.println ("\t
System.out.println ("\t From
From ThreadC:
ThreadC:
{
System.out.println ("\t From k=
k= "+k);
System.out.println ("\t From ThreadB: j= "+j); "+k);
ThreadA: i= "+i);
ThreadA: i= "+i); } }}
} System.out.println ("Exit from B"); System.out.println ("Exit
System.out.println ("Exit from
from C");
C");
}
System.out.println ("Exit from A"); } }}
System.out.println ("Exit from A");
} } }}
}

C extends
extends Thread
}
class
}
class A
B Thread
{{
public
public void
void run()
run()
{{
System.out.println ("Thread
System.out.println ("Thread AC started");
B started");
for (int
for (int j=1;j<=4;j++)
i=1;i<=4;i++)
k=1;k<=4;k++)
{{
System.out.println ("\t
System.out.println ("\t From
From ThreadA:
ThreadC: i=
ThreadB: k= "+i);
j= "+k);
"+j);
}}
System.out.println ("Exit
System.out.println ("Exit from
from A");
C");
B");
}}
}
}
Thread priority : An example
class ThreadPriority
{
public static void main (String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
threadC.setPriority (Thread.MAX_PRIORITY);
threadB.setPriority (threadA.getPriority()+1);
threadA.setPriority (Thread.MIN_PRIORITY);
System.out.println ("Started Thread A");
threadA.start();
System.out.println ("Started Thread B");
threadB.start();
System.out.println ("Started Thread C");
threadC.start();
System.out.println ("End of main thread");
}
}
Thread class : Join

public class Test1 {


static void main(String[] args){
Thread t1 = new Thread(new R(1));
Thread t2 = new Thread(new R(2));
t1.start();
t2.start();
try {
t1.join(); // waits until t1 has terminated
t2.join(); // waits until t2 has terminated
}
catch(InterruptedException e){ }
System.out.println("done");
}
}
Synchronization of Threads
Thread synchronization
When two or more processes attempts to access a shared resource, it should
be synchronized to avoid conflicts.

Java supports methods to be synchronized.

Following is the syntax by which methods can be made to protect from


simultaneous access:
synchronized (object) { block of statement(s) }
Thread synchronization : An example
class Account {
private int balance;
public int accountNo;
// To implement a thread for deposit
void displayBalance( ) {
class TransactionDeposite implements
System.out.println ( "Account No : " + accountNo
Runnable {
+ "Balance : " + balance );
Account accountX;
}
TransactionDeposite (Account x,
synchronized void deposite (int amount ) {
int amount ) {
// Method to deposit an amount
// Constructor to initiate this thread
balance = balance + amount;
accountX = x;
System.out.print( amount + " is deposited " );
this.amount = amount;
displayBalance( ) ;
new Thread (this).start ( );
}
}
synchronized void withdraw (int amount ) {
public void run( ) {
// method to withdraw an amount
accountX.deposite (amount);
balance = balance - amount;
}
System.out.print (amount + "is withdrawn" );
}
displayBalance ( );
}
}
Thread synchronization : An example
// To implement a thread for withdraw class Transaction {
class TransactionWithdraw implements public static void main (String,
Runnable { Account accountY; args[ ] ) {
int amount; Account ABC = new Account ( );
TransactionWithdraw (Account y; // Create an account
int amount ) { ABC.balance = 1000;
accountY = y ; // initialize the account by Rs 1000
this.amount = amount; TransactionDeposite t1;
new Thread (this).start( ); // A thread for deposite
} TransactionWithdraw t2
public void run ( ) { // Another thread for withdarw
accountY.withdraw (amount); t1 = new TransactionDeposite (ABC ,
} 500 );
} t2 = new TransactionWithdraw (ABC,
900);
// Two threads are started
}
}
Thread synchronization : Stack example
Example: Stack
public class Stack {
private int top = 0;
private int[] data = new int [10]; Two threads, one is
public void push(int x) {
pushing, the other
data[top] = x;
top++; popping objects
}
public int pop() {
top--;
return data[top];
}
}
Using blocks
Synchronized blocks
every object contains a single lock
lock is taken when synchronized section is entered
if lock is not available, thread enters a waiting queue
 if lock is returned any (longest waiting?) thread is resumed

public void push(int x) {


synchronized(this){
data[top] = x;
top++;
}
}
Instance methods

Often a method is synchronized on “this”:

public void push(int x) {synchronized(this) {……}}

Short form:

public synchronized void push(int x){……}


Question to think…

• In any software, Input-Output is a great


concern. How Java facilitates I-O handling?
• What makes Java suitable for network
programming?

You might also like