Download presentation
Presentation is loading. Please wait.
1
Slide design: Dr. Mark L. Hornick
SE3910 5/24/2019 SE3910 Week 6, Class 1 Week 6, Class 2 (Wednesday) Quiz – to be graded still Week 7, Class 2 (Wednesday) Half-Exam 2 Bring Calculator!! Qt and Java: Multithreading Full agenda: Return Exam Questions about lab due tomorrow in class? Threads: Locking on null object Threads: invokeLater Threads: The squares example Decorator Class Diagram More on Java IO Class diagrams Design Principles in the patterns we’ve seen so far Compare with alternatives Decorator vs. array approach suggested in class Non-decorator array – decorator can be added on without modifying the original hierarchy Decorator has “before-after” and possibly other combinatorial control that would be hard-coded in array [Show “screenshot” of discussion from class? Or just re-type?] Strategy vs. Decorator class diagrams side-by-side Structural difference (inheritance optional in Strategy pattern?) Decorator vs. “Strategy” array Perhaps next: Coding Starbuzz coffee (?) Add real patterns ArrayList – null-checking Java I/O: Students do coding examples SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Josiah Yoder
2
Multi-core programming
SE3910 5/24/2019 Multi-core programming SE-2811 Dr.Yoder Dr. Josiah Yoder
3
Slide design: Dr. Mark L. Hornick
SE3910 5/24/2019 Volatile SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Josiah Yoder
4
Volatile readily vaporizable at a relatively low temperature
SE3910 5/24/2019 Volatile readily vaporizable at a relatively low temperature flying or having the power to fly lighthearted, lively tending to erupt into violence : explosive unable to hold the attention fixed because of an inherent lightness or fickleness of disposition difficult to capture or hold permanently SE-2811 Dr.Yoder Dr. Josiah Yoder
5
SE3910 5/24/2019 Based on these definitions, which do you think is a closer fit for volatile? A variable which is cached – at any time, value could be incorrect because other threads change it. A variable which is not cached. Must always go to main memory to read value, because other threads may change it at any time. Note: I am NOT asking which is volatile in Java or other languages. SE-2811 Dr.Yoder Dr. Josiah Yoder
6
SE3910 5/24/2019 Volatile (in Java) A variable which is not cached. Must always go to main memory to read or write the value Why might these variables be called volatile? Why might we want a volatile variable? SE-2811 Dr.Yoder Dr. Josiah Yoder
7
SE3910 5/24/2019 Volatile (in C) SE-2811 Dr.Yoder Dr. Josiah Yoder
8
Task States (more essential rambling)
5/24/2019 Task States (more essential rambling) Wiki:Process (computing) See also Laplante and Ovaske 4E p. 97 Talk about task states and how they relate to mutex locks Draw an example UML timing diagram on the board. SE-2811 Dr.Yoder Dr. Josiah Yoder
9
Threading – pthreads (review)
SE3910 5/24/2019 Threading – pthreads (review) Java Pthreads java.lang.Thread #include <pthread.h> No external jar needed link with -pthread Thread t = new Thread(r) t.start(); pthread_create(t,r,sr,a) interface Runnable { void run(); } Parameter: void* (*sr) (void *) t.join(); pthread_join(*t, &p) Object o; pthread_mutex_init(m,null) synchronized(o) { … } … /* Garbage coll. */ pthread_mutex_lock(…) pthread_mutex_destroy(…) View links in class Dr. Josiah Yoder
10
Threading – pthreads (updated with links)
CS2852 5/24/2019 Threading – pthreads (updated with links) Java Pthreads Object o; o.notify(); phread_cond_t c = PTHREAD_COND_INITIALIZER; pthread_cond_broadcast(c); o.wait(); pthread_cond_wait(c,m); phtread_cond_signal(c); o.notifyAll(); phtread_cond_broadcast(c); View links in class. See Java coding example NotifyWaitExample Caveat: “POSIX threads can wait at condition variables of a greater generality than available in Java, but the corresponding queues may be leaky.” SE Dr. Josiah Yoder Slide style: Dr. Hornick Much Material: Dr. Schilling Dr. Yoder
11
Ex: Edit to include notify/wait to interleave these threads.
5/24/2019 Ex: Edit to include notify/wait to interleave these threads. Consider this code-snippet Queue q = … … synchronized(q) { while(true) { img = getImage(); q.offer(img); } // on another thread synchronized(q) { while(true) { img = q.poll(); showImage(); } SE-2811 Dr.Yoder Dr. Josiah Yoder
12
Threading – qthreads (with corrections)
SE3910 5/24/2019 Threading – qthreads (with corrections) Java qthreads java.lang.Thread #include <QThread> No external jar needed (moc and friends take care of this) Thread t = new Thread(r) t.start(); QThread *t = new QThread; moveToThread(t); // note here interface Runnable { void run(); } QObject (e.g. QWidget (e.g. QMainWindow)) t.join(); connect the QThread::finish() signal to a slot that checks if all threads are done. Object o; QMutex synchronized(o) { … } … /* Garbage coll. */ Avoid sharing memory entirely… … see code example… ???? Dr. Josiah Yoder
13
Useful if you are into Qt slots/signals == events
5/24/2019 Useful if you are into Qt slots/signals == events “A QThread should be used much like a regular thread instance: prepare an object (QObject) class with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That’s all.” I have successfully used this approach. SE-2811 Dr.Yoder Dr. Josiah Yoder
14
Qt’s connect method http://doc.qt.io/qt-5/qobject.html#connect
SE-2811 Dr.Yoder
15
Qt Connection types 1 2 3 0x80 Constant Value Description
Constant Value Description Qt::AutoConnection (Default) If the receiver lives inthe thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted. Qt::Direct Connection 1 The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread. Qt::Queued Connection 2 The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed inhe t receiver's thread. Qt::Blocking QueuedConnection 3 Same as Qt::QueuedConnection, except that the signalling thread blocks until the slot returns. This connection must not be used if the receiver lives in the signalling thread, or else the application will deadlock. Qt::Unique Connection 0x80 This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set,QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). This flag was introduced in Qt 4.6. SE-2811 Dr.Yoder
16
Coding example. Exercise:
Both the Java and Qt solutions will behave poorly if I start multiple threads. Predict how each solution will behave if I: Click start // what happens? (For Java? For Qt?) Click stop SE-2811 Dr.Yoder
17
Slide design: Dr. Mark L. Hornick
SE3910 5/24/2019 References EB: Derek Malloy, Exploring Beaglebone, Wiley, 2015 SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Josiah Yoder
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.