Firstly, itβs a bad practice to block a non-private object, because something else can block it, and then things go down, instead block a private member object, something like
Class Class1 Private lockObject as New Object Public Sub Clear() SyncLock lockObject ... End Synclock End Sub End Class
Now, for the actual question: if every operation you do does not change both lists (doubtful), you should have one lock on the list. Although you can use one lock object to indicate "I'm doing something with a list", it makes no sense to block a method in another thread that does not work in the same list as yours and will just slow down everything.
So, in short: use one lock object for each set of locks (locks for working in list1, locks for list 2, etc.). This is a bit more code for sections running on both lists, but the code will be more efficient.
As well as a general note: hold the lock as little time as possible. The less time you spend in the castle, the less likely it is that another stream will come and be blocked until you finish.
The MSDN page on SyncLock can also be read; it contains this information and a few examples. And @BasicLife is correct, always make sure that locks are in the same order everywhere.
And the general rule in the whole structure, if you do not refer to the static member of the class, unless otherwise specified, it is not thread safe. Therefore, when you work in lists, you need to block when you add, delete, clear or list the list, I am sure that there are others, but they are the most common.
Matt sieker
source share