你能指出这段java代码中的错误吗

问题描述 投票:0回答:0
@Component
public class Accumulator {

    private Map<String, Object> data = new HashMap<>();             
    private List<Map<String, Object>> dataList = new ArrayList<>();  

    private Map<Instant, String> responses = new HashMap<>();        

    private int dataId;                                              

    
    public void add(String field, String value) {
        data.put(field, value);
    }

    
    public <T> T get(String field) {
        return (T) data.get(field);
    }



    
    public String getResponses() {
        String out = "";
        for (String resp : responses.values()) {
            out += "; " + resp;
        }
        return out;
    }


    
    public synchronized void push() {

        dataList.add(data);

        if (dataList.size() == 100) {
            send();
            dataList.clear();
        }

        data = new HashMap<>();
        data.put("id", dataId++);
    }

    
    private void send() {
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<List<Map<String, Object>>> request = new HttpEntity<>(dataList);
        String url = "localhost:20808/msg/incoming";
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
        responses.put(Instant.now(), response.getBody());
    }

}

你能指出这段 java 代码中的错误吗?我需要更正这段代码中的错误。我不想错过什么。这必须作为多线程工作。它必须同步并使用线程安全的集合,但还有什么?还是我根本不对?

java spring multithreading optimization annotations
© www.soinside.com 2019 - 2024. All rights reserved.