我有一类线程安全的公共代码.
该类中的一个方法是抽象的,需要为不同的实现重写.
我需要确保或至少向其他开发人员标记此方法的所有实现都需要是线程安全的.
做这个的最好方式是什么?
这个效果有关键字或注释吗?
我已经尝试过,abstract synchronized但不允许使用关键字组合.
现在,我不确定这是否是一个愚蠢的问题,如果是的话,请耐心等待.
锁定对象是"递归的",即如果两个对象在其字段中引用了第三个对象,并且一个线程正在其中一个上运行同步方法,那么任何其他线程是否可以访问第三个对象?
// a and b are some objects that implement Runnable
// they both reference the same third object
a.ref = c;
b.ref = c;
// a is run in a thread and processes some data in a loop for a long time
// the method the loop belongs to is declared synchronized
threadA = new Thread(a);
threadA.start();
a.someSyncedMethod(); // this would block ...
b.ref.someOtherSyncedMethod(); // ... but would this?
a.ref.someOtherSyncedMethod(); // ... and how about this?
Run Code Online (Sandbox Code Playgroud)