CST8277_Week 1_Threads_and_Sync.pptx

CST8277_Week 1_Threads_and_Sync.pptx - CST8277 ENTERPRISE...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Instructor: Areej Office: A1-049 Email: [email protected] CST8277 ENTERPRISE APPLICATION PROGRAMMING
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Textbook: Synchronization Example: SimpleArray Fig. 23.5 , 23.6, 23.7 Produce/Consumer without synchronization Fig. 23.10, 23.11, 23.12 , 23.13 Produce/Consumer using ArrayBlockingQueque Fig. 23.14, 23.15 Advanced Produce/Consumer with synchronized, wait, notify, and notifyAll Fig. 23.16
Image of page 2
The synchronization problem This problem can arise when multiple threads access shared data. ex. Producer/Consumer problem. Producer fills the buffer Consumer empties the buffer Which one runs, when, and for how long? buffer |p|q|r| | | | idx = 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Object Monitor Lock In Java, every object has a flag associated with it, called the Monitor Lock. Used with the synchronized keyword All methods accessing shared data should synchronize on the same lock object Only one synchronized method may be active on an object at once All other threads trying to acquire the Monitor Lock will block
Image of page 4
Using synchronized keywords as a method modifier: public synchronized push( char c ) only one Thread can be active in the Object's synchronized methods Other Threads will block until the active Thread returns from the method when one Thread is executing in a synchronized method, all other Threads are locked out of all of the synchronized methods of that object
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using synchronized keyword (cont'd) as a block modifier: synchronized( someObject ){ … } Any thread trying to execute the code "..." in that block must obtain the Monitor Lock of someObject Only one thread at a time can hold the Monitor Lock of an object Other threads will block until the Monitor Lock is released when the current thread leaves the block
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern