Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1."— Presentation transcript:

1 Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1

2 Task Object, Summary A generic class. Override T call() with a process to be threaded that will run once. Cannot be re-started. Can only access nodes through bound properties. Invoke an “update” method from the Task. Winter 2016CMPE212 - Prof. McLeod2

3 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. McLeod3

4 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. McLeod4

5 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. McLeod5

6 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. McLeod6

7 Winter 2016CMPE212 - Prof. McLeod7 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.

8 Winter 2016CMPE212 - Prof. McLeod8 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.

9 Winter 2016CMPE212 - Prof. McLeod9 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.)

10 Winter 2016CMPE212 - Prof. McLeod10 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.

11 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. McLeod11

12 Use of Multi-Core Processors in Java >7 Look up the “Fork/Join” framework in the Java Tutorial, which is in the Concurrency section: http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html Designed to work best with recursive processes, it allows you to split your tasks over the multiple cores of your processor. The framework handles all the multiple threads needed in a thread “pool”. Winter 2016CMPE212 - Prof. McLeod12

13 Multi-Core Processing, Cont. The idea is straight-forward: if the task is small then: Do the task normally, non-recursively otherwise: Split the task into two parts and recurse. The size of “ small ” depends on what you are trying to do – some experimentation will be required... Winter 2016CMPE212 - Prof. McLeod13

14 Multi-Core Processing, Cont. You need to do this in such a way that the recursed tasks are given to “Worker” threads that are part of a thread pool controlled by the fork/join framework. The framework spreads the threads over the available cores using a “Work stealing” algorithm: If a core thread is idle a task can be stolen from a threaded core that is still busy. Winter 2016CMPE212 - Prof. McLeod14

15 Multi-Core Processing, Cont. Your task must be built as a class that extends java.util.concurrent.RecursiveAction. You need to write a concrete version of void compute() This is where you carry out the tasks. Recurse your task by instantiating your class twice and then supply these objects to a call to invokeAll(task1, task2) Winter 2016CMPE212 - Prof. McLeod15

16 Multi-Core Processing, Cont. In order to start the process, instantiate your class for the first time and supply this object to the invoke() method of an instantiated ForkJoinPool object. Sound complicated? Not really! See the demo program MultiCoreQuicksortingExperiment.java. Compares a normal quicksort to a multi-core quicksort. Winter 2016CMPE212 - Prof. McLeod16

17 Multi-Core Processing, Cont. Note the use of: Runtime.getRuntime().availableProcessors() to get the number of available cores. Even without any further experimentation to optimize the code, the multi-core version is definitely faster than a normally-coded quicksort! Winter 2016CMPE212 - Prof. McLeod17

18 Want to Know More about Multi- Threading? Read over the Concurrency section in the Java Tutorial: http://docs.oracle.com/javase/tutorial/essential/conc urrency/index.html Winter 2016CMPE212 - Prof. McLeod18

19 Other JavaFX Topics Packaging, Deployment & Distribution. Web page embedded, stand-alone, etc. 3D graphics. Images and image manipulation. 3D Animation. Lots of other controls! Charts. Visual effects and transformation. Media (sound and video) playback. Winter 2016CMPE212 - Prof. McLeod19

20 Other Java Topics Other Collection Types. Iterators. Streams. Sockets. Internet streams and web pages. Regular expressions. Working with databases. Security. JDBC, JSP, JMX, JNDI, JAXP, JAXB, RMI, … Winter 2016CMPE212 - Prof. McLeod20

21 More Acronyms! Java Database Connectivity Java Server Pages Java Management Extensions Java Naming and Directory Interface Java API for XML Processing Java Architecture for XML Binding Remote Method Invocation Look for ‘Specialized Trails and Lessons’ in the Java Tutorial. Winter 2016CMPE212 - Prof. McLeod21


Download ppt "Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1."

Similar presentations


Ads by Google