舉報

會員
Learning Concurrency in Kotlin
Theprimaryrequirementsofmodern-dayapplicationsarescalability,speed,andmakingthemostuseofhardware.Kotlinmeetstheserequirementswithitsimmensesupportforconcurrency.ManyconcurrentprimitivesofKotlin,suchaschannelsandsuspendingfunctions,aredesignedtobenon-blockingandefficient.Thisallowsfornewapproachestoconcurrencyandcreatesuniquechallengesforthedesignandimplementationofconcurrentcode.LearningConcurrencyinKotlinaddressesthosechallengeswithreal-lifeexamplesandexercisesthattakeadvantageofKotlin'sprimitives.BeginningwithanintroductiontoKotlin'scoroutines,youwilllearnhowtowriteconcurrentcodeandunderstandthefundamentalconceptsneededtobeabletowritemultithreadedsoftwareinKotlin.You'llexplorehowtocommunicatebetweenandsynchronizeyourthreadsandcoroutinestowriteasynchronousapplicationsthatarecollaborative.You'llalsolearnhowtohandleerrorsandexceptions,aswellashowtoleveragemulti-coreprocessing.Inadditiontothis,you’lldelveintohowcoroutinesworkinternally,allowingyoutoseethebiggerpicture.Throughoutthebookyou'llbuildanAndroidapplication–anRSSreader–designedandimplementedaccordingtothedifferenttopicscoveredinthebook.
目錄(298章)
倒序
- 封面
- Title Page
- Copyright and Credits
- Learning Concurrency in Kotlin
- Packt Upsell
- Why subscribe?
- PacktPub.com
- Contributors
- About the author
- About the reviewer
- Packt is searching for authors like you
- Preface
- Who this book is for
- What this book covers
- To get the most out of this book
- Download the example code files
- Conventions used
- Get in touch
- Reviews
- Hello Concurrent World!
- Processes threads and coroutines
- Processes
- Threads
- Coroutines
- Putting things together
- Introduction to concurrency
- Concurrency is not parallelism
- CPU-bound and I/O-bound
- CPU-bound
- I/O-bound
- Concurrency versus parallelism in CPU-bound algorithms
- Single-core execution
- Parallel execution
- Concurrency versus parallelism in I/O-bound algorithms
- Why concurrency is often feared
- Race conditions
- Atomicity violation
- Deadlocks
- Livelocks
- Concurrency in Kotlin
- Non-blocking
- Being explicit
- Readable
- Leveraged
- Flexible
- Concepts and terminology
- Suspending computations
- Suspending functions
- Suspending lambdas
- Coroutine dispatcher
- Coroutine builders
- Summary
- Coroutines in Action
- Downloading and installing Android Studio
- Creating a Kotlin project
- Adding support for coroutines
- Android's UI thread
- CalledFromWrongThreadException
- NetworkOnMainThreadException
- Requesting in the background updating in the UI thread
- Creating a thread
- CoroutineDispatcher
- Attaching a coroutine to a dispatcher
- Starting a coroutine with async
- Starting a coroutine with launch
- Using a specific dispatcher when starting the coroutine
- Adding networking permissions
- Creating a coroutine to call a service
- Adding UI elements
- What happens when the UI is blocked
- Displaying the amount of news that were processed
- Using a UI dispatcher
- Platform-specific UI libraries
- Adding the dependency
- Using Android's UI coroutine dispatcher
- Creating an asynchronous function to hold the request... or not
- A synchronous function wrapped in an asynchronous caller
- An asynchronous function with a predefined dispatcher
- An asynchronous function with a flexible dispatcher
- How to decide which option is better
- Summary
- Life Cycle and Error Handling
- Job and Deferred
- Job
- Exception handling
- Life cycle
- New
- Active
- Canceling
- Cancelled
- Completed
- Determining the current state of a Job
- Deferred
- Exception handling
- States move in one direction only
- A note on final states
- RSS – Reading from multiple feeds concurrently
- Supporting a list of feeds
- Creating a thread pool
- Fetching the data concurrently
- Merging the responses
- Testing the concurrent requests
- Non-happy path – Unexpected crash
- Having deferred store the exception
- Don't ignore the exception!
- Summary
- Suspending Functions and the Coroutine Context
- Improving the UI of the RSS Reader
- Giving each feed a name
- Fetching more information about the articles from the feed
- Adding a scrollable list for the articles
- Layout for the individual articles
- Adapter to map the information
- Adding a ViewHolder
- Mapping the data
- onCreateViewHolder
- onBindViewHolder
- getItemCount
- Allowing the incremental addition of articles to the adapter
- Connecting the adapter to the activity
- Testing the new UI
- Sanitizing the data
- Suspending functions
- Suspending functions in action
- Writing a repository with async functions
- Upgrading to suspending functions
- Suspending functions versus async functions
- The coroutine context
- Dispatcher
- CommonPool
- Default dispatcher
- Unconfined
- Single thread context
- Thread pool
- Exception handling
- Non-cancellable
- More about contexts
- Mixing contexts
- Combining contexts
- Separating contexts
- Temporary context switch using withContext
- Summary
- Iterators Sequences and Producers
- Suspendable sequences and iterators
- Yielding values
- Iterators
- Interacting with an iterator
- Going through all the elements
- Getting the next value
- Validating whether there are more elements
- Calling next() without validating for elements
- A note on the inner working of hasNext()
- Sequences
- Interacting with a sequence
- Reading all the elements in the sequence
- Obtaining a specific element
- elementAt
- elementAtOrElse
- elementAtOrNull
- Obtaining a group of elements
- Sequences are stateless
- Suspending Fibonacci
- Writing a Fibonacci sequence
- Writing a Fibonnaci iterator
- Producers
- Creating a producer
- Interacting with a producer
- Reading all the elements in the producer
- Receiving a single element
- Taking a group of elements
- Taking more elements than those available
- Suspending a Fibonacci sequence using a producer
- Producers in action
- Having the adapter request more articles
- Creating a producer that fetches feeds on demand
- Adding the articles to the list on the UI
- Summary
- Channels - Share Memory by Communicating
- Understanding channels
- Use case – streaming data
- Use case – distributing work
- Types of channels and backpressure
- Unbuffered channels
- RendezvousChannel
- Buffered channels
- LinkedListChannel
- ArrayChannel
- ConflatedChannel
- Interacting with channels
- SendChannel
- Validating before sending
- Sending elements
- Offering elements
- On channel closed
- On channel full
- On channel open and not full
- ReceiveChannel
- Validating before reading
- isClosedForReceive
- isEmpty
- Channels in action
- Adding a search activity
- Adding the search function
- Implementing the collaborative search
- Connecting the search functions
- Updating ArticleAdapter
- Displaying the results
- Summary
- Thread Confinement Actors and Mutexes
- Atomicity violation
- What atomicity means
- Thread confinement
- What is thread confinement?
- Confining coroutines to a single thread
- Actors
- What is an actor?
- Creating an actor
- Using actors to extend the functionality
- More on actor interaction
- Buffered actors
- Actor with CoroutineContext
- CoroutineStart
- Mutual exclusions
- Understanding mutual exclusions
- Creating mutexes
- Interacting with mutual exclusions
- Volatile variables
- Thread cache
- @Volatile
- Why @Volatile doesn't solve thread-safe counters
- When to use @Volatile
- Atomic data structures
- Actors in action
- Adding the label to the UI
- Creating an actor to use as a counter
- Increasing the counter as results are loaded
- Adding a channel so that the UI reacts to updates
- Sending the updated value through the channel
- Updating the UI on changes
- Testing the implementation
- Extending the actor to allow for resetting the counter
- Resetting the counter upon new searches
- Summary
- Testing and Debugging Concurrent Code
- Testing concurrent code
- Throwing away assumptions
- Focus on the forest not the trees
- Writing Functional Tests
- More advice on tests
- Writing the tests
- Creating a flawed UserManager
- Adding the kotlin-test library
- Adding a happy path test
- Testing for an edge case
- Identifying the issue
- Fixing the crash
- Retesting
- Debugging
- Identifying a coroutine in the logs
- Using automatic naming
- Setting a specific name
- Identifying a coroutine in the debugger
- Adding a debugger watch
- Conditional breakpoint
- Resiliency and stability
- Summary
- The Internals of Concurrency in Kotlin
- Continuation Passing Style
- Continuations
- The suspend modifier
- State machine
- Labels
- Continuations
- Callbacks
- Incrementing the label
- Storing the result from the other operations
- Returning the result of the suspending computation
- Context switching
- Thread switching
- ContinuationInterceptor
- CoroutineDispatcher
- CommonPool
- Unconfined
- Android's UI
- DispatchedContinuation
- DispatchedTask
- Recap
- Exception handling
- The handleCoroutineException() function
- CoroutineExceptionHandler
- CancellationException
- Cancelling the job
- Platform specific logic
- JVM
- JavaScript
- Summary
- Other Books You May Enjoy
- Leave a review - let other readers know what you think 更新時間:2021-08-05 10:47:31
推薦閱讀
- UI圖標創意設計
- Oracle Exadata性能優化
- Data Analysis with Stata
- Unity 5 for Android Essentials
- 0 bug:C/C++商用工程之道
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- INSTANT Yii 1.1 Application Development Starter
- Hadoop 2.X HDFS源碼剖析
- PHP與MySQL權威指南
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- Android移動應用開發項目教程
- Python物理建模初學者指南(第2版)
- Python編程入門(第3版)
- Java語言程序設計實用教程(第2版)
- C++服務器開發精髓
- Ubuntu Server Cookbook
- Processing開發實戰
- Managing Windows Servers with Chef
- Python程序設計教程
- Practical Linux Security Cookbook
- HTML5+CSS3從入門到精通(微課精編版)
- WooCommerce Cookbook
- C#大學實用教程
- C語言程序設計教程(微課版)
- Python機器學習核心算法編程實例
- Rust編程之道
- Python網絡爬蟲與數據分析從入門到實踐
- 零基礎看圖學Scratch 3.0少兒趣味編程(全彩大字版)
- Python數據挖掘入門與實踐(第2版)
- Gambas高級程序設計:基于國產操作系統