Distributed Systems. Βάνα Καλογεράκη Τµήµα Πληροφορικής Οικονοµικό Πανεπιστήµιο Αθηνών. Lecture 3: Threads (Νήµατα) Distributed Systems

Σχετικά έγγραφα
Νήµαταστην Java. Συγχρονισµός νηµάτων Επικοινωνία νηµάτων Εκτελέσιµα αντικείµενα Νήµατα δαίµονες Οµάδες νηµάτων. Κατανεµηµένα Συστήµατα 11-1

Ορισµός Νήµα (thread) είναι µια ακολουθιακή ροή ελέγχου (δηλ. κάτι που έχει αρχή, ακολουθία εντολών και τέλος) σ ένα

Περιγραφή και Έλεγχος ιεργασιών

Instruction Execution Times

Το παρακάτω πρόγραμμα ορίζει δυο κλάσεις την SimpleThread και την TwoThreadsTest:

Οντοκεντρικός Προγραμματισμός

Κατανεμημένα Συστήματα: Θεωρία και Προγραμματισμός. Ενότητα # 8: Ταυτοχρονισμός και νήματα Διδάσκων: Γεώργιος Ξυλωμένος Τμήμα: Πληροφορικής

Block Ciphers Modes. Ramki Thurimella

Κινητά και Διάχυτα Συστήματα. Ενότητα # 3: Νήματα και ταυτοχρονισμός Διδάσκων: Γεώργιος Ξυλωμένος Τμήμα: Πληροφορικής

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Προσομοίωση BP με το Bizagi Modeler

Εισαγωγικά & Βασικές Έννοιες

(C) 2010 Pearson Education, Inc. All rights reserved.

Εισαγωγικά & Βασικές Έννοιες

Βασικές Έννοιες Πρ Ταο υγρ τόα χ μ ρ μ ο α ν τισμ ος Π ό ρ ςο ΙΙΙ γραμματισμός 1

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Στο εστιατόριο «ToDokimasesPrinToBgaleisStonKosmo?» έξω από τους δακτυλίους του Κρόνου, οι παραγγελίες γίνονται ηλεκτρονικά.

The Simply Typed Lambda Calculus

Διεργασίες (μοντέλο μνήμης & εκτέλεσης) Προγραμματισμός II 1

Φροντιστήριο Α' Κατανεμημένα Συστήματα Επιμέλεια: Μπούτσης Ιωάννης

Test Data Management in Practice

Διεργασίες και Νήματα (2/2)

Διάλεξη Εισαγωγή στη Java, Μέρος Γ

Προγραμματισμός Ταυτόχρονος Προγραμματισμός

The challenges of non-stable predicates

2 Composition. Invertible Mappings

Προγραμματισμός ΙΙ (Java) 10. Πολυνηματικές εφαρμογές

EE512: Error Control Coding

Ειδικά Θέματα Προγραμματισμού

Νήµατα. ιεργασίες και νήµατα Υλοποίηση νηµάτων Ελαφριές διεργασίες Αξιοποίηση νηµάτων. Κατανεµηµένα Συστήµατα 10-1

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

Επιτεύγµατα των Λ.Σ.

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 5: Component Adaptation Environment (COPE)

CYTA Cloud Server Set Up Instructions

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

Λειτουργικά Συστήματα. Εισαγωγή

Fractional Colorings and Zykov Products of graphs

Bizagi Modeler: Συνοπτικός Οδηγός

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Κεφάλαιο 1ο Πολυπρογραμματισμός Πολυδιεργασία Κατηγορίες Λειτουργικών Συστημάτων

Λειτουργικά Συστήματα (Λ/Σ)

Τ.Ε.Ι. Μεσολογγίου, Τµήµα τηλεπικοινωνιακών Συστημάτων & Δικτύων

HY150a Φροντιστήριο 3 24/11/2017

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

Modbus basic setup notes for IO-Link AL1xxx Master Block

Εισαγωγή στην Java. Module 9: Threads. Prepared by Chris Panayiotou for EPL /03/2004

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

Section 8.3 Trigonometric Equations

[1] P Q. Fig. 3.1

Η Υλοποίηση της Επικοινωνίας. Κατανεµηµένα Συστήµατα

NIKOΛΑΟΣ ΝΤΙΡΛΗΣ 5ο ΦΡΟΝΤΙΣΤΗΡΙΟ ΑΙΘΟΥΣΑ Β4

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Démographie spatiale/spatial Demography

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

C.S. 430 Assignment 6, Sample Solutions

Προγραµµατισµός ΙΙ. Ηγλώσσααντικειµενοστραφούς. ιδάσκων ηµήτριος Κατσαρός, Τµ. Μηχανικών Η/Υ, Τηλεπικοινωνιών & ικτύων Πανεπιστήµιο Θεσσαλίας

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

Assalamu `alaikum wr. wb.

Approximation of distance between locations on earth given by latitude and longitude

Reminders: linear functions

Καρακασίδης Αλέξανδρος Καστίδου Γεωργία Παπαφώτη Μαρία Πέτσιος Κων/νος Στέφανος Σαλτέας Καλογεράς Παναγιώτης. Threads in Java ΝΗΜΑΤΑ ΣΤΗ JAVA

TMA4115 Matematikk 3

Example Sheet 3 Solutions

Advanced Subsidiary Unit 1: Understanding and Written Response

Models for Probabilistic Programs with an Adversary

the total number of electrons passing through the lamp.

Main source: "Discrete-time systems and computer control" by Α. ΣΚΟΔΡΑΣ ΨΗΦΙΑΚΟΣ ΕΛΕΓΧΟΣ ΔΙΑΛΕΞΗ 4 ΔΙΑΦΑΝΕΙΑ 1

Matrices and Determinants

ΤΕΧΝΙΚΕΣ ΑΥΞΗΣΗΣ ΤΗΣ ΑΠΟΔΟΣΗΣ ΤΩΝ ΥΠΟΛΟΓΙΣΤΩΝ I

ιεργασίες και νήµατα Προγραµµατισµός ΙΙΙ 1 lalis@inf.uth.gr

Abstract Storage Devices

Λειτουργικά Συστήματα

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

Capacitors - Capacitance, Charge and Potential Difference

άσκηση Hide UI Elements 7.2 Try and Catch

ΤΕΧΝΟΛΟΓΙΚΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ ΤΜΗΜΑ ΝΟΣΗΛΕΥΤΙΚΗΣ

VBA ΣΤΟ WORD. 1. Συχνά, όταν ήθελα να δώσω ένα φυλλάδιο εργασίας με ασκήσεις στους μαθητές έκανα το εξής: Version ΗΜΙΤΕΛΗΣ!!!!

Κατανεμημένα Συστήματα. Javascript LCR example

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Ε-85: Ειδικά Θέµατα Λογισµικού

Ντίρλης Νικόλαος- ΕΤΥ 3ο ΦΡΟΝΤΙΣΤΗΡΙΟ ΠΑΡΑΣΚΕΥΗ 25 ΟΚΤΩΒΡΙΟΥ 2013 ΑΙΘΟΥΣΑ Β4

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ & ΠΛΗΡΟΦΟΡΙΚΗΣ. του Γεράσιμου Τουλιάτου ΑΜ: 697

PARTIAL NOTES for 6.1 Trigonometric Identities

Homework 8 Model Solution Section

ΕΙΣΑΓΩΓΗ ΣΤΟN ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ ΚΑΙ ΠΛΗΡΟΦΟΡΙΚΗΣ

Math 6 SL Probability Distributions Practice Test Mark Scheme

Αρχές Τεχνολογίας Λογισμικού Εργαστήριο

Special edition of the Technical Chamber of Greece on Video Conference Services on the Internet, 2000 NUTWBCAM

Προσωπική Aνάπτυξη. Ενότητα 4: Συνεργασία. Juan Carlos Martínez Director of Projects Development Department

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

Δίκτυα Επικοινωνιών ΙΙ: OSPF Configuration

Λειτουργικά Συστήματα Η/Υ

Δίκτυα Επικοινωνιών ΙΙ: Network Programming UDP Sockets, Signals

ΕΠΛ221: Οργάνωση Υπολογιστών και Συμβολικός Προγραμματισμός. Εργαστήριο Αρ. 2

ΑΚΑ ΗΜΙΑ ΕΜΠΟΡΙΚΟΥ ΝΑΥΤΙΚΟΥ ΜΑΚΕ ΟΝΙΑΣ ΣΧΟΛΗ ΜΗΧΑΝΙΚΩΝ ΠΤΥΧΙΑΚΗ ΕΡΓΑΣΙΑ

Διεργασίες (μοντέλο μνήμης & εκτέλεσης) Προγραμματισμός II 1

Other Test Constructions: Likelihood Ratio & Bayes Tests

Partial Differential Equations in Biology The boundary element method. March 26, 2013

Section 1: Listening and responding. Presenter: Niki Farfara MGTAV VCE Seminar 7 August 2016

Transcript:

Βάνα Καλογεράκη Τµήµα Πληροφορικής Οικονοµικό Πανεπιστήµιο Αθηνών 1 Lecture 3: Threads (Νήµατα)

The Case for Cooperating Processes Independent process: cannot affect or be affected by the execution of another process Cooperating process: can affect or be affected by the execution of another process What are the reasons for process cooperation? Information sharing (e.g., interested in the same file) Computation speed-up (break a big task in smaller sub-tasks) Modularity (e.g., divide the system functions into separate processes or threads) Convenience (many concurrent tasks e.g., editing, printing) 2

Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions without sharing the same address space Message-Passing System: processes communicate with each other without the need to resort to shared variables 3

Interprocess Communication (cont.) IPC facility provides two operations: send(message) message size is fixed or variable receive(message) If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive operations 4

Direct Communication Processes that want to communicate must name each other explicitly Message Primitives: send (P, message) send a message to process P receive (Q, message) receive a message from process Q Properties Processes must name each other explicitly must know each other s id One link between each pair of communicating processes The link may be unidirectional, but is usually bi-directional 5

Indirect Communication Messages are directed and received from mailboxes Each mailbox has a unique id Processes can communicate only if they share a mailbox Properties Link established only if processes share a common mailbox A link may be associated with many processes; a pair of processes may share several communication links. Link may be unidirectional or bi-directional. 6

Interprocess Communication Mechanisms Pipes Signals Message queues Semaphores Shared memory Sockets 7

Threads Overview Thread States Processes and Threads Multithreading Models Threads in Java Today s Outline 8

Multithreading vs. Single threading Multithreading: when the OS supports multiple threads of execution within a single process The basic idea is that if a process has multiple threads of control, it can do more than one tasks at a time Single threading: when the OS does not recognize the concept of a thread 9

Processes (Διεργασίες) A process is a program in execution A process consists of: An executable program Associated data (variables, buffers,..) Process state (processor registers) A process needs resources (CPU time, memory, files) to accomplish a task Allocated when a process is created or.. While the process is executing Address space: set of all objects (usually in memory) accessible by the process 10

Process Control Block (PCB) Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information PCB 11

What is a Thread (Νήµα)? Is the basic unit of CPU utilization Also known as lightweight process (LWP) Αποτελείται από: χωριστή στοίβα (stack), καταχωρητές (register set), µετρητής προγράµµατος (program counter ), thread id Μοιράζεται µε άλλα threads στην ίδια διεργασία: κώδικα (code section), δεδοµένα (data section,) αρχεία (open files), signals 12

Single and Multithreaded Processes 13

Διεργασίες και Νήµατα Each process has Virtual address space that holds the process image Access to resources (files, I/O resources..) and other processes (for interprocess communication) Within a process, there may be one or more threads, each with the following An execution state (running, ready, etc.) An execution stack for each thread Some per-thread static storage for local variables Access to memory and resources of its process, shared with all threads in that process 14

Ερωτήσεις Εάν ένα thread µεταβάλει την τιµή µια µεταβλητής στη µνήµη, µπορούν τα άλλα threads να δουν το αποτέλεσµα αυτής της µεταβολής όταν διαβάσουν τη µεταβλητή? Εάν ένα thread ανοίξει ένα αρχείο για διάβασµα (read privileges), µπορούν τα άλλα νήµατα της ίδιας διεργασίας να διαβάσουν από το ίδιο αρχείο? 15

Multithreaded Applications Παραδείγµατα: Web servers, word processors, P2P applications Αξιοποίηση στους πελάτες Απόκρυψη καθυστερήσεων του δικτύου Παράδειγµα: ανάγνωση ιστοσελίδας (webpage) Εµφάνιση κειµένου προς ανάγνωση Πολλά νήµατα µεταφοράς εικόνων Αξιοποίηση στους εξυπηρετητές Διαχείριση πολλών πελατών Αξιοποίηση πολλών επεξεργαστών Παράδειγµα: εξυπηρετητής αρχείων Πολλά νήµατα για εξυπηρέτηση αιτήσεων Ένα νήµα για διανοµή αιτήσεων Κάθε νήµα λειτουργεί και εµποδίζεται αυτόνοµα 16

How Multithreading works in a Web Server Application? Web client A Assume that you have N Web clients. How many threads should the Web Server create? Web server Database Web client B Μοντέλο διανοµέα/εργατών: εξυπηρετητής ιστοσελίδων Όλα τα νήµατα-εργάτες είναι παρόµοια Εξυπηρέτηση αιτήσεων από οποιοδήποτε νήµα 17

Thread States Three thread states: running, ready, blocked No suspend state because all threads within the same process share the same address space Process termination: terminates all threads within the process 18

Thread Descriptor Data structure where the OS keeps all information it needs to manage a thread State Thread s current state (running, ready, blocked) Execution statistics Time accumulation, start time,.. Process List of related threads Stack Other resources A reference to the process descriptor of the thread s associated process A reference to a list of parent/child/sibling threads The location of the base thread s stack in main memory Reference to the thread-specific resources 19

Πλεονεκτήµατα Παράλληλη εκτέλεση πολλών βηµάτων Χωριστά νήµατα για κλήσεις εισόδου/εξόδου Ταυτόχρονη επικοινωνία µε χρήστη και επεξεργασία Ένα thread µπορεί να εξακολουθεί να εκτελείται ακόµα κι αν µέρος της διεργασίας είναι µπλοκαρισµένη (blocked) ή εκτελεί µακρόχρονη λειτουργία Αξιοποίηση εγγενούς παραλληλισµού εφαρµογής Χρήση όλων των διαθέσιµων επεξεργαστών (πχ. παράλληλη αρχιτεκτονική) Γρήγορη επικοινωνία µέσω κοινής µνήµης 20

Πλεονεκτήµατα (συνέχεια) Επίδοση: χρειάζεται λιγότερος χρόνος να δηµιουργήσουµε ή να καταστρέψουµε threads (πχ. σε σύγκριση µε τις processes) Takes less time to create a new thread in an existing process than to create a new process Takes less time to terminate a thread than a process Takes less time to switch between two threads in the same process Because threads within the same process share memory and files, they can communicate with each other without invoking the kernel 21

Thread Operation Latencies Operation User-Level Threads Kernel-Level Threads Processes Null Fork 34 948 11,300 Signal Wait 37 441 1,840 22

Any Challenges? Synchronization between threads that share the same resources Concurrent execution may be difficult 23

Example of an inconsistent view using threads 2 shared variables: A, B Thread T1 and T2 T1 transfers amount X from A to B T1 must do: A=A-X and B=B+X A+B is unchanged T2 computes C=A+B Schedule 1: Schedule 2: Schedule 3: T1: A=A-X T1: A=A-X T2: C=A+B T2: C=A+B T1: B=B+X T1: A=A-X T1: B=B+X T2: C=A+B T1: B=B+X 24

Thread Categories User-level threads Implemented by a thread library at the user level (e.g., POSIX Pthreads, Solaris 2 UI-threads) All thread creation and scheduling done in user space without the need for kernel intervention Kernel-level threads Supported by the OS (e.g., Windows NT/2000, Solaris 2..) Thread creation, scheduling and management done in kernel space Usually slower to create and manage than user threads 25

Questions What happens if the kernel is single-threaded and a user-level thread performs a blocking system call? What happens if a kernel-level thread performs a blocking system call? 26

Many-to-One Multithreading Model Many user-level threads mapped to a single kernel thread Used on systems that do not support kernel threads The entire process blocks if a thread makes a blocking system call 27

One-to-One Multithreading Model 28 Each user-level thread maps to a kernel thread Multiple threads run in parallel on multiprocessors despite blocking system calls Examples Windows NT/2000 OS/2

Many-to-Many Multithreading Model 29 Many user-level threads mapped to a smaller or equal number of kernel threads (not 1-1 mapping necessary) Examples Solaris 2, HP-UX, IRIX

Advantages and disadvantages of using userlevel threads Advantages Thread switching does not involve the kernel Scheduling can be application specific: choose the best algorithm User-level threads can run on any OS. Need to provide a thread library (e.g., Posix threads package, Java threads package) Disadvantages If system calls are blocking then the kernel blocks processes. So all threads within the process will be blocked The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors with user-level threads 30

Advantages and disadvantages of using kernel-level threads Advantages The kernel can simultaneously schedule many threads of the same process on many processors Blocking is done on a thread level Kernel itself and its internal routines can be multithreaded Disadvantages Thread switching within the same process involves the kernel Results in poor performance 31

Νήµατα στην Java Κάθε εφαρµογή ξεκινά µε ένα νήµα Εκτελεί τη µέθοδο main της εφαρµογής Το νήµα αυτό µπορεί να δηµιουργήσει άλλα Διάφοροι τρόποι διακοπής εκτέλεσης Αναµονή εισόδου / εξόδου Αναµονή χρονοµέτρου (sleep) Αναµονή γεγονότος (wait) Προτεραιότητες νηµάτων Κάθε νήµα έχει προτεραιότητα 1 (χαµηλή) έως 10 (υψηλή) Η προτεραιότητα κληρονοµείται από τον γονέα Χρονοπρογραµµατισµός ανάλογα µε την προτεραιότητα Διακοπτόµενος ή µη διακοπτόµενος χρονοπρογραµµατισµός 32

Νήµατα στην Java Νήµατα: υποστηρίζονται µέσω της τάξης Thread Thread (String όνοµα_νήµατος) Κατασκευάζει ένα νήµα µε συγκεκριµένο όνοµα Thread () Κατασκευάζει ένα νήµα µε όνοµα "Thread-#" void start() Ξεκινά την εκτέλεση ενός νέου νήµατος (µέθοδος run()) Τα δύο νήµατα εκτελούνται παράλληλα void run () Η µέθοδος που περιέχει τον κώδικα του νήµατος Κάθε thread ορίζει τη δική της run() void setname (String όνοµα) / String getname () Θέτει/επιστρέφει το όνοµα του νήµατος 33

Νήµατα στην Java 34 static Thread currentthread () Επιστρέφει µία αναφορά προς το εκτελούµενο νήµα void sleep (int διάστηµα) Αποκοιµίζει το νήµα για διάστηµα msec void yield () Παραχωρεί τον επεξεργαστή σε άλλα νήµατα void interrupt () Στέλνει στο νήµα ένα σήµα διακοπής σ ένα νήµα Αν το νήµα είναι σε κοιµισµένο ή σε αναµονή ξυπνάει Αλλιώς το νήµα πρέπει να ελέγχει ρητά αν έχει λάβει διακοπές boolean isinterrupted () Επιστρέφει true αν το νήµα έχει διακοπεί

Νήµατα στην Java 35 boolean isalive () Επιστρέφει true αν το νήµα εκτελείται void setpriority (int προτεραιότητα) / int getpriority () Θέτει/επιστρέφει την προτεραιότητα του νήµατος void join (long διάστηµα) Περιµένει τον τερµατισµό του νήµατος για διάστηµα msec Αν το διάστηµα είναι 0, αναµονή για πάντα void join (): Ισοδύναµη µε join(0) void checkaccess () Εξετάζει αν έχουµε πρόσβαση στο νήµα Εξαίρεση αν δεν έχουµε, επιστροφή αν έχουµε

Νήµατα στην Java 36 ThreadGroup getthreadgroup () Επιστρέφει την οµάδα του νήµατος int activecount () Επιστρέφει το πλήθος ενεργών νηµάτων της οµάδας int enumerate (Thread[] πίνακας) Αντιγράφει κάθε ενεργό νήµα της οµάδας στον πίνακα Αν ο πίνακας είναι µικρός, δεν αποθηκεύονται όλα String tostring () Επιστρέφει όνοµα, προτεραιότητα και οµάδα του νήµατος void dumpstack () Εµφανίζει τη στοίβα κλήσεων των µεθόδων του νήµατος Χρήσιµη για εκσφαλµάτωση εφαρµογών

A Simple Example The TwoThreadsTest class provides a main() method that creates two SimpleThread threads: one is named "Jamaica" and the other is named "Fiji The main() method also starts each thread immediately following its construction by calling the start() method 37 class TwoThreadsTest { public static void main (String args[]) { } } new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start();

A Simple Example 38 class SimpleThread extends Thread { // Thread class provided by the java.lang package public SimpleThread(String str) { // constructor that takes as argument the name of thread super(str); } public void run() { // the heart of any thread for (int i = 0; i < 10; i++) { //in each iteration it prints iteration number + thread name System.out.println(i + " " + getname()); try { sleep((int)(math.random() * 1000)); // sleeps between 0..1 second } catch (InterruptedException e) {} } } } System.out.println("DONE! " + getname()); // when it finishes it prints DONE

A Simple Example 39 0 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2 Fiji 3 Fiji 3 Jamaica 4 Jamaica Notice that the output from each thread is intermingled with the output from the other. This is due to the fact that both SimpleThread threads are running concurrently thus both run() methods are running at the same time and each thread is displaying its output at the same time as the other. 4 Fiji 5 Jamaica 5 Fiji 6 Fiji 6 Jamaica 7 Jamaica 7 Fiji 8 Fiji 9 Fiji 8 Jamaica DONE! Fiji 9 Jamaica DONE! Jamaica

Summary Today: Threads Multithreading Concepts Thread States Threads and Processes Multithreading Model Threads in Java 40