Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1.

Slides:



Advertisements
Similar presentations
Multithreading The objectives of this chapter are:
Advertisements

G52CON: Concepts of Concurrency Lecture 2 Processes & Threads Chris Greenhalgh School of Computer Science
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Java Threads Part I. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications in.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
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.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading in JAVA
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Java 3: Odds & Ends Advanced Programming Techniques.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
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 in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
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 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Notices Assn 4 posted. Due last day of class. Note that Exercise 10 contains the JavaFX code for three simple GUI programs. Winter 2016CMPE212 - Prof.
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.
Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Doing Several Things at Once
Multithreading / Concurrency
Chapter 4: Threads.
Multithreading Lec 23.
Chapter 13: Multithreading
Java Multithreading.
Multithreading.
Threads and Multithreading
Multithreaded Programming in Java
More About Threads.
Chapter 19 Java Never Ends
CMPE212 – Stuff… Marking finished for Quiz 3.
Threads Chate Patanothai.
Threads II IS
Multithreading Chapter 23.
Threads and Multithreading
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Threads and Multithreading
Threads and Multithreading
21 Threads.
Threads and Multithreading
9. Threads SE2811 Software Component Design
Chapter 15 Multithreading
Multithreading in java.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads.
Multithreading The objectives of this chapter are:
Mobile Computing With Android ACST 4550 Android Animation
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1

Oracle Demos 3D Drawing and moving the camera angle: MoleculeSampleDemo. 3D Drawing and sound: XylophoneDemo Complex animation - TreeAnimationDemo Winter 2016CMPE212 - Prof. McLeod2

Winter 2016CMPE212 - Prof. McLeod3 Example Program Suppose that we want to have a window that: 1.Has a timer counting seconds. 2.Buttons to start and stop the timer. 3.A TextArea node to store comments, provided one at a time. 4.A Close button to close the window. –No sweat, right? See LoggerNoThreads

Winter 2016CMPE212 - Prof. McLeod4 So, What’s the Problem? The program cannot move beyond the timer loop. Events such as paint() calls will not get processed – they just pile up! What’s the solution?

Winter 2016CMPE212 - Prof. McLeod5 Multi-Threading In a JavaFX program Application runs on its own thread. Our timing loop is still on the Application thread and is blocking other thread processes. The easiest way is to use the existing animation thread that is already available through the use of the Timeline object. See LoggerTimeline

Timeline From yesterday’s lecture: timeline = new Timeline(new KeyFrame(Duration.ZERO, actionEvent -> incrementClock()), new KeyFrame(Duration.millis(1000))); timeline.setCycleCount(Timeline.INDEFINITE); Winter 2016CMPE212 - Prof. McLeod6

Timeline, Cont. Use timeline.playFromStart() to start the timer and timeline.stop() to halt it. Invoking Platform.exit() will also stop all Application threads when you exit the program. Winter 2016CMPE212 - Prof. McLeod7

Winter 2016CMPE212 - Prof. McLeod8 Multi-Threading, Cont. Suppose you wish to create your own thread instead of piggybacking on an existing thread? Use classes from the javafx.concurrent package. These include Service and Task. Threads based on the Thread class cannot work with the Application thread.

Task Task is designed to work with JavaFX applications. It cannot directly access scenegraph nodes on the Application thread, because Node objects are not “thread safe”. It is a one-shot deal. Once the task is finished, you cannot re-start it. It is generic! You must override the T call() method. Winter 2016CMPE212 - Prof. McLeod9

Task, Cont. call() contains the process that will run on a separate thread. call() returns a type T, but if it does not return anything then you can return null and use “Void” for the type. Winter 2016CMPE212 - Prof. McLeod10

Task, Cont. The class uses “update” methods to update node contents: updateMessage(String message) updateProgress(…) updateTitle(…) updateValue(T value) These are to be bound to specific node properties: Winter 2016CMPE212 - Prof. McLeod11

Task, Cont. Binding, example: lblTime.textProperty().bind(myService.messageProperty()); “myService” is a Service object, but use the same syntax to bind a Task property to a node property. Inside the Task call method: updateMessage("" + timeCounter); Winter 2016CMPE212 - Prof. McLeod12

Task, Cont. These update calls are immediately “coalesced” on the Application thread. They may not run at once, but will run in the order in which they have been added to the main thread. One way to update multiple nodes is to add a change listener to the bound node which updates the other nodes of interest. Another is to use an AnimationTimer to trigger updates when a frame is drawn. Winter 2016CMPE212 - Prof. McLeod13

Task, Cont. If you need to do more to the scenegraph that just update the property of a single node, then consider using: Platform.runLater(Runnable r) The Runnable is a process (which can be a Task, for example) which will be piled onto the Application thread. Winter 2016CMPE212 - Prof. McLeod14

Service A Task cannot be re-started. Use a Service instead. Service creates and manages a Task in a way that is compatible with the Application thread. Generic! Override the method: Task createTask() This method creates the Task object for the Service. Winter 2016CMPE212 - Prof. McLeod15

Service, Cont. Has many useful methods to monitor and manipulate the task, including: –.cancel()// Stops the task –.cancelled()// Returns true if the task is cancelled –.isRunning()// Returns true if the task is running –.reset()// Resets the service –.restart()// Cancels and re-starts the task –.start()// Starts the task See LoggerThreaded Winter 2016CMPE212 - Prof. McLeod16

Winter 2016CMPE212 - Prof. McLeod17 Daemon vs. User Threads A Daemon thread is a system thread usually created by the JVM, but you can invoke setDaemon(true) to make your own thread a Daemon thread instead of a default User thread: Thread th = new Thread(task); th.setDaemon(true); This must be done on the Application thread.

Winter 2016CMPE212 - Prof. McLeod18 Daemon vs. User Threads A process is terminated when all user threads have stopped. Then all Daemon threads will be stopped by the JVM. For example, the garbage collector is a Daemon thread. You can use this to have a process that can prevent closing the window even when all other normal threads have halted.

Winter 2016CMPE212 - Prof. McLeod19 The Thread Class, Cont. Threads have a priority. This helps the processor decided which thread gets a larger slice of processor time. Threads with a lower priority can be delayed by CPU-intensive threads with a higher priority. Use getPriority() and setPriority(int) methods. Priorities lie between 1 and 10, with 5 being the default. (Use Thread.MAX_PRIORITY, Thread.MIN_PRIORITY and Thread.NORM_PRIORITY.)

Winter 2016CMPE212 - Prof. McLeod20 The Thread Class, Cont. Other methods are listed in the API. In particular, sleep(long sleeptime) can put the current thread to sleep for a specified number of milliseconds. This gives other threads a chance to do something. You should always place a sleep() command inside any loops in your thread. sleep() can throw the InterruptedException too. Or you can simply invoke the yield() method to pause the current thread and allow other threads to execute.

Multi-Threading, Cont. The thread in a task can be halted (externally!) in two ways: A cancel has been issued. An InterruptedException has been thrown. Inside a loop in your call method, you need to invoke.isCancelled() to see if you need to stop the loop. Put any blocking calls like Thread.sleep() inside a try catch to catch the InterruptedException and use the catch to stop the loop. Winter 2016CMPE212 - Prof. McLeod21