Object Not Locked Message When Calling RecyclerView.NotifyAll()

Nathan Sokalski 4,111 Reputation points
2022-10-23T20:22:21+00:00

I have a RecyclerView & several Slider(s). I want to update the RecyclerView items when the user moves the Slider. I have created the OnValueChange for the Slider(s). This contains a call to the NotifyAll() method of the RecyclerView, but when I call this I receive the following Exception:

Java.Lang.RuntimeException
object not locked by thread before notify()

This happens at the beginning of my app because I call the OnValueChange method/handler explicitly to set the Slider(s) to their desired initial values. I have never worked with locking stuff before. What should I do?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-10-24T05:53:09.19+00:00

    Hello,

    This issue is caused by your code not meeting the execution conditions of the notify() or notifyAll() methods.

    You could find the following conditions via referring to the official documentation: notify.

    This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
    By executing a synchronized instance method of that object.
    By executing the body of a synchronized statement that synchronizes on the object.
    For objects of type Class, by executing a synchronized static method of that class.

    Therefore, you need to lock the object it before calling NotifyAll() in C#.

    Please refer to lock statement (C# reference) to get more details.

    Update:

    This should be due to an error thrown by the compiler.

    When you use the RecycleView.Adapter, if the adapter gets an address different from the actual address when initializing, the adapter mistakenly thinks that there are multiple different threads operating it, which will cause this error to cause no matter what object we lock.

    In fact, the use case of notifyall is not for data updates, but for waking up threads, see:

    The notifyAll() method of thread class is used to wake up all threads. This method gives the notification to all waiting threads of a particular object.

    Please use the notifyDataSetChanged method to update the dataset, it is the most recommended way to do it.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.