Download presentation
Presentation is loading. Please wait.
1
Week 6, Class 2: Observer Pattern
11/15/2018 Week 6, Class 2: Observer Pattern THESE ARE CUT SLIDES THAT I MAY USE IN A FUTURE RENDITION OF THE CLASS 17q2 1-3,6-7,9-12 SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Yoder
2
SE-2811 11/15/2018 If you think the internet is not working in its current incarnation, you can’t change the system through think-pieces and F.C.C. regulations alone. You need new code [... you need cryptocurrency!]. Stephen Johnson, "Beyond the Bitcoin Bubble,” New York Times, Jan 15th 2018 [Interpretation added] 18q2-7-1 SE-2811 Dr. Mark L. Hornick Dr. Yoder
3
Java locks are re-entrant
public void example(Object lock, …) { synchronized (lock) { ... doSomething(lock, ...) } public void doSomething(Object lock, ...) { synchronized (lock) { // If I already have the lock, I can enter code ... // locked on the same object } // So I won’t accidentally lock waiting for myself! } SE-2811 Dr. Mark L. Hornick
4
Wait and Notify… wait() --- stop here until notified
SE-2811 11/15/2018 Wait and Notify… wait() --- stop here until notified notify() --- notify a waiting thread that it can continue notifyAll() – wakes up all waiting threads Like most threading methods notify() does not guarantee that any given thread will start next. Details: These are all methods of the Object class! (In contrast to join()) SE-2811 Dr. Mark L. Hornick Dr. Yoder
5
Multi-threading in Swing
SE-2811 11/15/2018 Multi-threading in Swing The Single-Thread Rule Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread. -- Sun Tutorial “Threads in Swing,” Way Back Machine What events should be handled by the event dispatching thread? SE-2811 Dr. Mark L. Hornick Dr. Yoder
6
Multi-threading in Swing
SE-2811 11/15/2018 Multi-threading in Swing “A Swing component that's a top-level window is realized by having one of these methods invoked on it: setVisible(true) show() /** Deprecated */ pack()” From the “Obsolete” Java Swing Tutorial SE-2811 Dr. Mark L. Hornick Dr. Yoder
7
Lambda Expression Syntax
SE2811 11/15/2018 Lambda Expression Syntax Exercise: What parts does a regular method have? Lambda Expression: A variety of syntaxes are available: <arguments> -> <method body> (Scanner in) -> {int x = in.nextInt(); return x+1;} (int x) -> x+1 () -> System.out.println(“hi”) x -> x+1 SE-2811 Dr. Mark L. Hornick Dr. Josiah Yoder
8
The synchronized statement
synchronized(variable) { variable.get(…) // only one of these } variable.set(…) // statements can be variable = new V() // executed at a time… SE-2811 Dr. Mark L. Hornick
9
What is the difference between
SE-2811 11/15/2018 What is the difference between public synchronized void myMethod() { // … } and public void myMethod() { synchronized (this) { } ? SE-2811 Dr. Mark L. Hornick Dr. Yoder
10
SE-2811 11/15/2018 The synchronized statement (§14.17) computes a reference to an object; it then attempts to perform a lock action on that object and does not proceed further until the lock action has successfully completed. ... SE-2811 Dr. Mark L. Hornick Dr. Yoder
11
SE-2811 11/15/2018 A synchronized method (§ ) automatically performs a lock action when it is invoked… If the method is an instance method, it locks the lock associated with the instance for which it was invoked (… “this” …). If the method is static, it locks the lock associated with the Class object that represents the class in which the method is defined. ... SE-2811 Dr. Mark L. Hornick Dr. Yoder
12
Conclusion: public synchronized void myMethod()
SE-2811 11/15/2018 Conclusion: public synchronized void myMethod() And synchronized (this) { // … } are the same public static synchronized void myMethod() synchronized (SomeClass.class) { // … } Are the same SE-2811 Dr. Mark L. Hornick Dr. Yoder
13
Is it safe to have multiple blocks access the same data?
SE-2811 11/15/2018 Is it safe to have multiple blocks access the same data? See e.g. “The synchronized statement (§14.17) computes a reference to an object; it then attempts to perform a lock action on that object” Yes or No? SE-2811 Dr. Mark L. Hornick Dr. Yoder
14
SE-2811 11/15/2018 I have been programming C# for 10 months now. I am now learning multi-threading and seems to be working nicely. I have multi-dimension array like string[,] test = new string[5, 13]; I have the threads calling methods that end up saving their outputs to different coordinates inside the array above, without any one thread writing to the same location as another thread. So thread1 might write to test[1,10] but no other thread will ever write to test[1,10] My question is: I have read about using locks on objects like my array, do I have to worry at all about locks even though my threads might access to the test array at the same time but never write to the same coordinates(memory location)? So far in my testing I have not had any issues, but if someone more seasoned than myself knows I could have issue then I'll look into use locks. SE-2811 Dr. Mark L. Hornick Dr. Yoder
15
What would you answer? string[,] test = new string[5, 13];
SE-2811 11/15/2018 What would you answer? string[,] test = new string[5, 13]; thread1 might write to test[1,10] but no other thread will ever write to test[1,10] do I have to worry at all about locks? SE-2811 Dr. Mark L. Hornick Dr. Yoder
16
Wait and Notify… wait() --- stop here until notified
SE-2811 11/15/2018 Wait and Notify… wait() --- stop here until notified notify() --- notify a waiting thread that it can continue notifyAll() – wakes up all waiting threads Like most threading methods notify() does not guarantee that any given thread will start next. Details: SE-2811 Dr. Mark L. Hornick Dr. Yoder
17
Multi-threading in Swing
SE-2811 11/15/2018 Multi-threading in Swing The Single-Thread Rule Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread. -- Sun Tutorial “Threads in Swing,” Way Back Machine What events should be handled by the event dispatching thread? SE-2811 Dr. Mark L. Hornick Dr. Yoder
18
Multi-threading in Swing
SE-2811 11/15/2018 Multi-threading in Swing “A Swing component that's a top-level window is realized by having one of these methods invoked on it: setVisible(true) show() /** Deprecated */ pack()” From the “Obsolete” Java Swing Tutorial SE-2811 Dr. Mark L. Hornick Dr. Yoder
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.