舉報

會員
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.
最新章節
- Leave a review - let other readers know what you think
- Other Books You May Enjoy
- Summary
- JavaScript
- JVM
- Platform specific logic
品牌:中圖公司
上架時間:2021-08-05 10:29:53
出版社:Packt Publishing
本書數字版權由中圖公司提供,并由其授權上海閱文信息技術有限公司制作發行
- Leave a review - let other readers know what you think 更新時間:2021-08-05 10:47:31
- Other Books You May Enjoy
- Summary
- JavaScript
- JVM
- Platform specific logic
- Cancelling the job
- CancellationException
- CoroutineExceptionHandler
- The handleCoroutineException() function
- Exception handling
- Recap
- DispatchedTask
- DispatchedContinuation
- Android's UI
- Unconfined
- CommonPool
- CoroutineDispatcher
- ContinuationInterceptor
- Thread switching
- Context switching
- Returning the result of the suspending computation
- Storing the result from the other operations
- Incrementing the label
- Callbacks
- Continuations
- Labels
- State machine
- The suspend modifier
- Continuations
- Continuation Passing Style
- The Internals of Concurrency in Kotlin
- Summary
- Resiliency and stability
- Conditional breakpoint
- Adding a debugger watch
- Identifying a coroutine in the debugger
- Setting a specific name
- Using automatic naming
- Identifying a coroutine in the logs
- Debugging
- Retesting
- Fixing the crash
- Identifying the issue
- Testing for an edge case
- Adding a happy path test
- Adding the kotlin-test library
- Creating a flawed UserManager
- Writing the tests
- More advice on tests
- Writing Functional Tests
- Focus on the forest not the trees
- Throwing away assumptions
- Testing concurrent code
- Testing and Debugging Concurrent Code
- Summary
- Resetting the counter upon new searches
- Extending the actor to allow for resetting the counter
- Testing the implementation
- Updating the UI on changes
- Sending the updated value through the channel
- Adding a channel so that the UI reacts to updates
- Increasing the counter as results are loaded
- Creating an actor to use as a counter
- Adding the label to the UI
- Actors in action
- Atomic data structures
- When to use @Volatile
- Why @Volatile doesn't solve thread-safe counters
- @Volatile
- Thread cache
- Volatile variables
- Interacting with mutual exclusions
- Creating mutexes
- Understanding mutual exclusions
- Mutual exclusions
- CoroutineStart
- Actor with CoroutineContext
- Buffered actors
- More on actor interaction
- Using actors to extend the functionality
- Creating an actor
- What is an actor?
- Actors
- Confining coroutines to a single thread
- What is thread confinement?
- Thread confinement
- What atomicity means
- Atomicity violation
- Thread Confinement Actors and Mutexes
- Summary
- Displaying the results
- Updating ArticleAdapter
- Connecting the search functions
- Implementing the collaborative search
- Adding the search function
- Adding a search activity
- Channels in action
- isEmpty
- isClosedForReceive
- Validating before reading
- ReceiveChannel
- On channel open and not full
- On channel full
- On channel closed
- Offering elements
- Sending elements
- Validating before sending
- SendChannel
- Interacting with channels
- ConflatedChannel
- ArrayChannel
- LinkedListChannel
- Buffered channels
- RendezvousChannel
- Unbuffered channels
- Types of channels and backpressure
- Use case – distributing work
- Use case – streaming data
- Understanding channels
- Channels - Share Memory by Communicating
- Summary
- Adding the articles to the list on the UI
- Creating a producer that fetches feeds on demand
- Having the adapter request more articles
- Producers in action
- Suspending a Fibonacci sequence using a producer
- Taking more elements than those available
- Taking a group of elements
- Receiving a single element
- Reading all the elements in the producer
- Interacting with a producer
- Creating a producer
- Producers
- Writing a Fibonnaci iterator
- Writing a Fibonacci sequence
- Suspending Fibonacci
- Sequences are stateless
- Obtaining a group of elements
- elementAtOrNull
- elementAtOrElse
- elementAt
- Obtaining a specific element
- Reading all the elements in the sequence
- Interacting with a sequence
- Sequences
- A note on the inner working of hasNext()
- Calling next() without validating for elements
- Validating whether there are more elements
- Getting the next value
- Going through all the elements
- Interacting with an iterator
- Iterators
- Yielding values
- Suspendable sequences and iterators
- Iterators Sequences and Producers
- Summary
- Temporary context switch using withContext
- Separating contexts
- Combining contexts
- Mixing contexts
- More about contexts
- Non-cancellable
- Exception handling
- Thread pool
- Single thread context
- Unconfined
- Default dispatcher
- CommonPool
- Dispatcher
- The coroutine context
- Suspending functions versus async functions
- Upgrading to suspending functions
- Writing a repository with async functions
- Suspending functions in action
- Suspending functions
- Sanitizing the data
- Testing the new UI
- Connecting the adapter to the activity
- Allowing the incremental addition of articles to the adapter
- getItemCount
- onBindViewHolder
- onCreateViewHolder
- Mapping the data
- Adding a ViewHolder
- Adapter to map the information
- Layout for the individual articles
- Adding a scrollable list for the articles
- Fetching more information about the articles from the feed
- Giving each feed a name
- Improving the UI of the RSS Reader
- Suspending Functions and the Coroutine Context
- Summary
- Don't ignore the exception!
- Having deferred store the exception
- Non-happy path – Unexpected crash
- Testing the concurrent requests
- Merging the responses
- Fetching the data concurrently
- Creating a thread pool
- Supporting a list of feeds
- RSS – Reading from multiple feeds concurrently
- A note on final states
- States move in one direction only
- Exception handling
- Deferred
- Determining the current state of a Job
- Completed
- Cancelled
- Canceling
- Active
- New
- Life cycle
- Exception handling
- Job
- Job and Deferred
- Life Cycle and Error Handling
- Summary
- How to decide which option is better
- An asynchronous function with a flexible dispatcher
- An asynchronous function with a predefined dispatcher
- A synchronous function wrapped in an asynchronous caller
- Creating an asynchronous function to hold the request... or not
- Using Android's UI coroutine dispatcher
- Adding the dependency
- Platform-specific UI libraries
- Using a UI dispatcher
- Displaying the amount of news that were processed
- What happens when the UI is blocked
- Adding UI elements
- Creating a coroutine to call a service
- Adding networking permissions
- Using a specific dispatcher when starting the coroutine
- Starting a coroutine with launch
- Starting a coroutine with async
- Attaching a coroutine to a dispatcher
- CoroutineDispatcher
- Creating a thread
- Requesting in the background updating in the UI thread
- NetworkOnMainThreadException
- CalledFromWrongThreadException
- Android's UI thread
- Adding support for coroutines
- Creating a Kotlin project
- Downloading and installing Android Studio
- Coroutines in Action
- Summary
- Coroutine builders
- Coroutine dispatcher
- Suspending lambdas
- Suspending functions
- Suspending computations
- Concepts and terminology
- Flexible
- Leveraged
- Readable
- Being explicit
- Non-blocking
- Concurrency in Kotlin
- Livelocks
- Deadlocks
- Atomicity violation
- Race conditions
- Why concurrency is often feared
- Concurrency versus parallelism in I/O-bound algorithms
- Parallel execution
- Single-core execution
- Concurrency versus parallelism in CPU-bound algorithms
- I/O-bound
- CPU-bound
- CPU-bound and I/O-bound
- Concurrency is not parallelism
- Introduction to concurrency
- Putting things together
- Coroutines
- Threads
- Processes
- Processes threads and coroutines
- Hello Concurrent World!
- Reviews
- Get in touch
- Conventions used
- Download the example code files
- To get the most out of this book
- What this book covers
- Who this book is for
- Preface
- Packt is searching for authors like you
- About the reviewer
- About the author
- Contributors
- PacktPub.com
- Why subscribe?
- Packt Upsell
- Learning Concurrency in Kotlin
- Copyright and Credits
- Title Page
- 封面
- 封面
- 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