Background:

Matlab's webread and urlread are single threaded functions. When executing multiple download commands they are executed sequentially, while blocking the main Matlab process. For most applications in Matlab this is not a problem, normally data is downloaded once and when the data are available the main program continues its execution. But if data have to be retrieved in various separate requests, for example in the MapViewer where a number of tiles with background images have to be downloaded, it is important that the application's communication is fast and does not block the main application while downloading. 

Multithreading is not possible in Matlab, only some of the built-in Linear algebra and numerical functions are multithreaded. To make use of multiple threads the use of Java is necessary. Multithreading is part of the Java core and available in Matlab. To make a multithreaded urlread method the following Java classes are useful:

One or more tasks can be submitted to the ThreadPoolExecutor. The tasks are put in a queue, when a thread becomes available the first task in the queue is assigned and executed on that thread. Upon completion the task is removed from the pool and the requested data is available from the task. The schematic of the ThreadPoolExecutor is shown in the figure below.

By using this construction HTTP request can be executed asynchronously, however all processing takes place in Java, the requested data is not available in Matlab. The data are available when the task is completed, but we cannot wait until that has happened, because that conflicts with what we want: a non blocking HTTP request. If the Matlab program is thus continued there is no way to know when the tasks are completed.

To overcome this problem the mechanism from the previous figure is enhanced with a Matlab task in which a callback is triggered when the task is completed. The Matlab task contains all the necessary information to construct an HTTP request. For example the Request Method, the URL, the query parameters etc. But it also contains the Matlab callback that has to be executed when the task is completed. This HTTP request is constructed in Java and queued in the ThreadPoolExecutor, upon completion of the task the Matlab callback is triggered with the retrieved data.
 

The following code fragment illustrates the use of the ThreadPoolExecutor in combination with the HTTP request.
Note that all the Java code is hidden from the user; the user only needs to know Matlab. 

import modelit.web.concurrent.*
import modelit.web.client.*

% Create a ThreadPoolExecutor to which HTTP requests can be submitted
pool = ThreadPoolExecutor(2); % Two threads

% Set the callback to be executed on completion
request = HttpRequest('get','http://www.httpbin.org/bytes/10000000');
% Set the callback to be executed on completion
request.setCompletedCallback(@obj(disp(obj));
% Submit the request
pool.submit(request);

% Another request
request = HttpRequest('get','http://www.httpbin.org/bytes/10000000').setCompletedCallback(@obj(disp(obj));

pool.submit(request);