Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does adding "synchronized" affect method overriding?

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?

like image 668
paul Avatar asked Aug 01 '14 01:08

paul


People also ask

What is the effect of adding synchronized keyword to a method?

The synchronized keyword causes a thread to obtain a lock when entering the method, so that only one thread can execute the method at the same time (for the given object instance, unless it is a static method).

Can synchronized method be overridden?

For example, an overridden synchronized method's contract can be violated when a subclass provides an implementation that is unsafe for concurrent use. Such overriding can easily result in errors that are difficult to diagnose.

What are the restrictions on method overriding?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.

What is the disadvantage of synchronization in Java?

First drawback is that threads that are blocked waiting to execute synchronize code can't be interrupted. Once they're blocked their stuck there, until they get the lock for the object the code is synchronizing on.


3 Answers

"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

like image 70
Daiwik Daarun Avatar answered Sep 21 '22 23:09

Daiwik Daarun


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

like image 31
Nir Alfasi Avatar answered Sep 20 '22 23:09

Nir Alfasi


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"));
    }

}
like image 24
Kumar Abhishek Avatar answered Sep 21 '22 23:09

Kumar Abhishek