Beruflich Dokumente
Kultur Dokumente
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.
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
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.
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/