CMSC 202 Threads.

Slides:



Advertisements
Similar presentations
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
Advertisements

Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
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.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
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.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
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.
1 Web Based Programming Section 8 James King 12 August 2003.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
CMSC 330: Organization of Programming Languages Threads.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
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.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
Java Thread Programming
A brief intro to: Parallelism, Threads, and Concurrency
Doing Several Things at Once
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
PA1 Discussion.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Java Programming Language
Multithreaded Programming in Java
Chapter 19 Java Never Ends
SE /11/2018 If you think the internet is not working in its current incarnation, you can’t change the system through think-pieces and F.C.C. regulations.
Multithreaded Programming in Java
Multithreading.
Java Based Techhnology
Multithreading.
Threads and Concurrency
21 Threads.
9. Threads SE2811 Software Component Design
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
9. Threads SE2811 Software Component Design
Gentle Introduction to Threads
Lecture 19 Threads CSE /6/2019.
9. Threads SE2811 Software Component Design
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Threads and concurrency / Safety
Presentation transcript:

CMSC 202 Threads

Principle of Multitasking: Processes and Threads Core concept: user wants to have multiple, simultaneous flows-of-control in their computation: “do several things at once” If they are independent programs, we call the units “processes” If they are execution streams within a shared application context, they are called “threads” (sometimes referred to as “lightweight processes”) Originally, concurrency of processes was simulated: “context-switched” Modern systems actually support true parallelism of processes and/or threads in hardware Fall 2010 2

Why Threads are Useful There are several situations in which multi-threading is a useful model: User prefers to launch multiple tasks simultaneously at outset, instead of sequentially with waits …or: Some tasks “block” (like I/O)—would like to continue other, independent computations in the meantime Parallel, coordinated tasks is a more intuitive model to implement …or… Fall 2010 3

Threads: Simple vs. Complex In simplest form, threads are easy: Start of multiple, independent tasks; wait for all to finish Trying to coordinate tasks can be very complex: Need communication/coordination constructs Need to control concurrent access to shared resources Most of these issues exist even if multitasking is only simulated! Fall 2010

Example Complication: Race Conditions Thread 1: Thread 2: // x == 1 at start y = x + 1; x = y; y = x + 1; x = y; // What are results? Fall 2010

Example Complication: Race Conditions Thread 1: Thread 2: // x == 1 at start y = x + 1; x = y; y = x + 1; x = y; // x == 3 now Fall 2010

Example Complication: Race Conditions Thread 1: Thread 2: // x == 1 at start y = x + 1; x = y; y = x + 1; x = y; // But x == 2 now! Fall 2010

Thread Applications: GUIs Need widgets to be independently responsive even though your application’s flow-of-control continues You might want to be responsive to widget events even through a long computation By default, Swing event handling is single-stream, but you might want it to be parallel-processing Fall 2010

Java is Inherently Threaded Even for a simple application with one class and a simple main(), using threads: main() is invoked from a foreground—or user—thread Garbage collection is implemented as a background—or “daemon”—thread (Bonus question: do you know what the difference between a “daemon” and “demon” is?) Fall 2010

Creating Your Own Threads— Method A A simple way to create a thread is: Create an instance of a Runnable type object Runnable is an interface with one method: public void run(); Instantiate a Thread class, passing your Runnable instance to the constructor Invoke the new Thread instance’s start() method, which will do some setup, then invoke your Runnable’s run() method Fall 2010

Creating Your Own Threads— Method A public class MyRunnable implements Runnable { public void run() { for (int i = 0; i < 1000000; i++) { long j = i * i; } System.out.println(“Done with thread!"); } public static void main(String args[]) { Runnable task = new MyRunnable(); Thread otherThread = new Thread(task); otherThread.start(); // Following will likely be output before above System.out.println(“Main thread here”); } } Fall 2010

Creating Your Own Threads— Method B The other way to create a thread is: Extend the Thread class, overriding the run() method (let’s call the new class MyThread) Instantiate your new Mythread class, calling the no-arg constructor Invoke the new MyThread instance’s start() method, which will do some setup, then invoke its overriding run() method Fall 2010

Creating Your Own Threads— Method B public class MyThread extends Thread { public void run() { for (int i = 0; i < 1000000; i++) { long j = i * i; } System.out.println(“Done with thread!"); } public static void main(String args[]) { Thread otherThread = new MyThread(); otherThread.start(); // Following will likely be output before above System.out.println(“Main thread here”); } } Fall 2010

Threads: Advanced Topics There are many additional facets to threaded programming, which we cannot cover here: Scheduling: Pre-emptive scheduling, priorities Memory: Race conditions, and thread-safe code Synchronization: Locks, deadlocks Fall 2010