Threads Rethreaded. Execution of Synchronized Sections Thread A enters synch section 1. A checks for lock; points lock to self 2. Enters code segment.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
The Zen of Threads. 3 Types of Execution Single, lone process doing all the work No problems with multiple-access Can be inconvenient/inefficient -- blocking,
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.
Overview Review – what constitutes a thread Creating threads – general Creating threads – Java What happens if synchronization is not used? Assignment.
ThreadThread Thread Basics Thread Synchronization Animations.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
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.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Threads some important concepts Simon Lynch
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
1 Java Threads Instructor: Mainak Chaudhuri
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Threading Eriq Muhammad Adams J
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Threading and Concurrency COM379T John Murray –
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
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.
SurfaceView.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Java 3: Odds & Ends Advanced Programming Techniques.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Multithreaded Programming in Java David Meredith Aalborg University.
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
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.
Multithreading Lec 23.
Multi Threading.
Java Concurrency.
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Java Concurrency.
Multithreaded Programming in Java
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Multithreading.
More on Thread Safety CSE451 Andrew Whitaker.
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
Multithreading.
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Threads in Java James Brucker.
Multithreading in java.
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threads Rethreaded

Execution of Synchronized Sections Thread A enters synch section 1. A checks for lock; points lock to self 2. Enters code segment 3. Completes code segment 4. Releases lock (points it back to nobody) Thread B enters synch at 3 B checks for lock; can’t get it; blocks When A completes 4, B can get lock B repeats 1-4 itself

Synchronization Examples public class MyClass { private Vector _myVector=new Vector(); private static _myList=new LinkedList(); private List _mySList=new SortedList(); public synchronized void writeData(int e) { _myVector.add(new Integer(e)); _myList.add(new Integer(e)); _mySList.add(new Integer(e)); } private static void writeSData(int e) { _myList.add(new Integer(e)); } private Integer readData(e) { return (Integer)_mySList.get(new Integer(e)); }

Synchronization Examples public class MyClass { private Vector _myVector=new Vector(); private static _myList=new LinkedList(); private List _mySList=new SortedList(); public synchronized void writeData(int e) { _myVector.add(new Integer(e)); _myList.add(new Integer(e)); _mySList.add(new Integer(e)); } private static void writeSData(int e) { _myList.add(new Integer(e)); } private synchronized Integer readData(e) { return (Integer)_mySList.get(new Integer(e)); }

Synchronization Examples public class MyClass { private Vector _myVector=new Vector(); private static _myList=new LinkedList(); private List _mySList=new SortedList(); public void writeData(int e) { _myVector.add(new Integer(e)); _myList.add(new Integer(e)); _mySList.add(new Integer(e)); } private synchronized static void writeSData(int e) { _myList.add(new Integer(e)); } private Integer readData(e) { return (Integer)_mySList.get(new Integer(e)); }

Synchronization Examples public class MyClass { private Vector _myVector=new Vector(); private static _myList=new LinkedList(); private List _mySList=new SortedList(); public void synchronized writeData(int e) { _myVector.add(new Integer(e)); _myList.add(new Integer(e)); _mySList.add(new Integer(e)); } private synchronized static void writeSData(int e) { _myList.add(new Integer(e)); } private synchronized Integer readData(e) { return (Integer)_mySList.get(new Integer(e)); }

Synchronization Examples public class MyClass { private Vector _myVector=new Vector(); private static _myList=new LinkedList(); private List _mySList=new SortedList(); public void writeData(int e) { _myVector.add(new Integer(e)); synchronized (_myList) { _myList.add(new Integer(e)); } _mySList.add(new Integer(e)); } private static void writeSData(int e) { synchronized (_myList) { _myList.add(new Integer(e)); } } private Integer readData(e) { return (Integer)_mySList.get(new Integer(e)); }

Returning to the Thread Corrup. Ex. private int[] _multArray[] = new int[128]; private constructor() { for (int i=0;i<_multArray.length;++i) { _multArray[i]=i; } private void _seedArray(int s) { for (int i=0;i<_multArray.length;++i) { _multArray[i]=i*s; } private void _writeToFile() { _myFile.print(“_multArray=[ “); for (int i=0;i<_multArray.length;++i) { _myFile.print(_multArray[i] + “ “); } _myFile.println(“]”); }

Creating Threads in Java Every thread is represented with an object of type Thread This object gives you a handle to manipulate the thread -- start it, kill it, prioritize it, etc. Thread implements the interface Runnable Runnable specifies only one method: public void run() When a thread is started, it enters the run() method. When run() returns, the thread ends (dies). run() can call any other method it likes, sleep, etc.

Two Ways to Make Threads To make a new thread, need two things: A run() method Start the thread running To get a run() method, either 1. Subclass Thread and override run(), or 2. Make another class that implements Runnable itself To start a new thread running, either 1. Call Thread.start() 2. Thread thr=new Thread(runnableObj)

Example of Method 1 class myThreadClass extends Thread { public void run() { System.out.println(“Here goes the thread...”); } // elsewhere... public void someOtherMethod() { Thread thrObj=new myThreadClass(); thrObj.start(); }

Example of Method 2 class myRunnable implements Runnable { public void run() { System.out.println(“Here goes the thread...”); } // elsewhere... public void someOtherMethod() { Runnable runObj=new myRunnable(); Thread thrObj=new Thread(runObj); thrObj.start(); }

Method 2 + Anonymous Inner Classes public void miscMethod() { Thread thrObj=new Thread(new Runnable { public void run() { System.out.println(“In new thread...”); }).start(); }