Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working in the Background Radan Ganchev Astea Solutions.

Similar presentations


Presentation on theme: "Working in the Background Radan Ganchev Astea Solutions."— Presentation transcript:

1 Working in the Background Radan Ganchev Astea Solutions

2 Content Threads Services Handlers Async Tasks

3 But first... some questions

4 Which of you... know the difference between a process and a thread?

5 Which of you... have ever worked with threads in Java?

6 Which of you... know that every object in Java has a lock?

7 Which of you... know what a synchronized block/method is?

8 Which of you... relate the phrase "dining philosophers" to multithreading?

9 Which of you... have ever worked with the java.util.concurrent API?

10 Which of you... know what Executors do to ThreadPools?

11 Threads

12 What is a thread? ‣ Means for parallel execution of tasks in a single process ‣ “A light-weight process”

13 What do we think of when using threads? Threads ‣ Doing things in parallel

14 What do we think of when using threads? Threads ‣ Doing things in parallel ‣ Synchronization

15 Creating Threads Inherit java.lang.Thread new Thread() { @Override public void run() { // Do cool stuff } }.start();

16 Creating Threads Implement java.lang.Runnable Runnable task = new Runnable() { @Override public void run() { // Do cool stuff } }; new Thread(task).start();

17 Android & Threads The main thread (UI thread) ‣ All activities, services and broadcast receivers are started in it, as well as the application itself ‣ Handles all UI manipulations Background threads - nothing unusual

18 DON’T BLOCK THE UI THREAD!!!

19 Android & Threads Five easy ways to block the UI thread: 1. Obvious mistake E.g. infinite loop/recursion 2. Perform long-running operations E.g. download resources from the Internet 3. Use blocking methods E.g. thread.join() or object.wait() 4. Make a heavy view hierarchy 5. Synchronize another thread on Activity.this

20 Services

21 What is a service? “A Service is an application component that can perform long-running operations in the background and does not provide a user interface.” Services always run in the UI thread! A service can be bound or started

22 Services bind vs. start What is called when? A service can be both bound and started startServiceonStartCommand bindServiceonBind

23 Services - bind & start

24 Creating a Service 1. Extend android.app.Service Override callback methods Start worker threads etc. 2. Declare your service in AndroidManifest.xml...

25 Using a Service What is your Intent ? Binding ‣ You get a reference in a callback ‣ The service is alive while you are alive Starting ‣ The service runs independently ‣ You just send it a command

26 AIDL Android Interface Definition Language “Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service.” Declares the communication interface between the client and the service Java interface-like syntax

27 Handlers

28 What they do? ‣ Bind to the thread they are created in. ‣ Allow us to send messages or post Runnable s from one thread to another. ‣ Allow us to perform delayed operations.

29 Handlers How they do it? Handler h = new Handler() {...}; Message m =...; h.sendMessage(m) h.handleMessage(m) Runnable r =...; h.post(m) r.run()

30 Async Tasks

31 “AsyncTask enables proper and easy use of the UI thread.” A simple alternative of background threads which takes care of synchronization for us.

32 AsyncTask UI Thread task.execute() task.doInBackground() publishProgress(); onPreExecute() return result; onProgressUpdate() onPostExecute(result) Task’s background thread

33 Q & A Questions? Feedback section: ‣ Did you hear well? ‣ Was there anything you didn’t understand? ‣ What would you like changed in our next lecture?

34 Resources In this lecture: ‣ Processes and Threads Processes and Threads ‣ Services Tutorial Services Tutorial ‣ Service Service ‣ Handler Handler ‣ AsyncTask AsyncTask

35 Resources Background reading ‣ Java Concurrency Tutorial Java Concurrency Tutorial ‣ java.util.concurrent package summary java.util.concurrent package summary ‣ Explore the resources of the following FMI courses: ‣ Operating Systems ‣ Concurrent Programming


Download ppt "Working in the Background Radan Ganchev Astea Solutions."

Similar presentations


Ads by Google