-1

I try to run several requests do different servers and debug program behaviour. Firstly I create and run several asynctasks which make several requests

public class MyTask extends AsyncTask<Object, Void, Void> {
    int runnnigDownloadThreadsCount = 0;

    @Override
    protected Void doInBackground(Object... params) {
        runnnigDownloadThreadsCount++;
        try {
            // TODO здесь необходимо исправить serverName на оригинальный урл с указанием параметра сервера
            List<TravelTicket> ticketsList = TravelTicket.downloadTickets((TravelTicket.RequestServerParams) params[0], (String) params[1]);
            if (ticketsList != null)
                synchronized (TravelBookingApplication.travelTicketsList) {
                    TravelBookingApplication.travelTicketsList.addAll(ticketsList);
                }
            return null;
        } catch (Exception e) {
            throw new Error(e);
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        runnnigDownloadThreadsCount--;
        if (runnnigDownloadThreadsCount == 0) {
            Intent i = new Intent(getActivity().getApplicationContext(), HotelsAndTicketsResponseListActivity.class);
            i.putExtra("requestType", "tickets");
            getActivity().startActivity(i);
            if (dialog != null)
                dialog.dismiss();
        }
    }
}

    String[] urls = new String[]{
            "http://dl.dropbox.com/u/34053723/provider1.json"
            ,
            "http://dl.dropbox.com/u/34053723/provider2.json",
            "http://dl.dropbox.com/u/34053723/provider3.json",
            "http://dl.dropbox.com/u/34053723/provider4.json"
    };
    for (final String serverName : urls) {
        // TODO ЗАМЕНИТЬ!!!
        // for (final String serverName : params.serverNames) {
        new MyTask().execute(params, serverName);
    }

Next in function TravelTicket.downloadTickets(...) I load data with RestTemplate

public static ArrayList<TravelTicket> downloadTickets(RequestServerParams requestServerParams, String serverName) throws Exception {
    // Запрос к серверу с указанием конретного провайдера и идентификторов запроса

    // Заголовки
    HttpHeaders requestHeaders = new HttpHeaders();

    requestHeaders.setUserAgent(TravelBookingApplication.USER_AGENT);

    HttpEntity requestEntity = new HttpEntity(requestHeaders);
    RestTemplate restTemplate = new RestTemplate();

Each the thread approaches with the last line HttpEntity requestEntity = new HttpEntity(requestHeaders); But after the first runs here debugger freezes. It looks like it stops on a breakpoint but there are no one.

sacredfaith
  • 850
  • 1
  • 8
  • 22
user826776
  • 1
  • 1
  • 3

1 Answers1

3

I'm not sure what is going on (there isn't enough information) but I can tell you for certain that RestTemplate is thread safe and is designed to be configured once and shared between threads, this is consistant with all of the spring *Template objects.

This statement is supported by the spring 4.0 documentation, in several places.

Section 11.6.1 Using the TransactionTemplate

The TransactionTemplate adopts the same approach as other Spring templates such as the JdbcTemplate. ... Finally, instances of the TransactionTemplate class are threadsafe

Section 13.2.1 JdbcTemplate best practices

Instances of the JdbcTemplate class are threadsafe once configured.

With persistence you can find similar statements about JmsTemplate. With regard to the RestTemplate the documentation merely states:

...It is conceptually similar to other template classes in Spring, such as JdbcTemplate and JmsTemplate and other template classes found in other Spring portfolio projects.

From this I have inferred that the intended to be thread safe in the same way that the other core Template classes are. This of course means it isn't guaranteed to be thread safe only that if it isn't, it is a bug.

Gareth Davis
  • 27,701
  • 12
  • 73
  • 106