Overview Review – what constitutes a thread Creating threads – general Creating threads – Java What happens if synchronization is not used? Assignment.

Slides:



Advertisements
Similar presentations
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Advertisements

CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
G52CON: Concepts of Concurrency Lecture 2 Processes & Threads Chris Greenhalgh School of Computer Science
Chapter 5: Threads Overview. Multithreading Models. Threading Issues. Thread Pools. Java ThreadLocal. Pthreads. Solaris 2 Threads. Java Threads.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
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.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
CSC3170 Introduction to Database Systems
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Critical Reference An occurrence of a variable v is defined to be critical reference: a. if it is assigned to in one process and has an occurrence in another.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Parts of JAVA 1www.gowreeswar.com. Features of JAVA 2www.gowreeswar.com.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
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
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Java 3: Odds & Ends Advanced Programming Techniques.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
CMSC 330: Organization of Programming Languages Threads.
Programming Languages and Paradigms Activation Records in Java.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Object Oriented Programming Lecture 2: BallWorld.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Java Thread Programming
Design issues for Object-Oriented Languages
Multi Threading.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Java Programming Language
Java Concurrency.
Java Concurrency.
Multithreaded Programming in Java
Multithreading.
برنامه‌نویسی چندنخی Multi-Thread Programming
Java Concurrency 17-Jan-19.
Recitation 12 November 18, 2011.
Java Concurrency.
Java Concurrency.
Representation and Management of Data on the Internet
Java Concurrency 29-May-19.
Corresponds with Chapter 5
CMSC 202 Threads.
SPL – PS3 C++ Classes.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Overview Review – what constitutes a thread Creating threads – general Creating threads – Java What happens if synchronization is not used? Assignment

What constitutes a thread? Instruction pointer Stack space (how much?) Registers/Local variables Auxiliary data  Exception handling stack  Priority  Interruption status  Thread-private data  … Thread control block (TCB)

Creating Threads - general What has to be supplied to create a thread?  Always and most important: some designation for the code it is to run  Stack space  Initial state  Daemon status  Human-readable identifier?  Thread group?  Initial priority?

Creating Threads - Java Built-in Thread class Supplying the code (1st way)  Subclass Thread and override run() method class MyThread (Thread) { void run () { …this code can be run as the body of a new thread } } MyThread mt = new MyThread() At this point mt is a Thread object but no new thread of execution exists

Constructing Threads - Java Supplying the code (2 nd way)  Provide an object of a class that implements Runnable when creating a new Thread object. class MyRunnable implements Runnable { void run () { …this code can be run as the body of a new thread } } MyThread mt = new Thread(new MyRunnable()) Again, mt is just a Thread object. No new executable thread. What has to happen?

Providing Initial State Why?  Want many threads that are “almost the same” How  Some approaches allow parameters to the run method, but not Java  In Java, pass arguments to Runnable (or Thread subclass) constructor then assign to object data fields MyRunnable(int instanceNum) { this.instanceNum = instanceNum; }

Creating an executing thread mt.start()  Thread.start() method constructs new stack and tells JVM to call runnable’s run() on the new stack What happens if you just call mt.run()?

A Bad Example The badness is not related to thread creation but rather to synchronization This example is also used in the assignment

Assignment 1. Install java on the machine you’ll be using for the class (Eclipse or NetBeans IDEs, jdk or gij for command line). Which one did you install? 2. Describe the machine that you are using: number of processor cores, physical memory, 64 or 32 bit architecture. 3. What is the default stack size for threads in the Java environment that you installed? (Dive into the documentation) How can it be changed? Can it be changed per-thread? 4. What does this suggest as the maximum number of threads that will run well on your machine using the default stack size? (Consider the available physical memory and how much is used by each thread) 5. Run the code of the following Runnable in a new thread. How many stack frames does it succeed in creating before throwing StackOverflow? 6. Run the Racer example code above. Vary the number of iterations until you regularly get an error. 7. Bring your answers and a code listing for your stack overflow test in class next Monday. TC students please the answers to Shaikot before class. Bring a copy, too, as we will discuss the results.

Stacksize testing Runnable class StacksizeTest implements Runnable { int depth = 0; public void run() { try { doOverflow(); } catch (StackOverflowError e) { System.out.println(“Overflow occurred at depth ” + depth + “.”); } } void doOverflow() { depth += 1; doOverflow(); } }

Note: Java “main” program class foo { static void main(String argv[]) { … code for main program... } } % javac foo.java % java foo

Next time Thread safety – what are the rules about accessing shared variables in Java? (Chapter 2 in the Java book).