Computer Engineering Rabie A Ramadan Lecture 8 Agenda

Computer Engineering Rabie A. Ramadan Lecture 8

Agenda l l l l l Introduction Thread Applications Defining Threads Java Threads and States • Priorities Accessing Shared Resources • Synchronisation Advanced Issues: • Concurrency Models: master/worker, pipeline, peer processing Serialization Reflection Java Beans 2

A single threaded program class ABC { …. public void main(. . ) { …. . } begin body end } 3

A Multithreaded Program Main Thread start Thread A start Thread B start Thread C Threads may switch or exchange data/results 4

Single and Multithreaded Processes threads are light-weight processes within a process Single-threaded Process Multiplethreaded Process Threads of Execution Multiple instruction stream Single instruction stream Common Address Space 5

Multithreaded Server: For Serving Multiple Clients Concurrently Server Process Client 1 Process Server Threads n Internet Client 2 Process 6

Web/Internet Applications: Serving Many Users Simultaneously PC client Internet Server Local Area Network PDA 7

Multithreaded Applications l l Modern Applications need Threads (ex 1): Editing and Printing documents in background. Printing Thread Editing Thread
![Multithreaded/Parallel File Copy reader() { - - - - lock(buff[i]); read(src, buff[i]); unlock(buff[i]); - Multithreaded/Parallel File Copy reader() { - - - - lock(buff[i]); read(src, buff[i]); unlock(buff[i]); -](http://slidetodoc.com/presentation_image_h2/6ba49bddefad1cd5c3d00a96682bfbfe/image-9.jpg)
Multithreaded/Parallel File Copy reader() { - - - - lock(buff[i]); read(src, buff[i]); unlock(buff[i]); - - - - } buff[0] buff[1] writer() { - - - - - lock(buff[i]); write(src, buff[i]); unlock(buff[i]); - - - - - } Cooperative Parallel Synchronized Threads 9 9

What are Threads? l A piece of code that run in concurrent with other threads. l Each thread is a statically ordered sequence of instructions. l Threads are being extensively used to express concurrency on both single and multiprocessors machines. l Programming a task having multiple threads of control – Multithreading or Multithreaded Programming. 10

Java Threads l Java has built in thread support for Multithreading • Synchronization • Thread Scheduling • Inter-Thread Communication: • current. Thread • yield • sleep • resume l start run stop set. Priority get. Priority suspend Java Garbage Collector is a low-priority thread. 11

Threading Mechanisms. . . Create a class that extends the Thread class l Create a class that implements the Runnable interface l Thread My. Thread (objects are threads) [a] Runnable Thread My. Class (objects with run() body) [b] 12

1 st method: Extending Thread class l Create a class by extending Thread class and override run() method: class My. Thread extends Thread { public void run() { // thread body of execution } } l l l Create a thread: My. Thread thr 1 = new My. Thread(); Start Execution of threads: thr 1. start(); Create and Execute: new My. Thread(). start(); 13

An example class My. Thread extends Thread { public void run() { System. out. println(" this thread is running. . . "); } } class Thread. Ex 1 { public static void main(String [] args ) { My. Thread t = new My. Thread(); t. start(); } } 14

2 nd method: Threads by implementing Runnable interface l Create a class that implements the interface Runnable and override run() method: class My. Thread implements Runnable {. . . public void run() { // thread body of execution } } l Creating Object: My. Thread my. Object = new My. Thread(); l Creating Thread Object: Thread thr 1 = new Thread( my. Object ); l Start Execution: thr 1. start(); 15

An example class My. Thread implements Runnable { public void run() { System. out. println(" this thread is running. . . "); } } class Thread. Ex 2 { public static void main(String [] args ) { Thread t = new Thread(new My. Thread()); t. start(); } } 16

Life Cycle of Thread new start() I/O completed ready Time expired/ interrupted notify() waiting sleeping dispatch wait() sleep() running resume() blocked suspend() Block on I/O completion stop() dead 17

A Program with Three Java Threads l Write a program that creates 3 threads 18

Three threads example l l l l l l class A extends Thread { public void run() { for(int i=1; i<=5; i++) { System. out. println("t From Thread. A: i= "+i); } System. out. println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1; j<=5; j++) { System. out. println("t From Thread. B: j= "+j); } System. out. println("Exit from B"); } } 19

l l l l class C extends Thread { public void run() { for(int k=1; k<=5; k++) { System. out. println("t From Thread. C: k= "+k); } System. out. println("Exit from C"); l } l class Thread. Test { public static void main(String args[]) { new A(). start(); new B(). start(); new C(). start(); } } l l l l 20
![Run 1 threads [1: 76] java Thread. Test From Thread. A: i= 1 From Run 1 threads [1: 76] java Thread. Test From Thread. A: i= 1 From](http://slidetodoc.com/presentation_image_h2/6ba49bddefad1cd5c3d00a96682bfbfe/image-21.jpg)
Run 1 threads [1: 76] java Thread. Test From Thread. A: i= 1 From Thread. A: i= 2 From Thread. A: i= 3 From Thread. A: i= 4 From Thread. A: i= 5 Exit from A From Thread. C: k= 1 From Thread. C: k= 2 From Thread. C: k= 3 From Thread. C: k= 4 From Thread. C: k= 5 Exit from C From Thread. B: j= 1 From Thread. B: j= 2 From Thread. B: j= 3 From Thread. B: j= 4 From Thread. B: j= 5 Exit from B l 21
![Run 2 threads [1: 77] java Thread. Test From Thread. A: i= 1 From Run 2 threads [1: 77] java Thread. Test From Thread. A: i= 1 From](http://slidetodoc.com/presentation_image_h2/6ba49bddefad1cd5c3d00a96682bfbfe/image-22.jpg)
Run 2 threads [1: 77] java Thread. Test From Thread. A: i= 1 From Thread. A: i= 2 From Thread. A: i= 3 From Thread. A: i= 4 From Thread. A: i= 5 From Thread. C: k= 1 From Thread. C: k= 2 From Thread. C: k= 3 From Thread. C: k= 4 From Thread. C: k= 5 Exit from C From Thread. B: j= 1 From Thread. B: j= 2 From Thread. B: j= 3 From Thread. B: j= 4 From Thread. B: j= 5 Exit from B Exit from A l 22

Thread Priority l 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: • Thread. Name. set. Priority(int. Number) • • • MIN_PRIORITY = 1 NORM_PRIORITY=5 MAX_PRIORITY=10 23

Accessing Shared Resources l Applications Access to Shared Resources need to be coordinated. • Printer (two person jobs cannot be printed at the • • same time) Simultaneous operations on your bank account. Can the following operations be done at the same time on the same account? • Deposit() • Withdraw() • Enquire() 24

Online Bank: Serving Many Customers and Operations PC client Internet Bank Server Local Area Network Bank Database PDA 25

Shared Resources l l l If one thread tries to read the data and other thread tries to update the same data, it leads to inconsistent state. This can be prevented by synchronising access to the data. Use “Synchronized” method: • public synchronized void update() • { • } • … 26

the driver: 3 rd Threads sharing the same object class Internet. Banking. System { public static void main(String [] args ) { Account account. Object = new Account (); Thread t 1 = new Thread(new My. Thread(account. Object)); Thread t 2 = new Thread(new Your. Thread(account. Object)); Thread t 3 = new Thread(new Her. Thread(account. Object)); t 1. start(); t 2. start(); t 3. start(); // DO some other operation } // end main() } 27

Shared account object between 3 threads class My. Thread implements Runnable { Account account; public My. Thread (Account s) { account = s; } public void run() { account. deposit(); } } // end class My. Thread class Your. Thread implements Runnable { Account account; public Your. Thread (Account s) { account = s; } public void run() { account. withdraw(); } } // end class Your. Thread class Her. Thread implements Runnable { Account account; public Her. Thread (Account s) { account = s; } public void run() {account. enquire(); } } // end class Her. Thread account (shared object) 28

Monitor (shared object access): serializes operation on shared object class Account { // the 'monitor' int balance; // if 'synchronized' is removed, the outcome is unpredictable public synchronized void deposit( ) { // METHOD BODY : balance += deposit_amount; } public synchronized void withdraw( ) { // METHOD BODY: balance -= deposit_amount; } public synchronized void enquire( ) { // METHOD BODY: display balance. } } 29

Thread concurrency/operation models l l l The master/worker model The peer model A thread pipeline 30

The master/worker model Program Resources Workers task. X Files Database s Master task. Y Input (Stream) main ( ) task. Z Disks Special Devices 31

The peer model Program Input Resources Workers task. X Files Database s task. Y task. Z Disks Special Devices 32

A thread pipeline Program Filter Threads Stage 1 Stage 2 Stage 3 Input (Stream) Resources Files Database s Disks Special Devices 33
- Slides: 33