Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Brief history of concurrency

Brief history of concurrency

What is concurrency, multithreading, parallelism?

Dmytro Danylyk

September 17, 2019
Tweet

More Decks by Dmytro Danylyk

Other Decks in Programming

Transcript

  1. At the very beginning... - computers did not have operating

    systems - computers were designed to execute a single program from beginning to end - each program had access to all of the machine’s resources
  2. Over time... Operating systems evolved to allow multiple programs to

    execute at once, each within a process — an independently isolated program that is assigned resources like memory, file handles and security controls.
  3. Over time... The same factors which motivated the development of

    processes, motivated the development of concurrency.
  4. What is concurrency? In computer science, concurrency is the ability

    of different parts or units of a program to be executed out-of-order or in partial order, without affecting the final outcome. Concurrency is achieved via multithreading. iconst_0 ... iload_2 ... iadd ... iconst_5 ... ... goto 4 ... iinc
  5. What is multithreading? When multiple threads execute bytecode instruction sequences

    in the same program, that action is known as multithreading. iconst_0 ... iload_2 ... iadd ... iconst_5 ... ... goto 4 ... iinc Thread 2 Thread 1
  6. Why do we need multithreading? - Resource sharing - Responsiveness

    - Utilization of Multiprocessor Architecture
  7. Why do we need multithreading? - Resource sharing - Responsiveness

    - Utilization of Multiprocessor Architecture
  8. Why do we need multithreading? - Resource sharing - Responsiveness

    - Utilization of Multiprocessor Architecture
  9. How multithreading is achieved? In single-processor systems, only a single

    thread of execution occurs at a given instant. The CPU quickly switches back and forth between several threads to create the illusion that the threads are executing at the same time. CPU I Thread 1 Thread 2 Thread 1 Thread 2 context switching
  10. How multithreading is achieved? On multiprocessor systems, several threads do,

    in fact, execute at the same time. CPU I Thread 1 CPU II Thread 2 CPU III Thread 3 parallelism
  11. What is a “thread”? A thread is an execution context,

    which is all the information CPU needs to execute an independent sequence of instructions. Executes as an independent sequence of instructions. val runnable = Runnable { val a = 1 val b = 2 println(a + b) } Thread(runnable).start() Java 1.1.4
  12. What is “main” and “UI” thread? - Thread which is

    executed when your program starts is called main thread - Thread which is used to work with GUI is called UI thread - On Android main and UI thread are the same thing
  13. What is “main” and “UI” thread? - Thread which is

    executed when your program starts is called main thread - Thread which is used to work with GUI is called UI thread - On Android main and UI thread are the same thing
  14. What is “main” and “UI” thread? - Thread which is

    executed when your program starts is called main thread - Thread which is used to work with GUI is called UI thread - On Android main and UI thread are the same thing
  15. Why there is only one “UI” thread? - Creating a

    thread for every UI update is expensive - By design, Android View objects are not thread-safe
  16. Why there is only one “UI” thread? - Creating a

    thread for every UI update is expensive - By design, Android View objects are not thread-safe
  17. Android “main” thread Looper Message Queue Message 2 Message 1

    Message 3 Message 4 Message 2 Message 1 Handler 1 Handler 2 for(;;) Thread 2 Thread 1 Main Thread
  18. The issues with threads - java threads are one-time use

    only - creating a thread takes time and resources - creating too many threads at the same time can cause the system to run out of memory
  19. The issues with threads - java threads are one-time use

    only - creating a thread takes time and resources - creating too many threads at the same time can cause the system to run out of memory
  20. The issues with threads - java threads are one-time use

    only - creating a thread takes time and resources - creating too many threads at the same time can cause the system to run out of memory
  21. Thread Pools A thread pool reuses previously created threads to

    execute current tasks. Task Queue Task 2 Task 1 Task 3 Task 4 Thread 2 Thread 1 Task 2 Task 1 Thread 3 Thread 4 running running idle idle Java 1.5
  22. Example of a thread pool val executor: ExecutorService = Executors.newFixedThreadPool(2)

    val task1 = Runnable { println("Task 1") } executor.execute(task1) val task2 = Runnable { println("Task 2") } executor.execute(task2) Executes as an independent sequence of instructions.
  23. Types of thread pools. - Fixed Thread Pool - Cached

    Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)
  24. Types of thread pools. - Fixed Thread Pool - Cached

    Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)
  25. Types of thread pools. - Fixed Thread Pool - Cached

    Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)
  26. Types of thread pools. - Fixed Thread Pool - Cached

    Thread Pool (unbound, thread has 60 seconds lifetime) - Scheduled Thread Pool - ForkJoinPool (Java 7, used in Coroutines, all threads in the pool attempt to find and execute subtasks created by other active tasks)
  27. Coroutines A coroutine — is an instance of suspendable computation

    not bound to any particular thread. It may suspend its execution in one thread and resume in another one. Process→Thread (lightweight process)→Coroutine (lightweight thread)
  28. I/O-bound and CPU-bound operations I/O operations - reading or writing

    to a file, database, or network - involves a lot of waiting for other hardware systems to complete their work - the upper bound for thread pool is fairly large (64 threads or unbound) CPU-intensive operations - compressing or decompressing, encryption or decryption in-memory data, image transformation - CPU won’t pause the execution as it does for an I/O operation - the upper bound for thread pool is to the number of cores the CPU has (and thus the number of threads it can run in parallel)
  29. I/O-bound and CPU-bound operations I/O operations - reading or writing

    to a file, database, or network - involves a lot of waiting for other hardware systems to complete their work - the upper bound for thread pool is fairly large (64 threads or unbound) CPU-intensive operations - compressing or decompressing, encryption or decryption in-memory data, image transformation - CPU won’t pause the execution as it does for an I/O operation - the upper bound for thread pool is to the number of cores the CPU has (and thus the number of threads it can run in parallel)
  30. I/O-bound and CPU-bound operations I/O operations - reading or writing

    to a file, database, or network - involves a lot of waiting for other hardware systems to complete their work - the upper bound for thread pool is fairly large (64 threads or unbound) CPU-intensive operations - compressing or decompressing, encryption or decryption in-memory data, image transformation - CPU won’t pause the execution as it does for an I/O operation - the upper bound for thread pool is to the number of cores the CPU has (and thus the number of threads it can run in parallel)
  31. Remember It doesn’t matter what you use for concurrency, under

    the hood it’s still treads. Thread Thread Pools Loaders RxJava AsyncTask Coroutines Intent Service Job Scheduler