Android Programming Lecture 8 Multi-threading
Android Programming Lecture 8 Multi-threading
Timesharing Operating Systems All modern operating systems are capable of sharing processor(s)between multiple users and processes o Process scheduling algorithms There are often lots (hundreds)of processes running o Many are managed by the system o Some are user executed Adobe
Timesharing Operating Systems • All modern operating systems are capable of sharing processor(s) between multiple users and processes o Process scheduling algorithms • There are often lots (hundreds) of processes running o Many are managed by the system o Some are user executed 2
Process A process is typically defined as execution of a single program on a computing device o Processes are scheduled by the OS o Processes are owned by a user or the OS(root) o Processes may have a long lifetime (a web server)or a short lifetime(one run of a short program) o Processes do not share memory between each other o Most OS support inter-process communication 路
Process • A process is typically defined as execution of a single program on a computing device o Processes are scheduled by the OS o Processes are owned by a user or the OS (root) o Processes may have a long lifetime (a web server) or a short lifetime (one run of a short program) o Processes do not share memory between each other o Most OS support inter-process communication 3
Threads A thread is a component of a process o Usually considered the smallest unit that can be scheduled by an OS o Can share memory and instructions with other threads in the same process o Most OS support inter-thread communication
Threads • A thread is a component of a process o Usually considered the smallest unit that can be scheduled by an OS o Can share memory and instructions with other threads in the same process o Most OS support inter-thread communication 4
Process V.S.Thread Differenco betwveen process+ad one tread of contol 众Second https://www.youtube.com/watch?v=O3EyzlZxx3g
Process V.S. Thread https://www.youtube.com/watch?v=O3EyzlZxx3g 5
Timesharing Operating Systems On a single processor system, o"Round robin"scheduling system switches between threads and processes fast enough that it appears that multiple programs are running o Only one is every truly executing at a time On multi-core or multi-processor systems, o Threads or processes can be allocated across the processors True parallel processing o“Round-robin”scheduling still occurs 6
Timesharing Operating Systems • On a single processor system, o “Round robin” scheduling system switches between threads and processes fast enough that it appears that multiple programs are running o Only one is every truly executing at a time • On multi-core or multi-processor systems, o Threads or processes can be allocated across the processors ▪ True parallel processing o “Round-robin” scheduling still occurs 6
Android Android starts a new Linux process for the application with a single thread of execution All components of the same application run in the same process and thread 。This thread is called the“main”thread or“U"thread Any updates to the Ul/display must be accomplished on the main thread
Android • Android starts a new Linux process for the application with a single thread of execution • All components of the same application run in the same process and thread • This thread is called the “main” thread or “UI” thread • Any updates to the UI/display must be accomplished on the main thread 7
Slow Android App If a component of the work takes a long time,the rest of the work will be“blocked" For example,a long time to access data across the network prevents responding to any GUl events In the Android OS,if a GUl doesn't respond to an input event in five seconds,then it is considered unresponsive and the OS will try to kill it赳 SimpleApp is not responding. Would you like to close it? wal OK 6
Slow Android App • If a component of the work takes a long time, the rest of the work will be “blocked” • For example, a long time to access data across the network prevents responding to any GUI events • In the Android OS, if a GUI doesn’t respond to an input event in < five seconds, then it is considered unresponsive and the OS will try to kill it! 8
Multi-threading 。Put non-Ul work on a Android App separate thread UI For example,download an Main Thread image,connect to a remote server,open a web browser... Java Threads BG BG BG Background Thread 9
Multi-threading • Put non-UI work on a separate thread • For example, download an image, connect to a remote server, open a web browser… 9 UI Android App Java Threads BG BG BG Main Thread Background Thread
Android App What technique shall I use for my background thread Java Threads execution? BG BG Linux Process BG Application Application Time Start End 10
10 Application Start Application End Linux Process BG Time UI UI Android App Java Threads BG BG BG What technique shall I use for my background thread execution?