Sie sind auf Seite 1von 41

Intel Do-It-Yourself Challenge

Managing Threads
Cdric Andreolli
www.Intel-Software-Academic-Program.com
paul.guermonprez@intel.com
Intel 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
synchronized
The keyword synchronized can be applied to a
method.

...or to a statement.

Threads in Java
Thread safe execution
Parts 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 synchronized
Here, 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 synchronized
Threads 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 scenario
We don't know which thread will be the first to
be executed.

Threads in Java
The wait()/notify() methods
Must 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 object
notifyAll(): wakes up all the treads
locked on the calling object

Threads in Java
The wait()/notify() methods
Must 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 object
notifyAll(): wakes up all the treads
locked on the calling object

Threaded Servo Controller

Threaded Servo Controller


Picture of the equipment
We 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-threading
To 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-threading
We 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 architecture
We 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 more
moving.

Threaded Servo Controller


Class diagram

Threaded Servo Controller


Servo class attributes
The 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 Thread
The 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() method
The 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() method
This 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 methods
All the manipulations of the actionQueue and
the isRunning boolean must be done in
synchronized region.

Threaded Servo Controller


Calls from the main
The main method creates the AdvancedServo
and initializes all the servos.

Threaded Servo Controller


Calls from the main
We can register a ServoStopListener.

We can start the servos even if no


command has been added.

Threaded Servo Controller


Calls from the main
Adding 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 threads
We'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 available
The 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/

Das könnte Ihnen auch gefallen