1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science.

Slides:



Advertisements
Similar presentations
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Concurrency 101 Shared state. Part 1: General Concepts 2.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
13-Jun-15 Animation. 2 Moving pictures Animation—making objects that appear to move on the screen—is done by displaying a series of still pictures, one.
Advanced Java Class Threads.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
28-Jun-15 Producer-Consumer An example of using Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
50.003: Elements of Software Construction Week 5 Basics of Threads.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
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.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
COMPSCI 230 S2C 2013 Software Design and Construction Deadlock, Performance, Programming Guidelines Lecture 6 of Theme C.
Threads.
Synchronizing threads, thread pools, etc.
11 G53SRP: Clocks and Times in RTSJ Chris Greenhalgh School of Computer Science.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads. Story so far  Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
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 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
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.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
1 G53SRP: Introduction to Real Time Scheduling Chris Greenhalgh School of Computer Science.
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.
Threads.
Multithreading / Concurrency
Chapter 13: Multithreading
Multi Threading.
G53SRP: Real Time Threads in RTSJ (part I)
Threads Chate Patanothai.
Threads II IS
Condition Variables and Producer/Consumer
G53SRP: Resource Sharing Issues
Condition Variables and Producer/Consumer
Principles of Software Development
Multithreading.
Multithreaded Programming
Chapter 15 Multithreading
Threads in Java James Brucker.
Threads and Multithreading
Threads.
Concurrent programming
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science

2 Contents The system clockThe system clock SleepingSleeping Absolute delaysAbsolute delays Time-outs on waitTime-outs on wait Java 1.5 extensions: TimeUnit, nanoTimeJava 1.5 extensions: TimeUnit, nanoTime Events, Timers and TimerTasksEvents, Timers and TimerTasks Book: Wellings 4.2 & 4.4Book: Wellings 4.2 & 4.4

3 The System Clock The JVM exposes a system clock:The JVM exposes a system clock: java.lang.System { static long currentTimeMillis(); } Also used by new java.util.Date(); to initialise to current time.Also used by new java.util.Date(); to initialise to current time. Returns milliseconds since midnight January 1 st 1970 GMT (like “UNIX time”).Returns milliseconds since midnight January 1 st 1970 GMT (like “UNIX time”). Depends on accuracy and granularity of underlying hardware clockDepends on accuracy and granularity of underlying hardware clock –Accuracy = How close it is to the real time –Granularity = The size of the “tick” (e.g. clock step) Note: may be larger than nominal resolution (e.g. 10ms in Java) Try ClockGranularity.java

4 Busy waiting delay public class BusyDelay { public static void main(String [] args) { public static void main(String [] args) { long start = System.currentTimeMillis(); long start = System.currentTimeMillis(); long delay = 1000; long delay = 1000; while (System.currentTimeMillis() < start+delay) while (System.currentTimeMillis() < start+delay) ; } ; }} Starting time “Current” time Target end time “Busy” loop – Inefficient – “wastes” CPU

5 Sleep The system allows a thread to perform a relative delay:The system allows a thread to perform a relative delay: java.lang.Thread { static void sleep(long millis); } java.lang.Thread { static void sleep(long millis); } N.B. delay is a minimum – see laterN.B. delay is a minimum – see later JVM can map request to OS sleepJVM can map request to OS sleep –Avoids busy waiting – more efficient: clock- watching is delegated to JVM and thence OS Compare CPU utilisation for BusyDelay & SleepDelay

