Java Threads. Written by John Bell for CS 342, Spring 2018

Similar documents
Contents. 6-1 Copyright (c) N. Afshartous

Java Threads. COMP 585 Noteset #2 1

CMSC 132: Object-Oriented Programming II. Threads in Java

CS 351 Design of Large Programs Threads and Concurrency

Threads Chate Patanothai

Animation Part 2: MoveableShape interface & Multithreading

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Concurrent Programming

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

7. MULTITHREDED PROGRAMMING

CS 556 Distributed Systems

What is a thread anyway?

Threads Questions Important Questions

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Introduction to Java Threads

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Unit - IV Multi-Threading

public class Shared0 { private static int x = 0, y = 0;

Concurrency in Java Prof. Stephen A. Edwards

Multithread Computing

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Multithreaded Programming

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Unit III Rupali Sherekar 2017

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

CS11 Java. Fall Lecture 7

Advanced Concepts of Programming

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

Concurrent Programming using Threads

Programming Language Concepts: Lecture 11

Techniques of Java Programming: Concurrent Programming in Java

Multithreading using Java. Dr. Ferdin Joe John Joseph

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

CMSC 330: Organization of Programming Languages

Multi-threading in Java. Jeff HUANG

Chapter 32 Multithreading and Parallel Programming

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Basics of. Multithreading in Java

Java Threads. Introduction to Java Threads

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Component-Based Software Engineering

Programming in Parallel COMP755

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

User Space Multithreading. Computer Science, University of Warwick

Handling Multithreading Approach Using Java Nikita Goel, Vijaya Laxmi, Ankur Saxena Amity University Sector-125, Noida UP India

CS180 Review. Recitation Week 15

Multithreading Pearson Education, Inc. All rights reserved.

Performance Throughput Utilization of system resources

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Shared-Memory and Multithread Programming

Robotics and Autonomous Systems

COMP346 Winter Tutorial 4 Synchronization Semaphores

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

Overview of Java Threads (Part 2)

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Java s Implementation of Concurrency, and how to use it in our applications.

Object Oriented Programming. Week 10 Part 1 Threads

Chapter 19 Multithreading

CIS233J Java Programming II. Threads


G52CON: Concepts of Concurrency

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Synchronization

Multithreaded Programming

Multi-threaded programming in Java

CMSC 330: Organization of Programming Languages

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

MCS-378 Intraterm Exam 1 Serial #:

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Introduction to Java Written by John Bell for CS 342, Spring 2018

Threads Assistant Professor DCS Operating System Concepts

UNIT V CONCURRENT PROGRAMMING

CS 159: Parallel Processing

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

UNIT IV MULTITHREADING AND GENERIC PROGRAMMING

EECS 482 Introduction to Operating Systems

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Parallel Programming Languages COMP360

Lecture 9: Introduction to Monitors

Java Programming Lecture 23

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Concurrency COMS W4115. Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science

Synchronization in Concurrent Programming. Amit Gupta

Module - 4 Multi-Threaded Programming

Implementing Coroutines. Faking Coroutines in Java

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

Concurrency, Thread. Dongkun Shin, SKKU

CSCD 330 Network Programming

By: Abhishek Khare (SVIM - INDORE M.P)

Transcription:

Java Threads Written by John Bell for CS 342, Spring 2018 Based on chapter 9 of Learning Java, Fourth Edition by Niemeyer and Leuck, and other sources. Processes A process is an instance of a running program. Programs are stored on the hard drive. Processes run in memory. If the same program is running more than one time, each instance is a separate process. Each process has a unique Process ID ( PID ), owner, blocks of allocated memory, and allocated process resources. ( It is possible, under certain circumstances, for processes to share memory and other resources, such as read-only blocks of executable code. ) The address space of each process is unique and private, regardless of what it may share with others. 1

Threads Threads are separate flows of execution, running within a single process, and sharing a common address space. Threads share a common PID and system resources, such as the open file table. Typical tasks assigned to separate threads include background file saving or spell checking, animations, event handling, keyboard processing, graphics updating, etc. Network communications are also commonly threaded. Shared Thread Access ( to private data. ) Shared access between threads is determined by scope, as well as privacy. Two threads running code in different instances of the same class will have shared access to class static data, but not instance data. Two threads running code in the same instance of a class will have access to shared instance as well as class data. Each running method has its own stack space containing separate local variables. ( Similar to recursion. ) Threads running code not within the same class will only have access to shared global data and resources, ( in languages that support global data. ) 2

Time Slicing Multiple processes appear to run concurrently through time-slicing: Rapidly switching from one process to another in very short time slices. Threads run concurrently through sharing and slicing at several levels: If multiple CPUs are present, each runs independently, and the OS schedules kernel threads on each CPU. The OS time-slices multiple kernel threads per CPU. The OS time-slices multiple user threads on each kernel thread. Thread scheduling is unknowable and largely uncontrollable from user code. Implementing Threads in Java Implementing Threads in Java requires 2 things: An instance of the Thread class ( or a descendant. ) An instance that implements the Runnable interface. 3

The Runnable Interface Defines a single method: void run( ); The run method may not take any parameters, return any values, or throw any unchecked exceptions. Calling the start( ) method of a Thread causes the system to call the run( ) method of the Runnable. ( Threads in Java are not automatically started upon creation, as opposed to C++ pthreads. ) Java Thread Class Constructors may take no arguments, or some combination of: An instance of Runnable, A String name, A ThreadGroup The Thread class implements Runnable, with a do-nothing run( ) method, which can be overridden in descendant classes. If a Thread is constructed without a separate Runnable, then it acts as its own Runnable. If a Thread is constructed with a separate Runnable, then calling run( ) on the Thread calls run( ) on the separate Runnable. 4

Sample I - Separate Classes class Animation implements Runnable { boolean animate = true; public void run() { while ( animate ) { // draw Frames... Animation happy = new Animation("Mr. Happy"); Thread mythread = new Thread( happy ); mythread.start(); Sample II - Runnable creates and starts its own thread class Animation implements Runnable { Thread mythread; Animation (String name) { mythread = new Thread( this ); mythread.start();... // run( ) method down here somewhere Alternate: Thread could be created and/or started in a separate method besides the constructor. 5

Sample III - Thread IS Runnable class Animation extends Thread { boolean animate = true; public void run() { while ( animate ) { // draw Frames... Creating an Animation creates a Runnable Thread. Controlling Thread Execution Scheduling of Threads is entirely at the whim of the OS. There is no real way to control how fast or slow different threads run, or to guarantee order of operations for activities occurring in different threads. However there is some means of control: sleep( ) causes the thread to pause for a time. wait( ) and join( ) are used to coordinate threads. interrupt( ) wakes up a thread blocked by sleep( ), wait( ), or a blocking I/O operation. Returning from run( ) is the best way to end a thread. ( Deprecated: stop( ), suspend( ), resume( ) ) 6

Sleep Example try { // The current thread Thread.sleep( 1000 ); // milliseconds, i.e. 1 sec catch ( InterruptedException e ) { // someone woke us up prematurely Thread.join( ) Causes the current thread to pause until a separate thread finishes. Thread background = new Thread ( myrunnable ); background.start( );... // Do stuff of my own for a while... background.join( ); // Pause till background finishes.... // Now use the results ( if any ) of background. join( int ) puts a limit on how long to pause. ( msec. ) 7

Synchronizing access to data One of the big problems with concurrent code is synchronizing access to shared data, so that only one thread modifies it at a time. The standard approach is to lock a data item or block of code, so that only one thread at a time can access that data or run that code. Every Object in Java has an associated lock ( monitor ), that can be used for this purpose, either through the synchronized keyword or the wait( ) and notify( ) methods of Object. The synchronized keyword A method in Java can be labelled with the keyword synchronized, guaranteeing that only one instance of the method will run at a time. synchronized int sumrow() { return cella1 + cella2 + cella3; If the method is static, a class-level lock is applied. If the method is not static, an instance-level lock is applied. Multiple synchronized methods in the same class share the same lock. Alternatively any block of code can be synchronized, by locking on any object: synchronized ( myobject ) { // Functionality that needs exclusive access to resources 8

wait( ), notify( ), and notifyall( ) There are times when synchronized code may want to voluntarily wait, and let other code have access until something occurs. wait( ) releases a lock, and sleeps until either a timer expires or it is notified. Upon reawakening, the code needs to reacquire the lock before it can proceed. notify( ) will wake up one sleeping thread that had blocked on wait( ). However it does not give up the lock. notifyall( ) wakes up all such sleeping threads. Thread State At any given time, a Thread can be in one of five defined states: NEW - Created, but not yet started. RUNNABLE - Normal running state, even when blocked on an I/O operation such as read( ). BLOCKED - Blocked waiting to enter a section of synchronized code. WAITING, TIMED_WAITING - waiting on another thread, via a call to join( ) or wait( ) TERMINATED - Completed, due to returning, an unhandled exception, or a call to (deprecated)stop( ). 9

Thread Priorities The setpriority( int ) method allows programmers to set the relative priorities of different threads. These should be considered as suggestions to the system, not hard guarantees. Due to the complexities of the underlying operating system, there is no way to rigidly enforce or guarantee any kind of ( relative ) performance when using threads. Thread Groups Threads may be assembled into groups ( and sub groups ) using ThreadGroup objects. The purpose of thread groups is to apply controls to entire groups of threads at one time. All of the methods available in Thread( ) are available in ThreadGroup( ), and are applied to all the threads in the group. 10