Your SlideShare is downloading. ×
Synchronization.37
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Synchronization.37

138
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
138
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Concept of Synchronization 1
  • 2. ObjectivesOn completion of this period, you would beable to learn• Synchronization• Monitors• Unsynchronized Example• Locking Object• Synchronized Examples 2
  • 3. RecapIn this previous class, you have learnt• Concept of thread priority• Relevant methods and constants• Relevant programs 3
  • 4. Synchronization• We learnt in operating systems the concept of critical section • Two or more processes shares a resource like variables, files etc • They need to be synchronized, so that, the state of the shared resource is consistent • The same concept can be applied to the treads as well 4
  • 5. Synchronization Contd . . .• What is synchronization? • Concurrently running threads may require outside resources (shared resources) or methods • Need to communicate with other concurrently running threads to know their status and activities • Example: Producer-Consumer problem 5
  • 6. Monitor• An object which is used as a mutually exclusive lock• Only one thread can acquire a monitor at a time• All other threads attempting to enter the monitor will be suspended• Every java object has its own implicit monitor• Such monitors can be used for synchronization 6
  • 7. Unsynchronized Exampleclass TwoStrings { static void print(String str1, String str2) { System.out.print(str1); try { Thread.sleep(500); } catch (InterruptedException ie) { } System.out.println(str2); } } 7
  • 8. Unsynchronized Example Contd . . .class PrintStringsThread implements Runnable { Thread thread; A Thread implementation String str1, str2; classPrintStringsThread(String str1, String str2) { this.str1 = str1; this.str2 = str2; thread = new Thread(this); thread.start(); } public void run() { TwoStrings.print(str1, str2); Shared Resource }} 8
  • 9. Unsynchronized Example Contd . . .class TestThread { public static void main(String args[]) { new PrintStringsThread("Hello ", "there."); new PrintStringsThread("How are ", "you?"); new PrintStringsThread("Thank you ", "very much!"); } Output } Expected output is Hello there How are you? Thank you very much! 9
  • 10. Locking of an object• Assures that only one thread gets to access a particular method• Java allows you to lock objects with the use of monitors • All Java objects have their own implicit monitor • Object enters the implicit monitor when the objects ‘synchronized’ method is invoked • Once an object is in the monitor, the monitor makes sure that no other thread accesses the same object 10
  • 11. Locking of an object Contd . . .• Monitor is implemented in Java through ‘synchronized’ keyword• The ‘synchronized’ keyword can be used in two ways: 1. Prefixed to the header of the method definition 2. Can synchronize the object of which the method is a member of • synchronized (<object>) { //statements to be synchronized } 11
  • 12. First Approach : synchronized Use of synchronized keywordclass TwoStrings { synchronized static void print(String str1, String str2) { System.out.print(str1); try { Thread.sleep(500); }catch(InterruptedException ie) { } System.out.println(str2); } } 12
  • 13. Contd . . . First Approach : synchronizedclass PrintStringsThread implements Runnable { Thread thread; String str1, str2;PrintStringsThread(String str1,String str2) { this.str1 = str1; this.str2 = str2; thread = new Thread(this); thread.start(); } public void run() { TwoStrings.print(str1, str2); }} Shared Resource 13
  • 14. Contd . . . First Approach : synchronizedclass TestThread { public static void main(String args[]) { new PrintStringsThread("Hello ", "there."); new PrintStringsThread("How are ", "you?"); new PrintStringsThread("Thank you ", "very much!"); } Output } Got the output as expected 14
  • 15. Second Approach : synchronizedclass TwoStrings { static void print(String str1, String str2) { System.out.print(str1); try { Thread.sleep(500); }catch(InterruptedException ie){ } System.out.println(str2); } }class PrintStringsThread implements Runnable { Thread thread; String str1, str2; TwoStrings ts; 15
  • 16. Second Approach : synchronized Contd . . .PrintStringsThread(String str1, String str2, TwoStrings ts) { this.str1 = str1; this.str2 = str2; this.ts = ts; thread = new Thread(this); thread.start();} Use of synchronized statementpublic void run() { synchronized (ts) { ts.print(str1, str2); } }} 16
  • 17. Contd . . . Second Approach : synchronizedclass TestThread { public static void main(String args[]) { new PrintStringsThread("Hello ", "there."); new PrintStringsThread("How are ", "you?"); new PrintStringsThread("Thank you ", "very much!"); } } Output Got the output as expected 17
  • 18. Summary• In this class we have discussed • Problem of synchronization • Monitors and locking objects • Two approaches of using synchronized keyword • Relevant example programs• In the next lesson we look at Inter-Thread Communication 18
  • 19. Quiz1. Which concept is used by Java in inter-thread- synchronization A. Process B. Monitor C. Multitasking D. None 19
  • 20. Quiz Contd..2. Which keyword is used for synchronization in Java ? A. synchronize B. synchronised C. synchronized D. synchronizing 20
  • 21. Frequently Asked Questions1. Explain the concept of synchronization2. What is monitor?3. How monitor is implemented in Java?4. What are the different ways of using ‘synchronized’ keyword ? 21