OOP&M - theory lectures1 OOP&M – and there it was an eighth day “I f you know what I know, what am I doing here? ” - Aristoteles -

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

Threads, SMP, and Microkernels
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
What is Concurrent Programming? Maram Bani Younes.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Your First Java Application Chapter 2. 2 Program Concepts Modern object-oriented programs help us build models to manage the complexity found in a problem.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Threads G.Anuradha (Reference : William Stallings)
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
1 OS Review Processes and Threads Chi Zhang
Lecture 20 Threads Richard Gesick. Threads Makes use of multiple processors/cores if available Share memory within the program (as opposed to processes.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
UNIX U.Y: 1435/1436 H Operating System Concept. What is an Operating System?  The operating system (OS) is the program which starts up when you turn.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Chapter 4 – Thread Concepts
Processes and threads.
Concurrency, Processes and Threads
Chapter 4 – Thread Concepts
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Multithreaded Programming
Lecture 21 Concurrency Introduction
Java programming lecture one
Operating System Concepts
Operating Systems (CS 340 D)
Your First Java Application
Multithreading.
What is Concurrent Programming?
Threads Chapter 4.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Multithreaded Programming
What is Concurrent Programming?
Operating Systems (CS 340 D)
CS510 Operating System Foundations
Representation and Management of Data on the Internet
Process Management -Compiled for CSIT
Concurrency, Processes and Threads
Chapter 4: Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

OOP&M - theory lectures1 OOP&M – and there it was an eighth day “I f you know what I know, what am I doing here? ” - Aristoteles -

OOP&M - theory lectures2 OOP&M – Threads Some theory Here we are going first to introduce a couple of basic concepts: mono- and multi- processor and tasking computer These two sets of prefix and roots generate a final set of four possible combinations of computer architectures that need to be explained in order to understand what a thread is: monoprocessor multiprocessor monotasking multitasking

OOP&M - theory lectures3 OOP&M – Threads Monoprocessor The typical desktop/laptop/handheld computer that we are used to work with has got only one CPU CPU: Central Process Unit or PROCESSOR These processors can execute one or more sequences of code at a time. Each one of the operations that the processor does for the operating system in our computer is called a process Process: Set of operations done in a computer for accomplishing some goal

OOP&M - theory lectures4 OOP&M – Threads Multiprocessor Computers with more than one CPU are called multiprocessor computers. If we have structures of several computers connected together but with only one user interface, we will consider it as a multiprocessor architecture This computers can execute one or more processes at a time: different processors can attack the different processes

OOP&M - theory lectures5 OOP&M – Threads Monotasking A TASK is nothing but a sequence of processes that should be executed by the computer TASKs are e.g. the execution of a program like the Antivirus-software, a MSOffice session, a window with a game... A monotasking computer is that one that is able of executing only one of those TASKs at a time on the operating system... this means that the rest of the TASK will be frozen until the TASK under execution finishes its work

OOP&M - theory lectures6 OOP&M – Threads Multitasking A multitasking computer is that one that is able of executing more than one TASKs at a time on the operating system... The policy of the use of the resources by each TASK is defined by the O.S. E.g. if you open RealPlayer it blocks the use of the soundcard to other programs: we say that RealPlayer allocates the computer´s resources BUT NOT THE PROCESSOR Modern O.S. like the latest versions of windows implement multitasking operation in our computers

OOP&M - theory lectures7 OOP&M – Threads Examples of operation Monoprocessor Monotasking Computer Monoprocessor Multitasking Computer Multiprocessor Monotasking Computer Multiprocessor Multitasking Computer (different cases) CPU TIME TASK TIME CPU TIME TASK 1TASK 2 CPU 1 TASK TIME CPU 2 CPU 1CPU 2CPU 1CPU 2 TASK 1TASK 2TASK 1T2TASK 2T1

OOP&M - theory lectures8 OOP&M – Threads Simultaneity and Concurrency There are another couple of concepts that we need to understand before we can enter to the theory of Threads Simultaneity: literally consists in carrying out instructions from different codes (or programs) at exactly the same time Concurrency: the execution of two or more processes overlapped in time Both definitions look almost the same, but the next slide will demonstrate that it is not like that

OOP&M - theory lectures9 OOP&M – Threads Simultaneity and Concurrency (2) If you analyze the definition of Simultaneity you will find a hole in the relationship of it to our reality: in order to execute two sets of instructions exactly at the same time you need two processors working with both input of data in parallel The Java Virtual Machine emulates this mode of working when we are working in a desktop computer by parting the code of each one of the tasks into smaller parts and then executing some parts of one task, afterwards some commands from another task and so on This way of working is what we call Concurrency: the emulation of simultaneity

OOP&M - theory lectures10 OOP&M – Threads Simultaneity and Concurrency (3) If we have multiprocessor architecture-based computers, the Java Virtual Machine will be responsible for the way in how the tasks will be parted and executed The execution of Java programs is depending on the architecture of the system where we are working With more processors our concurrency will be more and more close to simultaneity

OOP&M - theory lectures11 OOP&M – Threads Thread - a definition A THREAD is the process of carrying out a set of instructions one at a time. In a more practical way it could look like: INSTRUCTION 1 :: String winner = “Lisa”; INSTRUCTION 2 :: System.out.println(“And the winner is ” + winner); INSTRUCTION 3 :: String loser = “Bjorn”; INSTRUCTION 4 :: System.out.println(“But ” + loser + “ loses this time”); So far we have been doing programs that just look like that. They are composed by only one sequence of instructions that are executed sequentially: they are composed by only one THREAD time

OOP&M - theory lectures12 OOP&M – Threads Thread - a definition (2) Some programs need more than only one set of lines of code being executed at a time... image, e.g. the window of an internet browser: Scrollbar is controlled by a thread Independently if the browser is downloading a site, if we write a new address here, the process restart --> there is another thread controlling this Who controls the buttons of the window? A thread! THERE ARE MORE

OOP&M - theory lectures13 OOP&M – Threads Thread - a definition (3) In the example, there are many processes working at the same time in the browser window: the URL line, it is waiting for the user to type a new address to look for the links on the window, which are active and can be called even if the browser is looking for information (only in some browsers) the controls of the window: scrollbar, menus, buttons... Each one of them is controlled by a different thread

OOP&M - theory lectures14 OOP&M – Threads Thread - a definition (4) How can we compare the threads to the definitions that we saw at the beginning of the lecture (mono- multi- processor/tasking)? The Java Virtual Machine can be installed almost on every architecture and running on many different operating systems This makes threads NOT comparable to the structures that we saw before. This means that threads look almost like tasks, but they are not the same... One task could be composed of several threads (one program has got different processes running at a time). Even if we cannot compare both concepts directly, threads and alike from a conceptual point of view

OOP&M - theory lectures15 OOP&M – Threads Thread - the class Java provides a class, called Thread, that models threads and allows us to build applications and applets in which more than only one of those work Using this class, we can create Thread objects that are associated to a particular piece of code that we want to execute simultaneously with other threads In the browser example, we would have a thread that executed the code for opening a connection and download a new site to show in parallel to window controls, buttons, a.s.o.

OOP&M - theory lectures16 OOP&M – Threads Thread - the class (2) The availability of Thread s considerably expands the opportunities available to the programmer. Without them, the program just carries one task at the time The Thread class provides several methods that allow Thread objects to be controlled: *.start() - starts the execution of the Thread. The Thread can exist from before, but it won´t do anything until this method is invoked *.suspend() - ask the Thread to suspend the execution until *.resume() is called *.resume() - continues the execution of a suspended Thread *.stop() - asks the Thread to permanently cease the execution of its code. Once stopped, is cannot be restarted again!!

OOP&M - theory lectures17 OOP&M – Threads Thread - the class (3) The declaration of a Thread passes by the creation of a new class that extends the Thread library of the Java package: class myOwnThread extends Thread { rest of the definition } It then requires a method called run, which is the equivalent to the main method in a normal class public void run()

OOP&M - theory lectures18 OOP&M – Threads Thread - the class (4) An example: class myOwnThread extends Thread { public myOwnThread() {} public void run() { int i = 0; while (i<10) { System.out.println(“repetition number ”+ i); i++; } What is this code doing?

OOP&M - theory lectures19 OOP&M – Threads Thread - the class (5) The code was NOT doing anything as far as nobody is calling to it, we need a program that calls to it: class myThreadCaller { public static void main() { myOwnThread t1 = new myOwnThread(); t1.start(); } You can say that this code is not doing anything special, but there is a big difference if you compare it to other programs, and it is that it can be executed simultaneously with other code

OOP&M - theory lectures20 OOP&M – Threads Thread - the class (6) The result looks like: repetition number 0 repetition number 1 repetition number 2 repetition number 3 repetition number 4 repetition number 5 repetition number 6 repetition number 7 repetition number 8 repetition number 9

OOP&M - theory lectures21 OOP&M – Threads Thread - the class (7) Another example: class myOwnThread extends Thread { public myOwnThread(String ID) { this.ID = ID; } public void run() { int i = 0; while (i<10) { System.out.println(ID +“ rep. number ”+ i); i++; } private String ID; }

OOP&M - theory lectures22 OOP&M – Threads Thread - the class (8) Now we can call two different Threads and we can give different names to each one of those, just to see the results: class myThreadCaller { public static void main() { myOwnThread t1 = new myOwnThread(“A”); myOwnThread t2 = new myOwnThread(“B”); t1.start(); t2.start(); }

OOP&M - theory lectures23 OOP&M – Threads Thread - the class (9) The most probable result looks like (depends on the JVM): A repetition number 0 A repetition number 1 A repetition number 2 A repetition number 3 A repetition number 4 A repetition number 5 A repetition number 6 A repetition number 7 A repetition number 8 A repetition number 9 B repetition number 0 B repetition number 1 B repetition number 2... B repetition number 8 B repetition number 9

OOP&M - theory lectures24 OOP&M – Threads Thread - the class (10) In order to see how the JVM separates the processes we have to introduce the factor time: class myOwnThread extends Thread { public myOwnThread(String ID) { this.ID = ID; } public void run() { int i = 0; while (i<10) { System.out.println(ID +“repetition number ”+ i); i++; try { sleep(1); } catch (Exception e) {} } private String ID; }

OOP&M - theory lectures25 OOP&M – Threads Thread - the class (11) Now we can call two different Threads and we can give different names to each one of those, just to see the results: class myThreadCaller { public static void main() { myOwnThread t1 = new myOwnThread(“A”); myOwnThread t2 = new myOwnThread(“B”); t1.start(); t2.start(); }

OOP&M - theory lectures26 OOP&M – Threads Thread - the class (12) The most probable result looks like (depends on the JVM): A repetition number 0 B repetition number 0 A repetition number 1 B repetition number 1 A repetition number 2 B repetition number 2 A repetition number 3 B repetition number 3 A repetition number 4... A repetition number 8 B repetition number 8 A repetition number 9 B repetition number 9