I know in C# when you have an object you want to use as a lock for multi-threading, you should declare it as static inside of a class, which the class instance will be running in a separate thread.
Does this hold true for Java as well? Some examples online seem to declare the lock object as only final…
Edit:
I have a resource that I want to limit to only one thread access at a time. A class that extends Thread will be used to create multiple instances and started at the same time. What should I use?
Thanks.
Depends on in which context they are to be used. If you want a per-instance lock, then leave
staticaway. If you want a per-class lock, then usestatic. Further indeed keep itfinal.