Java ThreadsGraphics Programming Graphics Programming: Java Threads.

Slides:



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

1 More on Threads b b A section of code executed independently of other threads written within a single program. b b Java threads can access global data;
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1 Java - Threads A thread is an individual flow of control within a larger program. A program which is running more than one thread is said to be multithreaded.
Concurrency…leading up to writing a web crawler. Web crawlers.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng 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.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
1 Java Threads Instructor: Mainak Chaudhuri
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.
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
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
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.
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.
Multithreading in JAVA
Java Thread and Memory Model
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.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
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.
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.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
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.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
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.
Threads Chate Patanothai.
Multithreading.
Principles of Software Development
Multithreading.
Multithreaded Programming
NETWORK PROGRAMMING CNET 441
Representation and Management of Data on the Internet
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Threads and concurrency / Safety
Presentation transcript:

Java ThreadsGraphics Programming Graphics Programming: Java Threads

Agenda Definitions and the Java Thread Model Creating Threads in Java Suspending Threads (sleeping) Interrupting Threads Synchronising Thread processes

The Java Thread Model Software engine that can process instructions Allow for simulated concurrent processing Provides asynchronous input & output Essential for tasks such as animation Thread is not a program

New Threads 1 Create a Thread by Extending the Thread Class public class MyThread extends Thread { public void run () { // your instructions here }

New Threads 1 Instantiating: MyThread testThread = new MyThread( ); You can use any constructor that you define in your extended class. As in other classes, the default constructor for the superclass (Thread()) will be called automatically

New Threads 2 Create a Thread by implementing the Runnable Interface: Runnable has only one method - public void run(); public class MyRunnable implements Runnable { public void run(){ // your instructions here }

New Threads 2 Instantiating: MyRunnable firstRunnable = new MyRunnable ()

New Threads 2 A class that implements the Runnable interface can: extend any other class. may itself be the target of one or more threads throughout the Runnable threads existence. e.g Thread testThread = new Thread(firstRunnable);

Starting Threads Starting an Instance of a Thread: testThread.start()

Starting Threads testThread.start() This starts a new flow of execution with your instructions and returns immediately. From that moment onward, the instructions that you specified in your Thread’s run method will be executing in parallel to whatever follows the start() method call.

Threads in the Running State Execute their methods concurrently with other Threads Co-operate and share resources compete to get their tasks done as soon as possible On a single CPU, time slicing between Threads gives the illusion of true concurrency Threads runtime data stored in private memory space Different Threads can act on the same object

Sleeping Threads zzzzz…. Call the Thread’s sleep method Thread.sleep(5*60*1000) // for a five-minute nap This makes the current thread sleep for five minutes, while other threads keep running.

Interrupting Threads sleepingThread.interrupt( ) ; flag gets set inside the thread to wake it up throws an exception Thus the sleep call needs to be surrounded inside a try / catch block

Thread Exceptions try { Thread.sleep(sleepingTime) ; } catch(InterruptedExeption e) { // do something about it }

Threads: Synchronizing Access Synchronizing methods protect the current thread’s target object’s state from being modified by another thread. The current thread has a lock on the object preventing other Threads from accessing the object. Similar to a file being ‘locked to write’ public synchronized void aMethod() { // process data in local variables etc. }