CSC 143 1 CSC 143 Threads. CSC 143 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.

Slides:



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

CS 5704 Fall 00 1 Monitors in Java Model and Examples.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Foundations of Agents. Agent World Agent Definitions Agent’s Properties Classification of Agents Agent Autonomy Concurrent Programming.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
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. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
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.
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.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Internet Software Development Controlling Threads Paul J Krause.
Threading Eriq Muhammad Adams J
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Java Thread and Memory Model
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 –
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
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.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
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.
1 Dr.A.Srinivas PES Institute of Technology Bangalore, India
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
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
Multithreading / Concurrency
Multithreaded Programming in Java
Java Concurrency.
Threads Chate Patanothai.
Java Concurrency.
Multithreading Chapter 23.
Threads and Multithreading
Multithreading.
Multithreading.
Threads and Multithreading
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Computer Science 2 06A-Java Multithreading
NETWORK PROGRAMMING CNET 441
Synchronization and liveness
Presentation transcript:

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 execution environment is the same as for the rest of the program. The thread can share data with other parts (=other threads) of the program.  A thread executes concurrently with the other threads of the program.  e.g. when painting (or repainting), the method paint is executed by a thread.  Key issue: synchronization  If the threads run concurrently and share data, what happens when one thread uses data modified at the same time by another thread?

CSC The Thread class  in the package java.lang  Define a thread by extending the Thread class  override the run method to specify what the thread will do public class MyThread extends Thread{ public void run(){ /* code for thread to run when it gets control */} // more code }  to make a thread, write MyThread t = new MyThread(); t.start();

CSC Life of a thread  When start is called, the thread t becomes ready to be executed by the processor.  When another thread gets a turn, t will stop executing its run method. It will start again from where it left off, when it gets another turn  The scheduling of the threads is the responsibility of the OS  However, we can temporarily stop the execution, e.g. using the methods sleep and wait (see the examples to come)

CSC Example  Create 2 threads that write their names and sleep  see the full code on the class web site: ThreadName.java  Can you predict the order of execution?

CSC Using the Runnable interface  If our class already extends another class, how do we make it a thread? public class D extends B  Use the Runnable interface and implement the run method public class D extends B implements Runnable{ //Thread to host our run method private Thread t; // implement run public void run(){/*code*/} // to start the thread public D(){ t = new Thread(this); t.start(); }}  How would you program the example ThreadName using Runnable?

CSC Concurrent threads  When 2 threads running independently can modify the same data, they need to cooperate.  If not, the state of the data is unpredictable  See an example : BadConcurrency.java on the class web site.  To avoid the problem, synchronize the two threads,e.g. in BadConcurrency.java write: synchronized(data){ data[0]=val; data[1]=val; checkData();} // The thread must have an exclusive // access to data before entering the // synchronized block

CSC synchronization  Use the keyword synchronized  What can be synchronized?  A block of Java code (as in the previous example). synchronized(myObject){/*block*/}  The thread needs a lock on myObject before entering the block.  An instance method, e.g. public synchronized void update() {/*code*/}  a thread can execute update only if it has a lock on the class object (this).

CSC wait method  available for any object since it is part of the Object class  inside of a synchronized block, a thread can give up its hold on a lock by calling wait. Then, another thread can take the lock.  When the first thread reacquires its lock, it starts from where it left off.  Reacquisition is not assured. The thread must wake up. This is done if another thread calls notify() or notifyAll(). Then the thread is requeued and competes for the lock with the other threads.

CSC Example  A Sender sends items to a Receiver via a Buffer  Both the Sender and the Receiver can modify the Buffer content.  The access to the Buffer must be synchronized if Sender and Receiver are two independent threads.  See TestSenderReceiver.java (and other files) on the class web site.

CSC deadlock  When threads wait for locks that can't be freed.  e.g. in the Sender-Receiver example, there is deadlock if  The Sender sends only to an empty buffer  The Receiver takes only from a full buffer

CSC Animation  Animation is better achieved with threads  Without an animation thread, one part of the program can hold up the animation part.