Presentation is loading. Please wait.

Presentation is loading. Please wait.

V0.01 © 2009 Research In Motion Limited Introduction to Java Application Development for the BlackBerry Smartphone Trainer name Date.

Similar presentations


Presentation on theme: "V0.01 © 2009 Research In Motion Limited Introduction to Java Application Development for the BlackBerry Smartphone Trainer name Date."— Presentation transcript:

1 v0.01 © 2009 Research In Motion Limited Introduction to Java Application Development for the BlackBerry Smartphone Trainer name Date

2 © 2009 Research In Motion Limited Revision history Document versionDateUpdated byDescription of revision.01Brian WhiteInitial content creation

3 Agenda In this course, you will cover the following topics: –Introduction to Java® application development for mobile devices –Understanding Java APIs for mobile devices –Developing a user interface for mobile devices –Push technology for Java applications –Introduction to threading and background processing –Network communication for mobile devices –Testing and debugging an application API application programming Interface

4 Introduction to threading and background processing Objectives: –Describe multithreaded applications –Describe the Main Event thread –Describe background applications –Describe how to create and use threads –Describe the dangers of multithreaded applications

5 © 2009 Research In Motion Limited Multithreaded applications

6 –Traditionally, applications run in a single thread of execution: each task the application must perform during the course of its execution happens in a sequence, one task at a time. –Multithreaded applications can create multiple threads of execution (known as spawning a thread) that perform many tasks concurrently.

7 Multithreaded applications –On the left is an application performing three tasks in a single thread of execution. Each task is in succession and cannot start the next task before finishing the previous one. –On the right is an application performing the same tasks with multiple threads of execution. The application places each task in its own thread of execution simultaneously and returns the data when the task is complete while the main thread of execution (the main application) continues to operate.

8 Multithreaded applications Benefits: –Improved application responsiveness –Efficient multiprocessor use –Efficient system resource usage

9 Main Event thread

10 –Is the thread of execution in every Java® ME application developed for the BlackBerry® smartphone –Is the center point from which all other threads of execution are launched in the application –The Main Event thread is designated by calling the Application.enterEventDispatcher() method.

