简体   繁体   中英

How to synchronize setter and getter?

I'm new to Java and just need to find the correct syntax to synchronize this method.

I don't think I can change the template, I get "cannot edit in read-only editor." Normally I would just have the setter return the value.

This is just the general idea of the code, the syntax is not correct.

public class Task extends AbstractTemplate<Service> {
    private Enum result;
    
    @Override
    @Transactional
    public void execute(Map params) {
        result = this.executeService(params);
    }
    
    public Enum getResult() {
        return result
    }
}

The issue I'm running into of course is as I execute many concurrent requests to execute function and then immediately call getResult, the results returned do not correspond with correct params.

Unfortunately there is no good way to do that.

If execute and getResult are called from same thread you can put result in thread local variable. It is bad design, it is better to refactor you code so execute returns result directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM