Upload
yoshio-martinez
View
217
Download
2
Tags:
Embed Size (px)
Citation preview
Intel Do-It-Yourself ChallengeManaging Threads
Cdric Andreolliwww.Intel-Software-Academic-Program.com
[email protected] Software
2013-02-08
Agenda
Agenda
Introduction
Threads in Java
Threaded Servo Controller
Conclusion
Introduction
Introduction
In our previous example, we used a sleep to guaranty that the motor had enough time to end up its move.Intel CPUs have several cores and can execute many threads.This course will show you how to use your Intel CPU in a better way.
Threads in Java
Threads in Java
Java has its own synchronization model that use the keyword synchronized and the methods:
wait() notify() notifyall()
Threads in Java
synchronizedThe keyword synchronized can be applied to a method.
...or to a statement.
Threads in Java
Thread safe executionParts of code in a synchronized section on the same object can only be executed on 1 thread.
Those 2 snippets of code use a synchronized on this.
Threads in Java
Sample without synchronized
Threads in Java
Sample without synchronizedHere, there is no synchronized, threads are executed in parallel without synchronization.Threads output are mixed up.
Threads in Java
What happens ?As there is no synchronized area in this code, each thread computed the sum at its own speed and print out the result when it's done.
Threads in Java
Sample with synchronized
Threads in Java
Sample with synchronizedThreads are still executed in parallel but the synchronized area guaranty that only one thread at a time can access this part of the code.
Threads in Java
What happens ?As there is a synchronized region in this code, both threads can't access at the same time.
Threads in Java
Other possible scenarioWe don't know which thread will be the first to be executed.
Threads in Java
The wait()/notify() methodsMust be called in a synchronized region. The thread executing the wait() will be suspended. The object that called wait() will be the only one able to wake it. Waking up the thread can be done by calling notify() or notifyAll():
notify(): wakes up one thread locked on the calling objectnotifyAll(): wakes up all the treads locked on the calling object
Threads in Java
The wait()/notify() methodsMust be called in a synchronized region. The thread executing the wait() will be suspended. The object that called wait() will be the only one able to wake it. Waking up the thread can be done by calling notify() or notifyAll():
notify(): wakes up one thread locked on the calling objectnotifyAll(): wakes up all the treads locked on the calling object
Threaded Servo Controller
Threaded Servo Controller
Picture of the equipmentWe just plug 2 servos on the Phidget Advanced Servo controller.
Threaded Servo Controller
OnMotorStop event?If you remember the Hello World, we needed to insert long sleeps after each command sent to the controller.The main reason is that the commands are executed asynchronously and no event is fired at the end of the command....In other word, the library doesn't provide any event to indicate that the position has been changed.
Threaded Servo Controller
Multi-threadingTo take advantage of the multi-core intel platform, each servo will have it's own thread.Each servo will also have a queue containing the list of command to process.The thread related to the servo will execute an infinite loop and will try to execute a command from the queue at each iteration.
Threaded Servo Controller
Multi-threadingWe want to fire an event when the servo is no more moving.We want to be able to end up the thread attached to each servo.
Threaded Servo Controller
Project architectureWe are going to use 3 classes:
Servo: That represents a single servo. This calss manages its own thread.ServoAction: Represent an action/command that will be executed by the servo.ServoStopListener: A listener that will be called when the servo is no moremoving.
Threaded Servo Controller
Class diagram
Threaded Servo Controller
Servo class attributesThe Servo class represents a physical servo. If you remember the Hello World, each physical servo is identified by its number (from 0 to 7).The Servo object must be able to execute commands, so it needs to own a reference on the Phidget card (remember that all the commands are launched from a AdvancedServoPhidget object).
Threaded Servo Controller
Servo constructor and class definition
Threaded Servo Controller
Servo extends ThreadThe simplest way to manage a thread with the class Servo is by extending Thread.Then, Servo will have a method named start() that will be used to launch the thread.We need to add some code in the run() method. This method is executed when the user calls start() on a Thread object.
Threaded Servo Controller
The run() methodThe run method is in charge of retrieving commands in the command queue.If the command queue is empty, the thread must call wait().The thread can be awaken when new commands are added to the queue.
Threaded Servo Controller
The run() method
Threaded Servo Controller
The executeAction() methodThis method is in charge of: Executing a command available in the
queue Firing an event when the servo is no more
moving.
Threaded Servo Controller
The executeAction() method
Threaded Servo Controller
Synchronized methodsAll the manipulations of the actionQueue and the isRunning boolean must be done in synchronized region.
Threaded Servo Controller
Calls from the mainThe main method creates the AdvancedServo and initializes all the servos.
Threaded Servo Controller
Calls from the mainWe can register a ServoStopListener.
We can start the servos even if no command has been added.
Threaded Servo Controller
Calls from the mainAdding a new command is easy.
The call to addAction add a ServoAction to the actionQueue and then wakes up the sleeping thread.The servo will start moving!
Conclusion
Conclusion
Managing threadsWe've seen how to manage threads in Java and how to use synchronized, wait and notify.The threaded version is easier to use. You don't have to evaluate the time needed by the command anymore.The new version use very few CPU.
Conclusion
Samples availableThe full samples are available on www.http://intel-software-academic-program.com. Feel free to reuse this code and to modify it for your own projects.
License Creative Commons - By 3.0
You are free:to Share to copy, distribute and transmit the work to Remix to adapt the work to make commercial use of the work Under the following conditions:Attribution You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).With the understanding that: Waiver Any of the above conditions can be waived if you get permission from the copyright holder. Public Domain Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license. Other Rights In no way are any of the following rights affected by the license: Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; The author's moral rights; Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
Notice For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
http://creativecommons.org/licenses/by/3.0/
Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41