Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads.

Slides:



Advertisements
Similar presentations
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
Advertisements

Section 3. True/False Changing the order of semaphores’ operations in a program does not matter. False.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CSCC69: Operating Systems
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Classes and Objects Systems Programming.
1 Thread Pools Representation and Management of Data on the Internet.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
Server Architecture Models Operating Systems Hebrew University Spring 2004.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Fundamentals of Python: From First Programs Through Data Structures
CREATIONAL DESIGN PATTERN Object Pool. CREATIONAL DESIGN PATTERN creational design patterns are design patterns that deal with object creation mechanisms,
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
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.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Concurrency Patterns Emery Berger and Mark Corner University.
1 GUI programming with threads. 2 Threads and Swing Swing is not generally thread-safe: most methods are not synchronized –correct synchronization is.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Concurrent Programming and Threads Threads Blocking a User Interface.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Design Patterns Definition:
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Confinement.
Designing a Middleware Server for Abstract Database Connection.
Generics. Writing typed checked generic code There are many instances where the type of the object is irrelevant : –The construction of data structures.
The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
MULTIPROCESSING MODULE OF PYTHON. CPYTHON  CPython is the default, most-widely used implementation of the Python programming language.  CPython - single-threaded.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Animate objects and threads
Threaded Programming in Python
Multi Threading.
Singleton Pattern Command Pattern
Out-of-Process Components
This pointer, Dynamic memory allocation, Constructors and Destructor
Using local variable without initialization is an error.
Multithreading Chapter 23.
Cs561 Presenter: QIAOQIAO CHEN Spring 2012
Introduction to Classes and Objects
Multithreaded Programming
CSE 451 Autumn 2003 Section 3 October 16.
Chapter 5: CPU Scheduling
Out-of-Process Components
CE 221 Data Structures and Algorithms
NETWORK PROGRAMMING CNET 441
Classes and Objects Systems Programming.
Message Passing Systems
Introduction to Classes and Objects
Presentation transcript:

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 Some existing techniques An application needing to deal with reusing threads includes –Data structures –and –Methods ad-hoc basis to deal with the problem –Plus: addresses a pressing issue –Negative: No software reuse, each application dealing with it will have to come up with its own solution. Of the two apps reviewed: –Not sufficiently good.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 Threading issues to deal with Expense of thread creation, deallocation. Reuse of threads There will be an indirect major issue to deal with as well: –termination of threads being reused.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 Thread reuse Main ideas –A reusable thread can be designed as a running waiting for a job to execute. When the job is given, thread will execute it. –After execution of job, reusable thread is placed on a hold until a next job arrives.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 Abstracting Running thread’s Job We can abstract a job as an instance of Runnable. For jobs with arguments, we can abstract it as a command object wrapped in a runnable instance interface Command { void execute(); } The user passes a command instance, and the implementation of the execution of the command will do: new Runnable( public void run(){ command.execute(); } )

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 Note on Command If the command’s execute requires parameters –The parameters to the execute() are given as parameters to the Command’s constructor which set instance variables to be used by execute() If the command ought to be a query, the implementation of command specifies an instance variable which will contain the final result. The implementation will also provide a query to return such value.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 Reusable Thread’s run() The thread run() method will basically have as body public void run(){ job.run(); } The questions are: – where will “job” come from. –How to keep the thread’s run() method executing, as we cannot issue start() again on a thread that has been started.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 Jobs data structure For reusable threads, jobs will be coming from a queue of Runnable. This queue must be designed to be thread-safe. In particular: –When the queue is empty, any thread dequeing will have to be placed on wait. –If the queue has a max length, then any enqueue request will have to be placed on wait.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 Body of the run() method public run(){ while(!shutDown()){ (Runnable)jobQueue.dequeue().run(); } Note that the condition of the loop is to keep the thread running. When a thread finishes executing a job’s run() method, and the thread is not shutDown() it will proceed to dequeue a job. If there are no jobs, the thread is put on wait()

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 Job creation The client of the pool thread will proceed to request the pool to execute a job –The execute method of the pool is to add the job to the job queue. Thus as an object the pool thread will have –A constructor for initialization The main job of this one will be to start worker threads. –An execute method –A shutdown method that will terminate all those threads on wait() for jobs.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 11 Evaluation This is a more generic solution, that can be reused. On the negative side: creating a bunch of threads ahead of time, that may all not be used. Question: can we design the class so that we only create threads as needed? YES.