Does adding "synchronized" affect method overriding?
Asked Answered
T

3

5

my problem is that:

search_text.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            ArrayList<Object> GPDMvalue = (ArrayList<Object>) multiSortList.getValue();
            ArrayList<Map<String, Object>> valueList = getDefaultValue(GPDMvalue);
            multiSortList.clear();
            if(getGPDMList().size()==0)return;
            multiSortList.setDataSource(getGPDMList());//new thread 1
            multiSortList.setDefaultOrAddValue(valueList);//new thread 2
        }
    });

when the text changing too fast ,and the thread 1 or thread 2 does't excute completely,and the maybe some problem,so i want add the synchronized like this public synchronized void modifyText(ModifyEvent e), is this still a override method and will it work?

Treed answered 1/8, 2014 at 1:38 Comment(1)
A similar discussion could be found here: #15998835. The key point is 'synchronized' is NOT part of the method signature. So the answer is yes.Cretaceous
B
3

Adding the synchronized keyword does not get in the way of overriding a method (it is still overridden) because the method's signature remains the same.

For more details see JLS-§9.4.1.3

Binion answered 1/8, 2014 at 1:46 Comment(0)
D
5

"Whether or not a method is synchronized is an implementation detail of the method. Synchronization isn't specified anywhere as a declarative contract - it's not like you can synchronize in interfaces, either.

How a class implements whatever thread safety guarantees it provides is up to it."

Taken from here

Derisive answered 1/8, 2014 at 1:46 Comment(0)
B
3

Adding the synchronized keyword does not get in the way of overriding a method (it is still overridden) because the method's signature remains the same.

For more details see JLS-§9.4.1.3

Binion answered 1/8, 2014 at 1:46 Comment(0)
G
3

You can override a synchronized method and you can also remove synchronized keyword.

abstract class SynchronizedClass {
    public synchronized String myIterfaceMethod(String str){
        return str;
    }

}

public class OverrideSynchronozied extends SynchronizedClass{

    @Override
    public String myIterfaceMethod(String str){
        return str;
    }

    public static void main(String[] args) {
        SynchronizedClass ss = new OverrideSynchronozied();
        System.out.println(ss.myIterfaceMethod("Class Instance"));
    }

}
Galleon answered 6/4, 2016 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.