Don't Make These 2 Mistakes With Lists in Kotlin

Поділитися
Вставка
  • Опубліковано 3 гру 2024

КОМЕНТАРІ • 47

  • @xybnedasdd2930
    @xybnedasdd2930 Рік тому +8

    Would be good to mention the more "Kotliny" way of locking by using a mutex, since technically locking means blocking the thread, and also synchronized will not work when inside a coroutine.

  • @andi6271
    @andi6271 Рік тому +4

    hi Philipp, i would like to see a video about your story. How you became developer, how you got better and became such knowledgeable and proficient. Something about your mindset, your daily routine when you were at the beginning, 3 years into coding, 7 years and now. Thank you for everything you do :)

  • @saabzero
    @saabzero Рік тому +26

    Good video, however i don't think the issue presented here is specific to lists, race conditions can also happen with any kind of mutable data.

    • @PhilippLackner
      @PhilippLackner  Рік тому +2

      True :)

    • @vibovitold
      @vibovitold Рік тому

      But collections in particular - that's a textbook example. Although a collection doesn't have to be a List.
      In fact, this example really calls for a Map, since the messages are indexed - looking an index up with indexOfFirst every time is a bit iffy. Could be a LinkedHashMap, if the order matters. I know it was only meant to illustrate the concept; consider it as a side remark, not criticism

  • @michaels891
    @michaels891 Рік тому +1

    Дякую за чудове пояснення.

  • @haykmkrtchyan7093
    @haykmkrtchyan7093 4 місяці тому

    For this scenarios I would use CopyOnWriteArrayList that already utilizes synchronized blocks for every write operation under the hood

  • @StingerAJ
    @StingerAJ Рік тому +8

    I think a critical race condition that would break list-indices in the first Chatmanager example-code is only theoretical unless you add code to delete messages or change the order of messages in the list. Right now with only adding, indices are stable and working on them with multiple threads should not introduce any bug. Please correct me if I am wrong (but please give a reason and explain).

    • @AyodeleKehinde-j5r
      @AyodeleKehinde-j5r Рік тому +2

      I agree since adding to the list will only append. Indices will break if messages are inserted by index and also by removed as you mentioned. So the first chat manager looks fine.

  • @lindaporsius
    @lindaporsius Рік тому +1

    Hi Philipp. This is very interesting. I would have liked, if you run the code to see exactly where race conditions are at work.
    Maybe a follow up tutorial?

  • @toyboatt
    @toyboatt Рік тому

    Love this time format. More please!

  • @cazewonda
    @cazewonda Рік тому

    This is a good idea although synchronization comes with performance cost in increased latency. Theres also possibilities of deadlocks and resource contention.
    I'll be working on a small personal project to see how dealing with this in the real sense gets.

  • @KCIsMe
    @KCIsMe Рік тому +3

    In this case if you're only ever adding to the end of the list and we're using a unique ID to find the index, then the index will always refer to same element, won't it? If there is no other code modifying the list I don't think we'd see a bug.
    A race condition that results in a bug could occur if we were moving elements around or deleting elements.
    Edit: I see that @StingerAJ pointed out the same thing.

  • @manuelsalas6190
    @manuelsalas6190 Рік тому

    Thanks Philipp, great content!! is it there a way to test the case when the race conditions happen?

  • @bro_chenzox
    @bro_chenzox Рік тому

    I see huge progress in the insertion of advertising content

  • @ИльяШелковенко

    Thank you, for video. I think you could also mention about CopyOnWriteArrayList, that preferable for this kind of operations )

  • @robchr
    @robchr Рік тому

    Can you also show how coroutine channels can be used to synchronize changes as well?

  • @507cHoco
    @507cHoco Рік тому

    Good video, and is the main reason why I use Mutex objects to lock on coroutines

  • @zeeshansyed4654
    @zeeshansyed4654 11 місяців тому

    Question regarding the synchronized.
    Does it not execute or add the execution in queue?

  • @saeedghiassy5435
    @saeedghiassy5435 Рік тому

    Hello, would you turn on "Thank you" option as well so we can contribute to your videos as well ? thanks :)

  • @safionweb
    @safionweb Рік тому +3

    Content we pay our internet bill for :)

  • @koweratus
    @koweratus Рік тому

    whats your android studio theme?

  • @oscarescam
    @oscarescam Рік тому

    What theme you use ?

  • @nikolozlatsabidze2196
    @nikolozlatsabidze2196 Рік тому

    i would personally go with AtomicReference or Volatile

  • @ddstudio1580
    @ddstudio1580 Рік тому

    will this method work for methods marked as suspend? I think it's better to use Muxed

  • @NaorBK
    @NaorBK Рік тому

    thanks for sharing

  • @sasha-f9x8h
    @sasha-f9x8h Рік тому

    Sir, Android 11 factory reset data can recover?

  • @byaruhaf
    @byaruhaf Рік тому

    I'm confused about why the index in `onMessageDeliveryStateChanged` would not be valid if all we are doing is adding to the message list. Are indices reevaluated every time a list is updated?

  • @sebastiankupis4448
    @sebastiankupis4448 Рік тому

    synchronized(this) should also do the job

  • @ejtravel3034
    @ejtravel3034 Рік тому

    Why not use volatile annotation over the messages list?

  • @addyi89
    @addyi89 Рік тому

    I use kotlins Mutex concept not javas syncronized in that case but both should work ❤

    • @mark-147
      @mark-147 Рік тому

      But Muted would mean you have to use suspend funs

    • @addyi89
      @addyi89 Рік тому

      true but that is my many case that courses concurrency issues.

  • @manuelsilveriof
    @manuelsilveriof Рік тому

    wouldn't be better to use a State Flow instead or a Shared State Flow.

  • @vahekhachaturian2424
    @vahekhachaturian2424 Рік тому

    Is this going to work if you were using coroutines?

    • @Denis0Kor
      @Denis0Kor Рік тому

      No, it won't work with coroutines, but you can find corresponding manual in official Kotlin docs named "Shared mutable state and concurrency".

  • @dandamiano
    @dandamiano Рік тому +1

    Good

  • @Carlos-xz9zq
    @Carlos-xz9zq Рік тому +1

    Hi Phillip, idk what happened in the last videos but the audio quality dropped, your voice sounds a little bit weird

  • @pqsk
    @pqsk Рік тому

    I would avoid synchronized with Kotlin.

  • @icodethis
    @icodethis Рік тому

    goat

  • @antonko1717
    @antonko1717 Рік тому +1

    Philipp you are doing great content but something dies inside of me when I see those thumbnails >_

  • @m77mo65
    @m77mo65 Рік тому

    Deadlock...