6 Sleep delay public class SleepDelay { public static void main(String [] args) { public static void main(String [] args) { long delay = 1000; long delay = 1000; try { try { Thread.sleep(delay); Thread.sleep(delay); } catch(InterruptedException ie) } catch(InterruptedException ie) { /*ignore*/ } { /*ignore*/ } }} Relative delay time May be woken by Thread.interrupt() – see later notes

7 Sleep Granularity Time specified by program Granularity difference between clock and sleep time Interrupts disabled Thread runnable here but not executing Thread executing Time Local drift © Andy Wellings 2004 – from Wellings figure 4.1

8 Absolute delays Clock granularity, pre-emption and compute time make exact timing of sleep and code impossibleClock granularity, pre-emption and compute time make exact timing of sleep and code impossible Approximated by sleeping until (after) a specific target timeApproximated by sleeping until (after) a specific target time

9 Drifting clock public class DriftingClock { public static void main(String [] args) { public static void main(String [] args) { long delay = 1000; // 1 second = 1000 ms long delay = 1000; // 1 second = 1000 ms while(true) { while(true) { try { try { Thread.sleep(delay); Thread.sleep(delay); } catch(InterruptedException ie) } catch(InterruptedException ie) { /*ignore*/ } { /*ignore*/ } System.out.println("Tick at "+ System.out.println("Tick at "+ System.currentTimeMillis()); System.currentTimeMillis()); } }} Execution and preemption time ignored Granularity and preemption time ignored Absolute time errors accumulate

10 Absolute delay example public class SleepAbsoluteDelay { public static void main(String [] args) { public static void main(String [] args) { long start = System.currentTimeMillis(); long start = System.currentTimeMillis(); // next 10 second boundary // next 10 second boundary long tenseconds = 10*1000; long tenseconds = 10*1000; long end = ((start+tenseconds) / tenseconds)* long end = ((start+tenseconds) / tenseconds)* tenseconds; tenseconds; long delay = end-start; long delay = end-start; try { try { Thread.sleep(delay); Thread.sleep(delay); } catch(InterruptedException ie) { /*ignore*/ } } catch(InterruptedException ie) { /*ignore*/ } long now = System.currentTimeMillis(); long now = System.currentTimeMillis(); }} N.B. integer arithmetic

11 Time-outs on wait() Default wait() blocks thread indefinitely until notifiedDefault wait() blocks thread indefinitely until notified Possible to set maximum wait time:Possible to set maximum wait time: –java.lang.Object { void wait(long millis); void wait(long millis, long nanos); } E.g. forE.g. for –error detection (e.g. deadlock, message loss) –timed protocols

12 Timed-wait notes wait(0) = wait(0,0) = wait()wait(0) = wait(0,0) = wait() Cannot distinguish time-out from notifyCannot distinguish time-out from notify –Neither raises an exception (unlike interrupt) Consider notify “at the same time” as timeoutConsider notify “at the same time” as timeout –Just before => thread is woken by notify, waits for lock and returns just after timeout time –Just after => thread is woken by time-out, waits for lock and returns just after timeout time

13 Timed-wait example See TimedWait.javaSee TimedWait.java // thread 1… synchronized(tw) { long start = System. long start = System. currentTimeMillis(); currentTimeMillis(); try { try { tw.wait(5*1000); tw.wait(5*1000); } catch (InterruptedException ie) {} } catch (InterruptedException ie) {} System.out.println ("Done: " +(System.currentTimeMillis()- start)+"ms"); System.out.println ("Done: " +(System.currentTimeMillis()- start)+"ms");} // thread 2… long delay = 4000+(new java.util.Random()). nextInt(2000); try { Thread.sleep(delay); } catch(InterruptedException ie) {} synchronized(tw) { tw.notify(); } Should never be (much) more than 5000ms

14 Java 1.5 extensions (JSR166) Better support for time granularityBetter support for time granularity java.lang.System { static long nanoTime(); } –See also next slide Other concurrency utilities:Other concurrency utilities: –java.util.concurrent, e.g. queues –java.util.concurrent.atomic – for lock-free thread-safe programming w. shared variables –java.util.concurrent.locks – various lock utility classes

15 java.util.concurrent.TimeUnit package java.util.concurrent; public final enum TimeUnit { // available granularities MICROSECONDS, MILLISECONDS, NANOSECONDS, SECONDS; // utilities to manipulate times long convert(long duration, TimeUnit units); long toNanos(long duration); // timed operations in this granularity void sleep(long timeout) throws InterruptedException; void timedWait(Object monitor, long timeout) throws InterruptedException; void timedJoin(Thread thread, long timeout) throws InterruptedException; } e.g. see SleepTimeUnitsDelay

16 Events, TimerTasks and Timers A java.util.TimerTask is a Runnable object that represents a unit of workA java.util.TimerTask is a Runnable object that represents a unit of work –E.g. a GUI redraw or update A java.util.Timer represents a background thread that executes a set of TimerTask s non- concurrently:A java.util.Timer represents a background thread that executes a set of TimerTask s non- concurrently: –Enforces order –Avoids overhead of multiple threads or thread per task

17 java.util.TimerTask package java.util; public abstract class TimerTask implements Runnable { protected TimerTask(); // cancel task before (next) run public boolean cancel(); // most recent release time public long scheduledExecutionTime(); // actual task code – override public abstract void run(); }

18 java.util.Timer package java.util; public class Timer { Timer(); Timer(boolean isDaemon); void cancel(); // one-shot void schedule(TimerTask task, long delay); void schedule(TimerTask task, java.util.Date time); // repeating void schedule(TimerTask task, java.util.Date firstTime, long period); void schedule(TimerTask task, long delay, long period); void scheduleAtFixedRate(TimerTask task, java.util.Date firstTime, long period); void scheduleAtFixedRate(TimerTask task, long delay, long period); } package java.util; public class Timer { Timer(); Timer(boolean isDaemon); void cancel(); // one-shot void schedule(TimerTask task, long delay); void schedule(TimerTask task, java.util.Date time); // repeating void schedule(TimerTask task, java.util.Date firstTime, long period); void schedule(TimerTask task, long delay, long period); void scheduleAtFixedRate(TimerTask task, java.util.Date firstTime, long period); void scheduleAtFixedRate(TimerTask task, long delay, long period); }

19 Timer example import java.util.Timer; import java.util.TimerTask; public class DriftingTimerClock { static class TickTask extends TimerTask { static class TickTask extends TimerTask { int count = 0; int count = 0; public void run() { public void run() { System.out.println("Tick "+(count++)+"!"); System.out.println("Tick "+(count++)+"!"); } } public static void main(String [] args) { public static void main(String [] args) { Timer timer = new Timer(); Timer timer = new Timer(); // in 1 second, every second // in 1 second, every second timer.schedule(new TickTask(), 1000, 1000); timer.schedule(new TickTask(), 1000, 1000); }} To avoid drift use timer.scheduleAtFixedRate(…)

20 Summary Access to the system clock can support simple timingAccess to the system clock can support simple timing Thread.sleep allows efficient relative delaysThread.sleep allows efficient relative delays –Sleep time is a minimum –Care is needed to avoid cumulative errors/drift wait() can have a timeout specifiedwait() can have a timeout specified –But timeout and notify cannot be distinguished java.util.Timer and TimerTask support sequential scheduling of one-shot and repeated tasks on a shared threadjava.util.Timer and TimerTask support sequential scheduling of one-shot and repeated tasks on a shared thread –Avoiding per-task thread overhead, –preventing concurrency between tasks