Title: Modernas Programmeanas Tehnologijas Advanced Programming Technologies
1Modernas Programmeanas Tehnologijas (Advanced
Programming Technologies)
- Edgars Celms, Martin Opmanis
- (askola_at_mii.lu.lv)
Latvijas Universitates Matematikas un
informatikas instituts 2007, Riga, Latvija
2Daudzpavedienu (multithreading) programmeana
valoda JAVA
- Java How to Program, Sixth Edition
- Chapter 23. Multithreading
- Pavedienu stavokli, pavedienu dzives cikls
- Pavedienu prioritates un to aktiveana
(scheduling) - Pavedienu izveidoana un izpilde
- Pavedienu sinhronizacija
- Daudzpavedienu programmeana un GUI
- Pakotne java.util.concurrent
- Monitori valoda JAVA
3Ievads
- Daudzpavedienu programmeana (multithreading)
- Nodroina lietojumprogrammam iespeju stradat ar
daudziem izpildes pavedieniem (threads of
execution) - Dod iespeju programmai izpildit uzdevumus
paraleli - adi programmejot biei rodas nepiecieamiba pec
pavedienu sinhronizacijas, lai nodroinatu velamo
funkcionalitati
4Daas piezimes I (Software Engineering
Observation)
- A problem with single-threaded applications is
that lengthy activities must complete before
other activities can begin. In a multithreaded
application, threads can be distributed across
multiple processors (if they are available) so
that multiple tasks are performed concurrently
and the application can operate more efficiently.
Multithreading can also increase performance on
single-processor systems that simulate
concurrency when one thread cannot proceed,
another can use the processor. - Unlike languages that do not have built-in
multithreading capabilities (such as C and C)
and must therefore make nonportable calls to
operating system multithreading primitives, Java
includes multithreading primitives as part of the
language itself and as part of its libraries.
This facilitates manipulating threads in a
portable manner across platforms.
5Pavedienu stavokli, pavedienu dzives cikls
- Pavedienu stavokli (thread states)
- new stavoklis
- Pavediens sak savu dzives ciklu ar new state
- Atrodas aja stavokli lidz tam bridim kad
programma to starte un parved to runnable
stavokli - runnable stavoklis
- Pavediens aja stavokli izpilda savu uzdevumu
- waiting stavoklis
- Pavediens nonak ai stavokli ja tam ir jagaida uz
kadu citu pavedienu lidz tas paveiks kadu
uzdevumu - timed waiting stavoklis
- Pavediens nonak ai stavokli ja tam ir jagaida uz
kadu citu pavedienu vai ari tam ir jagaida uz
laika intervalu kura tas varetu izpildities - Pavediens no i stavokla atgrieas runnable
stavokli, ja tas sanem signalu no cita pavediena
vai ari sagaida savu laika intervalu - terminated stavoklis
- runnable stavokli esos pavediens nonak aja
stavokli pec sava uzdevuma izpildianas
6Pavedienu stavokli, pavedienu dzives cikls
- Pavedienu dzives cikls attelots ar UML stavoklu
diagrammu (state diagram)
7Pavedienu stavokli, pavedienu dzives cikls
- Operaciju sistemas skats uz pavediena runnable
stavokli - ready stavoklis
- Pavediens aja stavokli negaida uz citu
pavedienu, bet gan gaida kamer operaciju sistema
izdalis procesora laiku - running stavoklis
- Pavedienam aja stavokli tiek reali izpildits
(izmanto procesora laiku) - A thread in the running state often executes for
a small amount of processor time called a time
slice or quantum before transitioning back to the
ready state
Operating systems internal view of Javas
runnable state
8Pavedienu prioritates un to aktiveana (Thread
Priorities and Thread Scheduling)
- Prioritates
- Katram Javas pavedienam ir prioritate
- Prioritates ir robeas no MIN_PRIORITY (vertiba
1) un MAX_PRIORITY (vertiba 10) - Pavedieni ar lielaku prioritati ir svarigaki un
pieklus procesora resursiem pirms pavedieniem ar
zemaku prioritati - Prioritate pec nokluseanas ir NORM_PRIORITY
(konstante 5) - Pavedienu aktivetajs (thread scheduler)
- Nosaka kuram pavedienam ir jastrada ka nakamajam
- Vienkaraka implementacija ir ta saucamais
aplkartes (round-robin) princips vienadas
prioritates pavedieniem - Augstakas prioritates pavedieni var parpirkt
(parnemt) (preempt) tekoo running stavokli
esou pavedienu - Daos gadijumos augstakas prioritates pavedieni
var nedefineti aizkavet zemakas prioritates
pavedienus tiek saukta ari par badoanos
(starvation)
9Daas piezimes II (Software Engineering
Observation)
- Thread scheduling is platform dependent an
application that uses multithreading could behave
differently on separate Java implementations. - When designing applets and applications that use
threads, you must consider the threading
capabilities of all the platforms on which the
applets and applications will execute.
Thread-priority scheduling.
10Pavedienu izveidoana un izpilde
- Runnable interfeiss
- Izmanto, lai veidotu daudzpavedienu
lietojumprogrammas - Deklare run metodi
- Izpilda objekts kur implemente Executor
interfeisu - Executor interfeiss
- Deklare execute metodi
- Izveido un parvalda pavedienu grupu tiek saukta
par thread pool - ExecutorService interfeiss
- Executor apakinterfeiss kura ir deklaretas citas
metodes, kuras parvalda Executor dzives ciklu - Objektus var radit izmantojot atbilstoas klases
Executors static metodes - Metode shutdown pabeidz pavedienus ja uzdevumi ir
pabeigti - Executors klase
- Metode newFixedThreadPool izveido thread pool no
fikseta skaita pavedieniem - Metode newCachedThreadPool izveido thread pool,
kur rada jaunus pavedienus pec vajadzibas
11(No Transcript)
12Declare run method to fulfill interface
Invoke static method sleep of class Thread to
place the thread into the timed waiting state. At
this point, the thread loses the processor, and
the system allows another thread to execute. When
the thread awakens, it reenters the runnable state
13Execute each task this method will assign a
thread to the runnable
Shutdown thread pool when runnables complete
their tasks
14(No Transcript)
15Pavedienu sinhronizacija (thread synchronization)
- Pavedienu sinhronizacija
- Valoda Java programmetajam tiek dota iespeja
izmantot mutual exclusion mehanismu - Dod iespeju ekskluzivi (vienam paam) stradat ar
kopigiem (shared) objektiem - Mutual exclusion (often abbreviated to mutex)
algorithms are used in concurrent programming to
avoid the simultaneous use of un-shareable
resources by pieces of computer code called
critical sections. - Java tas tiek implementets izmantojot aiztures
(locks) - Lock interfeiss
- lock metode uzstada aizturi (lock) un iedarbina
mutual exclusion - unlock metode atbrivo aizturi (lock)
- Klase ReentrantLock implemente Lock interfeisu
- The constructor for a ReentrantLock takes a
boolean argument that specifies whether the lock
has a fairness policy. If this is set to true,
the ReentrantLock's fairness policy states that
the longest-waiting thread will acquire the lock
when it is available. If this is set to false,
there is no guarantee as to which waiting thread
will acquire the lock when it is available.
16Pavedienu sinhronizacija (thread synchronization)
- Nosacijuma mainigie (condition variables)
- Ja pavediens, kuram ir aizture (holding the lock)
nevar izpildit savu uzdevumu del ta, ka kads
nosacijums nav izpildijies, tad pavediens var
gaidit lidz nosacijums ir izpildijies - Nosacijuma mainigos var izveidot izsaucot Lock
metodi newCondition - Tiek reprezentets ar objektu kur implemente
Condition interfeisu - Condition interfeiss
- Deklare metodes
- await liek pavedienam gaidit
- signal lai pamodinatu (wake up) gaidoo
pavedienu - signalAll lai pamodinatu visus gaidoos
pavedienus
17Popularakas programmeanas kludas I (Common
Programming Error)
- Deadlock occurs when a waiting thread (let us
call this thread1) cannot proceed because it is
waiting (either directly or indirectly) for
another thread (let us call this thread2) to
proceed, while simultaneously thread2 cannot
proceed because it is waiting (either directly or
indirectly) for thread1 to proceed. Two threads
are waiting for each other, so the actions that
would enable each thread to continue execution
never occur. - It is an error if a thread issues an await, a
signal, or a signalAll on a condition variable
without having acquired the lock for that
condition variable. This causes an
IllegalMonitorStateException.
18Daas piezimes II (Software Engineering
Observation)
- When multiple threads manipulate a shared object
using locks, ensure that if one thread calls
method await to enter the waiting state for a
condition variable, a separate thread eventually
will call Condition method signal to transition
the thread waiting on the condition variable back
to the runnable state. - If multiple threads may be waiting on the
condition variable, a separate thread can call
Condition method signalAll as a safeguard to
ensure that all the waiting threads have another
opportunity to perform their tasks. If this is
not done, indefinite postponement or deadlock
could occur. - The locking that occurs with the execution of the
lock and unlock methods could lead to deadlock if
the locks are never released. Calls to method
unlock should be placed in finally blocks to
ensure that locks are released and avoid these
kinds of deadlocks. - Performance tip. Using a Lock with a fairness
policy helps avoid indefinite postponement, but
can also dramatically reduce the overall
efficiency of a program. Because of the large
decrease in performance, fair locks are only
necessary in extreme circumstances. - Performance tip. Synchronization to achieve
correctness in multithreaded programs can make
programs run more slowly, as a result of thread
overhead and the frequent transition of threads
between the waiting and runnable states.
19Raotaja/pateretaja attieciba (producer/consumer
relationship) bez sinhronizacijas
- Raotaja/pateretaja attieciba
- Raotajs genere datus un glaba tos kopiga atminas
apgabala (shared memory) - Pateretajs lasa saraotos datus no kopiga atminas
apgabala - Kopigo atminas apgabalu sauksim par buferi
(buffer)
20Implement the runnable interface so that producer
will run in a separate thread
21(No Transcript)
22Implement the runnable interface so that producer
will run in a separate thread
Declare run method to satisfy interface
23Sleep for up to 3 seconds
24Read the value of the buffer
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29Raotaja/pateretaja attieciba (producer/consumer
relationship) ar sinhronizaciju
- Raotaja/pateretaja attieciba ar sinhronizaciju
- Piemera tiek izmantots Locks un Conditions, lai
implementetu sinhronizaciju
30Buffer shared by producer and consumer
31(No Transcript)
32(No Transcript)
33(No Transcript)
34(No Transcript)
35(No Transcript)
36(No Transcript)
37(No Transcript)
38(No Transcript)
39Popularakas programmeanas kludas II (Common
Programming Error)
- Place calls to Lock method unlock in a finally
block. If an exception is thrown, unlock must
still be called or deadlock could occur. - Forgetting to signal a thread that is waiting for
a condition is a logic error. The thread will
remain in the waiting state, which will prevent
the thread from doing any further work. Such
waiting can lead to indefinite postponement or
deadlock.
40Daas piezimes III (Software Engineering
Observation)
- Always invoke method await in a loop that tests
an appropriate condition. It is possible that a
thread will reenter the runnable state before the
condition it was waiting on is satisfied. Testing
the condition again ensures that the thread will
not erroneously execute if it was signaled early.
41Raotaja/pateretaja attieciba (producer/consumer
relationship) izmantojot rinkveda (circular)
buferi
- Iepriekejos piemeros neoptimals laika
sadalijums, ja plusmas izpilda savus uzdevumus ar
daadiem atrumiem - Rinkveida buferis (circular buffer)
- Tiek nodroinats kopigs atminas apgabals
(programmejama izmera), kura raotajs var
ievietot vertibas un pateretajs var is vertibas
nolasit
42Daas piezimes IV (Software Engineering
Observation)
- Performance tip.Even when using a circular
buffer, it is possible that a producer thread
could fill the buffer, which would force the
producer thread to wait until a consumer consumes
a value to free an element in the buffer.
Similarly, if the buffer is empty at any given
time, the consumer thread must wait until the
producer produces another value. The key to using
a circular buffer is to optimize the buffer size
to minimize the amount of thread wait time.
43Circular buffer provides three spaces for data
44Update index this statement imposes the
circularity of the buffer
Signal waiting thread it can now read data from
buffer
45Lock the object before attempting to read a value
Wait for a value to be written to the buffer
Update read index this statement imposes the
circularity of the buffer
46Signal thread waiting to write to the buffer
47(No Transcript)
48Create CircularBuffer for use in both the
producer and consumer
49(No Transcript)
50(No Transcript)
51(No Transcript)
52Raotaja/pateretaja attieciba (producer/consumer
relationship) izmantojot ArrayBlockingQueue
- Klase ArrayBlockingQueue
- Rinkveida bufera implementacija valoda Java
- Implemente BlockingQueue interfeisu
- Deklare metodes put un take, lai varetu rakstit
un lasit datus buferi
53(No Transcript)
54Remove value from buffer blocks if buffer is
empty
55Create BlockingBuffer for use in producer and
consumer
56(No Transcript)
57Daudzpavedienu programmeana un GUI
- Swing GUI komponentes
- Nav pavedienu droas
- Izmainas ir javeic event-dispatching pavediena
(tiek saukts ari par event-handling pavedienu) - Lai realizetu var lietot klases SwingUtilities
static metodi invokeLater nododot tai objektu,
kur implemente Runnable interfeisu
58Implement the Runnable interface
Lock to implement mutual exclusion
Condition variable for suspending the threads
Boolean to control whether thread is suspended
Create the Lock and a Condition variable
Get name of current thread
59Wait while thread is suspended
60(No Transcript)
61(No Transcript)
62Create Lock for the application
63(No Transcript)
64(No Transcript)
65(No Transcript)
66Daas piezimes V (Software Engineering
Observation)
- The Runnable interface provides only the most
basic functionality for multithreaded
programming. In fact, this interface has several
limitations. - Suppose a Runnable encounters a problem and tries
to throw a checked exception. The run method is
not declared to throw any exceptions, so the
problem must be handled within the Runnable. The
exception cannot be passed to the calling thread.
- Now suppose a Runnable is performing a long
calculation and the application wants to retrieve
the result of that calculation. The run method
cannot return a value, so the application must
use shared data to pass the value back to the
calling thread. This also involves the overhead
of synchronizing access to the data. - The developers of the new concurrency APIs in
J2SE 5.0 recognized these limitations and created
a new interface to fix them. The Callable
interface (package java.util.concurrent) declares
a single method named call. This interface is
designed to be similar to the Runnable interface
allowing an action to be performed concurrently
in a separate thread but the call method allows
the thread to return a value or to throw a
checked exception.
67Citas klases java.util.concurrent pakotne
- Callable interfeiss
- Deklare call metodi
- Metode call dod iespeju paralelam uzdevumam
atgriezt vertibu vai ari izveidot iznemumgadijuma
situaciju (throw an exception) - ExecutorService metodei submit ir Callable
parametrs un ta atgrie Future, kas reprezente
uzdevuma rezultatu - Future interfeiss
- Deklare get metodi
- Metode get atgrie uzdevuma rezultatu
68Monitori valoda JAVA (monitors and monitor locks)
- Monitori (monitors)
- Katram Javas objektam ir monitors
- Atlauj vienlaicigi izpildities tikai vienam
pavedienam synchronized instrukcijas iekiene - synchronized ( object )
-
- statements
- // end synchronized statement
- Pavedieni kuri gaida, lai iegutu monitora aizturi
(lock) tiek novietoti blocked stavokli - Object metode wait novieto pavedienu waiting
stavokli - Object metode notify pamodina waiting stavokli
esou pavedienu - Object metode notifyAll pamodina visus waiting
stavokli esous pavedienus - Java ir iespejamas ari sinhronizetas metodes
(synchronized methods). - Sinhronizeta metode ir ekvivalenta tam, ja
synchronized instrukcija butu ieklavusi visu
metodes kermeni
69Daas piezimes VI (Software Engineering
Observation)
- The locking that occurs with the execution of
synchronized methods could lead to deadlock if
the locks are never released. When exceptions
occur, Javas exception mechanism coordinates
with Javas synchronization mechanism to release
locks and avoid these kinds of deadlocks.
70Popularakas programmeanas kludas III (Common
Programming Error)
- It is an error if a thread issues a wait, a
notify or a notifyAll on an object without having
acquired a lock for it. This causes an
IllegalMonitorStateException.
71Declare a synchronized set method
72Declare synchronized get method
73Notify thread it may now write to buffer
74Create SynchronizedBuffer for use in producer and
consumer
Execute the producer and consumer in separate
threads
75(No Transcript)
76(No Transcript)
77Jautajumi ?