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?
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).
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.
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.
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.
"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
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
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"));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With