Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide design: Dr. Mark L. Hornick

Similar presentations


Presentation on theme: "Slide design: Dr. Mark L. Hornick"— Presentation transcript:

1 Slide design: Dr. Mark L. Hornick
SE-2811 6/4/2018 Week 3, Day 1: Threads Muddiest Point Balancing load Transferring tasks to the Event Dispatch Thread Transferring tasks between threads manually? SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Yoder

2 Questions on Lab? I've received some good ones by email SE-2811
Dr. Mark L. Hornick

3 Threading Muddiest Point
SE-2811 6/4/2018 Threading Muddiest Point Notify() Is it always the same thread until it has nothing to do? notify() who? Why do the threads jump out of order in your example program? Why doesn't it go thread 1 then 2 then 3? Is there a way to even the load so all 3 threads finish at the same time? even loading? More thread examples more examples Nothing SE-2811 Dr. Mark L. Hornick Dr. Yoder

4 Threading Muddiest Point
SE-2811 6/4/2018 Threading Muddiest Point Everything seemed pretty straightforward today Will we learn about more ways to use threads? more examples What is synchronizing "best practice"? best practice None. How do all three threads know that they can leave the method if another thread doesn't come through and notify() them? leaving the method In what other ways can multi threading be used for? Have a great weekend! hope you did too! SE-2811 Dr. Mark L. Hornick Dr. Yoder

5 Synchronizing Best Practice
SE-2811 6/4/2018 Synchronizing Best Practice Unless you KNOW that code will be used multi-threaded, don't synchronize it (Vector vs ArrayList) If a variable will be accessed by multiple threads, synchronize every access to that variable If one thread must wait on another thread's result, synchronize that thread. SE-2811 Dr. Mark L. Hornick Dr. Yoder

6 Wait and Notify… wait() --- stop here until notified
SE-2811 6/4/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

7 Multi-threading in Swing
SE-2811 6/4/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

8 Multi-threading in Swing
SE-2811 6/4/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

9 Lambda Expression Syntax
SE2811 6/4/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

10 SE-2811 6/4/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. To best answer this question requires advanced knowledge of writing lock-free code. That will have to wait for another class… SE-2811 Dr. Mark L. Hornick Dr. Yoder

11 What would you answer? string[,] test = new string[5, 13];
SE-2811 6/4/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

12 https://www.polleverywhere.com/free_text_polls/zIp2tH2IVWjXv4H SE-2811
6/4/2018 Muddiest Point SE-2811 Dr. Mark L. Hornick Dr. Yoder


Download ppt "Slide design: Dr. Mark L. Hornick"

Similar presentations


Ads by Google