求教牛人两段代码为什么不合理?
近日完成了一个项目,然后把代码交给总公司进行code review时,总公司同事指出下面两段代码存在问题,并给出了修改意见,是用英文描述的,小弟基本上能看懂其意思,但不知如何来修改,特地请教各位大虾.
第一段代码:
public Category getCategory() {
return category;
}
public Category getCategoryRank() {
return categoryRank;
}
同事给出的Error Description:
Directly return the object reference of a mutable object from getter method may violates the loose coupling concept and casue thread interference problem unless it is well thought and written in documents.
Suggestion:
Return a duplicated object
第二段代码:
public synchronized boolean add() {
boolean result = false;
try {
.........
}catch(Exception ){.....
}finally{
....
}
return result
}
同事给出的Error Description:
Where 2 users add a new category at the same time, the 2 new categories can be with the same name which is not desired.
Suggestion:
Intrinsic locks of class but not object.
[解决办法]
第二个是把方法改为static的 第一个好像是说返回的对象的clone,
英文不好不知道说的对不对
[解决办法]
第二个控制synchronized 粒度在细些!
[解决办法]
在第二个类里成名个static 类型的属性,用这个属性做锁旗标,用来锁类(个人理解)
[解决办法]
import java.lang.Runnable;
public class TestFrame{
public static void main(String[] args){
synchronized (name){
TestRunnable test1 = new TestRunnable();
TestRunnable test2 = new TestRunnable();
Thread t1 = new Thread(test1);
t1.start();
TestRunnable.b = false;
Thread t2 = new Thread(test2);
t2.start();
}
}
}
class TestRunnable implements Runnable{
public static boolean b = true;
public static String str = new String( "1234 ");
public void run(){
if(b){
while(true){
this.show1();
}
}else{
while(true){
this.show2();
}
}
}
public void show1(){
synchronized (str){
System.out.println( "in test runnable "+Thread.currentThread().getName());
try{
Thread.sleep(10000);
}catch(InterruptedException msg){
}
System.out.println( "out test runnable "+Thread.currentThread().getName());
}
}
public void show2(){
synchronized (str){
System.out.println( "in test runnable "+Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(InterruptedException msg){
}
System.out.println( "out test runnable "+Thread.currentThread().getName());
}
}
}
[解决办法]
public Category getCategoryRank() {
return categoryRank;
}
返回类型 确定是正确的?
[解决办法]
首先,他的这个建议要看你这个JavaBean,还有这个Category的具体情况,并不适用于所有情况。
他的意思是说,由于你直接返回了当前这个JavaBean的某个property/field的实例本身,而不是它的一个clone,导致了某个类似调用getCategory().setX(1000);的改变category/categoryRank的内部属性的操作,会使得整个系统错误。
[解决办法]
public Category getCategory() {
return category;
}
public Category getCategoryRank() {
return category;
}
第二個:
利用內部排他鎖處理就好了,不明白就直接問總公司的人
[解决办法]
1、java中的对象都是引用传递的,所以直接return category;这样返回的话,其他
Category a = getCategory() ;
a.setXXX(b);
这样的话,category的内容也会做相应的修改,他们的建议是你做好能够将category对象里面的内容“复制”一份给局部变量,然后返回局部变量;
2、该段代码的同步范围太“大”,只需要在你具体add的地方,进行同步就足够了。
[解决办法]
第一个 把它变作属性传出不行么 ?
使他变只读.
[解决办法]
第一个应该返回一个对象得引用,而不是对象本身
第二个应该加上线程控制,使其线程安全