11 Main Event thread Example: public class SimpleApp extends UiApplication { public static void main(String[] args) { MyApp app = new MyApp(); app.enterEventDispatcher() } –Class inherited from UiApplication: Any GUI application must be inherited from the UiApplication class which itself is inherited from the Application class. Part of this inheritance is the inclusion of the enterEventDispatcher() method. –Use of enterEventDispatcher() method: The enterEventDispatcher() method is called in the main() method to assign event dispatching duties to this thread. The method is called on an instance of the class instantiated in the main() method.

12 Main Event thread Use the Main Event thread to: –Control the overall execution of the application –Serve as the main launching and collapsing point of all threads of execution in the complete application.

13 Background applications

14 –Run in a separate process that does not interfere with the other applications and processes running on the BlackBerry smartphone –Can theoretically perform any number of tasks; a common use is to listen for data being pushed to the device –Run as self-contained programs in separate processes

15 Creating and using threads

16 –Create runnable classes –Create and start threads –Suspend threads –Prioritize threads –Stop threads

17 Creating and using threads Create runnable classes: –For multiple inheritance in classes, the Java APIs provide either the Thread class or the Runnable interface. –The Runnable interface is the one method used to create a runnable class. Can inherit functionality from another fully formed class. An interface provides a framework that any classes that implement it must follow. This gives the developer much more flexibility in the classes they create. –The Thread class is easier to use, but limited. Can only use interfaces to lend additional functionality.

18 Creating and using threads Create and start threads: –Create and instance of the class as an object. 1.Ensure the class to be used is within the scope of the current class. 2.Declare an object variable. 3.Instantiate the object. –After a threaded class is properly instantiated into an object, it is ready for execution. –To begin execution call the start() method.

19 Creating and using threads Suspend threads: –After a thread of execution is stopped, it cannot be restarted. Threads of execution can be suspended and resumed. There are two methods for suspending thread execution. Thread suspension—Threads can be manually suspended and resumed. Thread sleeping—Threads can be suspended for a set amount of time using the sleep() method and passing the amount of time in milliseconds to suspend the thread. Wait and notify—The wait() and notify() methods work in conjunction to suspend and wake threads that require access to a group of objects.

20 Creating and using threads Prioritize threads: –Prioritize threads to ensure they are executed before other threads spawned by the application. –Prioritization applies only in the context of the current application. –A useful technique for ensuring that some threads are executed before others. –Thread prioritization is accomplished using the setPriority() method of the threaded object and passing a numeric value that represents the new priority. –Thread priority works on an ascending scale.

21 Creating and using threads Stop threads: –Although any threaded object provides a stop() method for ending execution, this method can sometimes cause problems in applications. –A better solution is to fashion code that interrupts the execution of the thread.

22 Dangers of multithreaded applications

23 –A TooManyThreadsError exception will be returned by an application that has reached either the application or operating system maximums for thread creation. This exception should be handled during any thread creation code. Refer to the relevant J2ME and BlackBerry smartphone operating system documentation for current maximums. –Long-running threads (such as those found in background applications) should occasionally be suspended so their resources can be freed up for other threads of execution.

24 Dangers of multithreaded applications –All threads of execution spawned by applications that are not designed to run in the background should be ended by the time the application itself ends. –To end a thread of execution use coded methods instead of the stop() method. The stop() method can cause damage to objects associated with the thread of execution, and even though the thread may exit, other parts of the application might still use these objects.

25 Summary –Multithreaded applications have the ability to create multiple threads of execution that perform many tasks in a concurrent fashion. –The BlackBerry smartphone operating system supports multithreading so developers can create responsive applications that best exploit the unique features of the operating environment. –Multithreaded applications provide the following key advantages: Improved application responsiveness Efficient multiprocessor use Efficient system resource usage

26 Summary –A background application is one that runs in a separate process that does not interfere with the other applications and processes running on the BlackBerry smartphone. –A runnable class can be created from the Thread class or the Runnable interface. The Runnable interface is preferable because it allows a developer to further inherit from another class. –Runnable classes expose the run() method to perform the threaded work. –Thread execution can be frozen through either thread suspension or thread sleeping. –It is preferable to stop thread execution through a coded method other than explicitly calling the stop() method. –There are many risks inherent in using multithreaded applications. Be aware of these risks during the development process.

27 © 2009 Research In Motion Limited

28 Legal Disclaimer The following trademarks and/or registered trademarks of Research In Motion are referred to in this presentation: –BlackBerry® –Research In Motion® (only include if it appears in the webinar/presentation) –RIM® (only include if it appears in the webinar/presentation) The following BlackBerry products are referred to in this presentation: (include all items from the official BlackBerry products list that appear in the webinar/presentation) –BlackBerry® Internet Service –BlackBerry® Enterprise Solution –BlackBerry® Enterprise Server –BlackBerry® Pearl™ 8100 smartphone –BlackBerry® Curve™ 8300 smartphone The following third-party trademarks are referred to in this presentation: (include all third-party products that appear in the webinar/presentation) –Microsoft® Excel® –Microsoft® SQL Server® Please remove any products not mentioned in this presentation and add all mentioned products before completing this presentation. Also, remove any remaining red text. © 2009 Research In Motion Limited

29 Legal Disclaimer © 2009 Research In Motion Limited. All rights reserved. BlackBerry®, RIM®, Research In Motion®, SurePress™ SureType® and related trademarks, names and logos are the property of Research In Motion Limited and are registered and/or used in the U.S. and countries around the world. All other trademarks are the property of their respective owners. This documentation is provided "AS IS" and without condition, endorsement, guarantee, representation or warranty, or liability of any kind by Research In Motion Limited and its affiliated companies, all of which are expressly disclaimed to the maximum extent permitted by applicable law in your jurisdiction.

30 © 2009 Research In Motion Limited Revision history Document versionDateUpdated byDescription of revision


Download ppt "V0.01 © 2009 Research In Motion Limited Introduction to Java Application Development for the BlackBerry Smartphone Trainer name Date."

Similar presentations


Ads by Google