Your SlideShare is downloading. ×
0
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
Mobile Application Development: Lecture 20
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

Mobile Application Development: Lecture 20

499

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
499
On Slideshare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
0
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. Mobile Application Development Lecture 20 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Threads & Access Synchronization ● SQLite Database Access Synchronization ● Content Providers – Authority Registration – Content URIs – Table Data Model – ContentProvider Resolutionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 3. Threads & Access Synchronizationhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 4. Thread Behavior ● Each thread starts and runs to completion ● A thread finishes when its run() method finishes ● A thread can never be re-started (calling start() more than once on the same Thread object throws IllegalThreadStateException) ● The order in which the Runnable threads get to run is JVM-specifichttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 5. Thread Behavior ● When a Thread goes to sleep (Thread.sleep() is executed) and wakes up, it does not start running – it becomes Runnable and may be selected to run by the JVM scheduler ● A yielding Thread (Thread.yield() is executed) yields to the Threads with the same priorities but there is no guarantee that another Thread will be chosen by the JVM scheduler – the same Thread (the one that just yield) may be chosen to run again and againhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 6. Thread Behavior ● When a Thread goes to sleep (Thread.sleep() is executed) and wakes up, it does not start running – it becomes Runnable and may be selected to run by the JVM scheduler ● A yielding Thread (Thread.yield() is executed) yields to the Threads with the same priorities but there is no guarantee that another Thread of the same priority will be chosen by the JVM scheduler – the same Thread (the one that just yielded) may be chosen to run again and againhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 7. When to Synchronize ● Any time when multiple threads are accessing and modifying the same data ● Any time when there is a race condition: multiple threads render the data inconsistent ● Race condition example: Two threads A and B manipulate the same book database. A reads a books price and goes to sleep. B modifies the books price. A wakes up and orders the book on the basis of the old price.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 8. How to Synchronize ● Every Java Instance Object and Java Class Object has a built-in lock ● Non-static methods should synchronize on Instance Objects (this Object) ● Static methods should synchronize on Class Objects ● Since there is only one lock per Object, once a Thread pick up the lock, no other thread can enter until the first Thread finisheshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 9. How to Synchronize: Blocks & Methods // Here is an example of a synchronized block // Here is an example of a synchronized method class MySync { class MySync { public void myMethod() { public void synchronized myMethod() { synchronized(this) { // do some stuff here // do some stuff here } } } } }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. How to Synchronize: Static Blocks & Methods // An example of a synchronized static block // Example of a synchronized static method class MyStaticSync { class MyStaticSync { static int mInt = 0; static int mInt = 0; public static int getInt() { public static synchronized int getInt() { return mInt; synchronized(MyStaticSync.class) { } return mInt; } } } }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. How Threads Sync ● Threads calling non-static synchronized methods/blocks block each other if they synchronize on the same Instance Objects ● Threads calling static synchronized methods/blocks block each other if they synchronize on the same Class Objects ● Threads executing static and non-static methods/blocks NEVER block each other ● Rule of Thumb: Be careful when mixing static and non- static synchronization and check which Thread synchronizes on which objecthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 12. Example We have created several applications that manipulate the book database with two tables: book_title and book_author. We have learned how to update and populate the database in the background with Threads and Services. We will now synchronize access to the database. To wit, we will implement an application and that starts a service that monitors two XML resources (XML book title table and XML book author table) and updates the appropriate tables in the book database. Database access is statically synchronized for reading and writing via a custom Application object. So that only one thread can read/write from/into the book database.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 13. SyncSQLiteBookApp Source code is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 14. Content Providershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 15. Content Providers as Web Sites ● Each content provider registers itself on a device as a web site ● The registration is done with a string called authority ● Authority is similar to a domain name ● Authority string is the basis of a set of URIs the content provider offershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 16. Authority Registration ● Authority must be registered in AndroidManifest.xml ● This is how ContentProviders become known (registered) with the Android ecosystem <provider android:name=".ScienceContentProvider" android:authorities="org.vkedco.provider.science"> </provider>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 17. Authority Registration <provider android:name=".ScienceContentProvider" android:authorities="org.vkedco.provider.science"> </provider> ● In the above registration, the provider serves the URIs with the prefix content://org.vkedco.provider.sciencehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 18. Table Data Model ● Content providers structure data like relational database tables ● Rows can be viewed as records or instances of some data type (e.g. a Mathematician or a Book) ● Columns represent member variables and their values in corresponding instances ● It is irrelevant to outside clients whether the actual data reside in a relation databasehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 19. Content Resolvers ● When a component must access the data in a ContentProvider it must use the ContentResolver object in the Context of its application ● The ContentResolver object allows the component to communicate with the ContentProvider as a client ● The ContentResolver is the intermediary between the client component and the ContentProviderhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 20. Content Resolvers ● ContentResolver objects offers clients the same methods as the corresponding ContentProvider object ● ContentResolver objects provide the create, retrieve, update and delete (aka CRUD) methods for persistent data storage ● Once paired, ContentProvider and ContentResolver objects handle inter-process communicationhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 21. Segments of a Content URI ● Suppose the URI is content:// com.google.provider.NotePad/notes/12 ● content:// com.google.provider.NotePad is the authority string used to locate the provider in the provider registry ● notes and 12 are path segments – notes is the tables path – 12 is a record reference in the table ● Path segments are ContentProvider-specific and defined by the ContentProvider data logic ● ContentProviders should have a separate path for each table they exposehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 22. Sample Content URIs ● content://contacts/people/ ● content://contacts/people/12 ● These URIs do not have fully qualified names because they are built-in ● All third-party content providers are recommended to have fully qualified pathshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 23. ContentProvider Resolution ● In the ContentProvider model, data are manipulated only through URIs ● URIs are parsed by ContentResolvers ● Upon parsing the URI, a given ContentResolver object compares the authority extracted from the URI to the table of ContentProviders registered in the ecosystemhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 24. ContentProvider Resolution ● In the ContentProvider model, data are manipulated only through URIs ● URIs are parsed by ContentResolvers ● Upon parsing the URI, a given ContentResolver object compares the authority extracted from the URI to the table of ContentProviders registered in the ecosystemhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 25. References ● http://developer.android.com/guide/topics/providers/content-providers.html ● http://tools.ietf.org/html/rfc2046 ● http://www.iana.org/assignments/media-types ● https://github.com/VKEDCO/SyncSQLiteBookDbApp_00001http://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×