Futures
In earlier chapters, we learned that parallel executions in a concurrent program proceed on entities called threads. At any point, the execution of a thread can be temporarily suspended, until a specific condition is fulfilled. When this happens, we say that the thread is blocked. Why do we block threads in the first place in concurrent programming? One of the reasons is that we have a finite amount of resources; multiple computations that share these resources sometimes need to wait. In other situations, a computation needs specific data to proceed, and if that data is not yet available, threads responsible for producing the data could be slow or the source of the data could be external to the program. A classic example is waiting for the data to arrive over the network. Let's assume that we have a getWebpage
method, that given a url
string with the location of the webpage, returns that webpage's contents:
def getWebpage(url: String): String
The return type of the getWebpage
method...