<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vasco Veloso on Medium]]></title>
        <description><![CDATA[Stories by Vasco Veloso on Medium]]></description>
        <link>https://medium.com/@vasco-veloso?source=rss-425c13962028------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hBIuBI6CnIiyIOVFPloSzA.jpeg</url>
            <title>Stories by Vasco Veloso on Medium</title>
            <link>https://medium.com/@vasco-veloso?source=rss-425c13962028------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:04:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vasco-veloso/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Generics in Java explained]]></title>
            <link>https://medium.com/javarevisited/generics-in-java-explained-10bd559b3a94?source=rss-425c13962028------2</link>
            <guid isPermaLink="false">https://medium.com/p/10bd559b3a94</guid>
            <category><![CDATA[introduction]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Vasco Veloso]]></dc:creator>
            <pubDate>Sat, 01 May 2021 07:47:55 GMT</pubDate>
            <atom:updated>2021-05-01T07:47:55.001Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are generics, anyway?</h4><p>Recently, I had to explain what generics are and how they are used in the Java language. In a nutshell. Within a couple of minutes.</p><p>I ended up stating that generics are a mechanism that allows us to write code that does not care about the type of objects it is handling while at the same time giving enough information to the compiler to preserve type safety. Kind of a note left behind for the compiler.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6xvlVnWm5Ff0a0ELCMcvGA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Kelly Sikkema</a> on <a href="https://unsplash.com/s/photos/placeholder?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h4>We can not use Object to represent any type all the time</h4><p>Let us get started with the classic example of a list. Forgetting for a moment that <a href="https://javarevisited.blogspot.com/2016/01/9-difference-between-array-vs-arraylist-in-java.html#axzz6rDtB0US0">List </a>and <a href="https://javarevisited.blogspot.com/2012/02/difference-between-linkedlist-vs.html#axzz5hP3QBNdL">LinkedList</a> are generic types, the following is legal Java and creates a list holding any object type.</p><pre>List myList = new LinkedList();</pre><p>That means that this is legal:</p><pre>myList.add(“Hello”);</pre><p>And that this is also legal:</p><pre>myList.add(1);</pre><p>This mixture of types feels fishy. Can we also write the following loop?</p><pre>for (Integer i : myList) System.out.println(i);</pre><p>No. Because the list does not carry type information about its elements, the compiler can not verify whether each assignment to the variable i is valid. Instead, we must write:</p><pre>for (Object i : myList) System.out.println(i);</pre><p>If we intend to handle each list element as an integer to perform some arithmetic operation, it gets ugly:</p><pre>for (Object i : myList) System.out.println(((Integer) i) * 2);</pre><p>In fact, it got so ugly that any element that is not an integer will now crash the application with a <a href="https://javarevisited.blogspot.com/2012/12/how-to-solve-javalangclasscastexception-java.html#axzz6qVaG06bu">ClassCastException</a>. This is the problem when using Object to represent some unknown type: the code becomes brittle when an assumption is made about what that type represents.</p><h4>Fixing the problem by using type parameters</h4><p>When we take advantage of the <a href="https://www.java67.com/2019/07/top-50-java-generics-and-collection-interview-questions.html">generic type</a> and write the following instead, that problem is solved because the compiler can now do all checks for us:</p><pre>List&lt;Integer&gt; myList = new LinkedList&lt;Integer&gt;()</pre><pre>myList.add(1);</pre><pre>myList.add(“Hello”); // compiler error: now it is illegal.</pre><pre>for (Integer i : myList) System.out.println(i * 2); // beautiful.</pre><p>Now, we can be sure that the list contains nothing else but integers.</p><p>A type parameter is, first and foremost, an instruction for the compiler. Fellow humans come second.</p><pre>interface SomeCollection&lt;T&gt; {</pre><pre>  void add(T element);</pre><pre>}</pre><p>When we declare an interface such as the above, we’re telling the compiler that it must ensure that the argument to the add() method will always be of the type specified by the user code (T). Whatever type might it be, we do not care, as long as it is always the same!</p><p><a href="https://javarevisited.blogspot.com/2012/08/how-to-write-parametrized-class-method-Generic-example.html#axzz6tWnd7QYO">Generic types</a> can also be used at the method level. Let’s say we declare this function in some class:</p><pre>public &lt;T,R&gt; Optional&lt;R&gt; process(T input, Predicate&lt;T&gt; validator, Function&lt;T,R&gt; processor) {</pre><pre>  return validator.test(input)</pre><pre>    ? Optional.ofNullable(processor.apply(input));</pre><pre>    : Optional.empty();</pre><pre>}</pre><p>This function is a little bit convoluted, but it is an appropriate example nevertheless. Here, we use generic types to tell the compiler two things:</p><ol><li>That the validator and the processor must handle compatible types, represented by T.</li><li>That our function returns an instance of <a href="https://javarevisited.blogspot.com/2017/04/10-examples-of-optional-in-java-8.html#axzz6ccm5KWKs">Optional </a>wrapping the processor’s return value, represented by R.</li></ol><p>Again, we do not care which types those are. We only want to ensure that they are compatible.</p><h4>When the generic type must have some standard semantics</h4><p>It is possible to place restrictions on the generic type declarations using the extends keyword to expose a known interface. This way, the generic type becomes meaningful and useable within the method or class. However, the underlying concept remains. Any type whatsoever can be used in place of the generic type, as long as it implements the interface specified.</p><pre>class MyWrapper&lt;T extends MyOtherInterface&gt; {</pre><pre>  // use T in some manner</pre><pre>}</pre><h4>Conclusion</h4><p>Generic types in Java allow us to write high-level code that can handle different object types without knowing which exact types are those while maintaining type safety.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10bd559b3a94" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/generics-in-java-explained-10bd559b3a94">Generics in Java explained</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency and locking on databases]]></title>
            <link>https://medium.com/javarevisited/concurrency-and-locking-on-databases-daa14e2aa68d?source=rss-425c13962028------2</link>
            <guid isPermaLink="false">https://medium.com/p/daa14e2aa68d</guid>
            <category><![CDATA[jpa]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Vasco Veloso]]></dc:creator>
            <pubDate>Fri, 16 Apr 2021 14:01:18 GMT</pubDate>
            <atom:updated>2021-04-16T14:01:18.258Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving concurrency problems when working with JPA, Hibernate and Spring Data.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NyT2xZPFl98ymTOZjLQE9A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@tofi?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Tobias Fischer</a> on <a href="https://unsplash.com/s/photos/database?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>As soon as different threads on an application — or different application instances in a microservice ecosystem — need to make changes to the same database table, eventually, race conditions will surface.</p><p>By operating on the same table, those different threads will eventually want to update the same row. Without further controls, some updates will be silently lost…</p><figure><a href="https://www.java67.com/2016/02/top-20-hibernate-interview-questions.html"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sSsXyFJ1sX9j7ZoOthJuEg.png" /></a><figcaption>Losing changes silently</figcaption></figure><p>It is evident that this can happen when we’re talking about the same field being updated in both flows.</p><p>However, it also happens when different fields are updated! By default, <a href="https://medium.com/javarevisited/top-5-hibernate-online-training-courses-for-beginners-and-advance-java-programmers-469460596b2b">Hibernate</a> issues an UPDATE statement for all entity fields, so the entire row is updated. Doesn’t Hibernate’s <a href="https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/annotations/DynamicUpdate.html">@DynamicUpdate</a> annotation solve the problem? It will only update the fields which were changed, right? Well, no. <a href="https://medium.com/javarevisited/top-5-books-to-learn-hibernate-for-java-developers-b2cb4b16ccd6?source=---------14------------------">Hibernate </a>will compare the state of the in-memory entity with the database. Then it will generate an UPDATE statement for all columns which are different, regardless of where they changed. So, saving the in-memory entity will still overwrite changes made in the database!</p><p>This is a silent problem because there is no error raised to flag the overwrite. The only solution is to reload the record and retry the update. But to do so, the application must know that there is a conflict.</p><p>There are two workable solutions to this problem. All require a transaction to be active while the affected entity is being processed.</p><h3>Optimistic locking</h3><p>Each JPA entity that may be processed concurrently is enhanced with a <a href="https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/Version.html">@Version</a> column.</p><p>When an update is made on a row that has been modified since the last time it was read, the version number is no longer the same, and an exception is thrown.</p><p>Recovery is possible by reloading the affected entity, reapplying the changes, and saving it again.</p><p>Disadvantages of optimistic locking:</p><ol><li>Conflict is known only at commit time, so after all processing was done. The transaction is rolled back at that time. It can be a problem when using other, non-transactional resources for processing the entity.</li><li>The complexity of the retry mechanism: only the results of the processing can be played back. If non-transactional resources are involved in the processing, care must be taken not to repeat <a href="https://javarevisited.blogspot.com/2016/05/what-are-idempotent-and-safe-methods-of-HTTP-and-REST.html#axzz5j9AEsxuT">non-idempotent operations</a>.</li></ol><h3>Pessimistic locking</h3><p>In order words, using long-running transactions with row locking at the database level. Each transaction that wants to work with a row will need to wait until the other is done or a timeout occurs. The lock is released when the transaction completes.</p><p>Lock timeouts result in exceptions but not necessarily in a transaction rollback. Only when using a Spring transaction inside a <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html">@Transactional</a> method or a <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/support/TransactionTemplate.html">TransactionTemplate</a> callback is a rollback guaranteed because Spring rolls back the transaction if those methods throw an exception.</p><p>The most frequent pessimistic locking pattern is to lock the row against reading and modifying when it is read at the start of the transaction. This is known as a <em>pessimistic write</em> lock. No other transaction can read or modify the same row while it is locked. It often results in a SELECT … FOR UPDATE or similar statement being issued.</p><p>It is also possible to lock the row against modifications only, which means that other <a href="https://javarevisited.blogspot.com/2011/11/database-transaction-tutorial-example.html#axzz5WDqhDqX3">transactions </a>can read it at any time. This is a <em>pessimistic read</em> lock. Not all databases or JPA implementations support this mode and often upgrade it automatically to a <em>pessimistic write</em> lock.</p><p>The desired JPA lock mode can be configured as described in this <a href="https://javaee.github.io/tutorial/persistence-locking002.html">Java EE tutorial about lock modes</a>. When using Spring Data, we can use the <a href="https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/Lock.html">@Lock</a> annotation in Spring repository methods to specify the desired lock mode. The lock timeout value can be defined by setting the javax.persistence.lock.timeout hint in a <a href="https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/QueryHints.html">@QueryHints</a> annotation applied to a Spring repository method.</p><p>Disadvantages of pessimistic locking:</p><ol><li>Relies on the database for synchronization.</li><li>Timeout must be configured as a hint, so it’s not a contract and thus not necessarily portable between databases.</li><li>Deadlocks may occur when multiple resources are being locked.</li><li>Uses more resources at the database level.</li></ol><h3>Which one should be used?</h3><p>It depends. As it always does.</p><p>Ideally, application flows should be designed in such a way that <a href="https://medium.com/javarevisited/8-best-multithreading-and-concurrency-courses-for-experienced-java-developers-8acfd3b25094">concurrency </a>is not a problem. Thus, no resources are shared. This general principle also applies to database resources such as <strong>rows</strong>.</p><p>However, when it is impossible to prevent different flows from updating the same row concurrently, two factors influence the choice between one of these mechanisms.</p><p>In the end, the choice should be made after balancing the consequences brought about by either of the following:</p><h4>Traffic</h4><p>Pessimistic locking increases the resources needed by the database server, whereas optimistic locking leaves the burden on the client.</p><p>For low traffic, pessimistic locking may be appropriate. If traffic raises, though, the database server may become resource-constrained, leading to another type of problem. Also, threads in the client will be blocked until row locks are released, diminishing the client’s capacity to process requests.</p><p>When traffic levels rise, optimistic locking is a better choice.</p><h4>Concurrency</h4><p>Optimistic locking requires the client application to refresh its data when a conflict arises. This refresh increases the number of database roundtrips and thus the load at the database server.</p><p>High concurrency scenarios are good candidates for pessimistic locking.</p><h3>Conclusion</h3><p>There are two methods available for managing concurrency at the<a href="https://medium.com/hackernoon/top-5-sql-and-database-courses-to-learn-online-48424533ac61"> database</a> level: <strong>optimistic locking</strong> and <strong>pessimistic locking.</strong></p><p>The choice for one — or a combination of both — should be made after carefully considering the impact that traffic and concurrency have on the application and the database server, under either locking mode.</p><p>It is understood that load testing should always be performed under the expected maximum traffic levels to boost confidence in the solution and uncover problems early.</p><h3>Resources</h3><p>Documentation about the @QueryHints annotation: <a href="https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/QueryHints.html">QueryHints (Spring Data JPA 2.4.7 API)</a></p><p>Link to a <a href="https://javaee.github.io/tutorial/persistence-locking002.html">Java EE tutorial about lock modes</a>.</p><p>Documentation about the @Lock annotation: <a href="https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/Lock.html">Lock (Spring Data JPA 2.4.7 API)</a></p><p>Documentation about the @Transactional annotation: <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html">Transactional (Spring Framework 5.3.5 API)</a></p><p>Documentation about the @Version annotation: <a href="https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/Version.html">Version (hibernate-jpa-2.1-api 1.0.0.Final API) (jboss.org)</a></p><p>Documentation about the @DynamicUpdate annotation: <a href="https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/annotations/DynamicUpdate.html">DynamicUpdate (Hibernate JavaDocs) (jboss.org)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=daa14e2aa68d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/concurrency-and-locking-on-databases-daa14e2aa68d">Concurrency and locking on databases</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data models for partial update messages]]></title>
            <link>https://vasco-veloso.medium.com/data-models-for-partial-update-messages-e045433fac0?source=rss-425c13962028------2</link>
            <guid isPermaLink="false">https://medium.com/p/e045433fac0</guid>
            <category><![CDATA[data-modeling]]></category>
            <category><![CDATA[data-modelling]]></category>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[domain-events]]></category>
            <dc:creator><![CDATA[Vasco Veloso]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 10:44:57 GMT</pubDate>
            <atom:updated>2021-02-03T10:44:57.178Z</atom:updated>
            <content:encoded><![CDATA[<p>During a recent code review, I had to think about alternative solutions to the data model of a simple message processing flow.</p><p>For the sake of this article, let’s assume that we’re talking about some webshop customer data: address and payment method.</p><p>A system of record contains this data and publishes events every time that a section of it is updated.</p><p>So, every time that a customer updates their address, a CustomerAddressChanged message is posted. Likewise, a CustomerPaymentMethodChanged message is posted when the customer edits their payment options.</p><p>Message models are already defined by the system of record. The payload of each message is the data relevant to the event and the unique customer identifier, so consumers can relate the event data to the customer profile.</p><p>The piece of software under review needs to process both events.</p><p>If I were the one writing the message processing code, I’d use the following structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/0*1hprEjMRjwpGmWyM" /><figcaption>The model I would have used</figcaption></figure><p>The reason would be that this model implements the exact data model of the system of record events, thus allowing future readers of the code to quickly understand the event model without requiring further documentation. Because this code belongs to an adapter layer, there is no problem in having the model of an external system there — that’s what an adapter is for.</p><p>However, my colleague chose the following implementation instead:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/0*LiwxHKCjAN1_R6-B" /><figcaption>My colleague’s first proposal</figcaption></figure><p>He argued that because the updates were related to the same conceptual entity — the customer — it made more sense to share the full entity model amongst notifications. Then some fields would be “optional” or “empty” depending on the context.</p><p>It is a defendable position. True, the entity does contain both fields from a holistic perspective, and we have materialized that in the internal data model for this application.</p><p>My concern was that, given the model as it was proposed, it’s possible to call both CustomerAddressChanged::getCustomer()::getAddress() and CustomerPaymentMethodChanged::getCustomer()::getAddress(). However, <em>within the context of incoming events</em>, only the former will yield valid data whereas the latter will always be invalid.</p><p>The responsibility to know which piece of data is valid in which context was thus transmitted to the user of the model. It is not expressed in the model itself and it becomes extremely <a href="https://www.martinfowler.com/bliki/AnemicDomainModel.html">anemic</a>.</p><p>If we want to keep the single Customer definition, a compromise can be reached. One possibility would be to make it fully private and expose the relevant public getters in the notification classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/0*kQLSVCPE2mQVoiTk" /><figcaption>The final model</figcaption></figure><p>This way the model would make it clear what can be expected to be present in each context, and user code could no longer access fields that are always invalid for a specific message.</p><p>It is not the exact model of the incoming events. But it does not matter much because the intent of each event and the data they carry are now clearly expressed.</p><p><em>The model as depicted is still anemic. We have also introduced message validation in the model implementation, so the model knows how to validate itself. Less responsibility to the code using the model, which only needs to know how to deal with validation errors. This is not shown in the diagrams.</em></p><p>My conclusion is that, as always, there are several ways to solve a problem. Sometimes a compromise between two different perspectives can produce a perfectly appropriate solution.</p><p>Will I use the latter pattern as a starting point for similar needs in the future? No. I still like my first option better. However, now we’re talking about personal preferences. These are always debatable…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e045433fac0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When flows spilt]]></title>
            <link>https://vasco-veloso.medium.com/when-flows-spilt-133c9348cfd9?source=rss-425c13962028------2</link>
            <guid isPermaLink="false">https://medium.com/p/133c9348cfd9</guid>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Vasco Veloso]]></dc:creator>
            <pubDate>Thu, 19 Nov 2020 14:01:09 GMT</pubDate>
            <atom:updated>2020-11-19T14:01:09.790Z</atom:updated>
            <content:encoded><![CDATA[<p>An introduction to concurrent programming</p><p>A computer program is a nicely ordered flow of instructions, executed one after the other from start to finish… Except when it is not. We often wish to split this flow. Let’s see why and when to do it.</p><h3>So, what’s concurrent programming?</h3><p>The traditional way of writing programs is to express them sequentially. When one input is received, an output is produced by executing a number of tasks in order. Then the program becomes ready to process the next input, over and over again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y3nHcq750K4C1B9WX49A9g.png" /><figcaption>Figure 1 — sequential computing</figcaption></figure><p>This way of working matches how machines with a single processing core work. A single processor can only do one thing at a time, so this is the most straightforward method to program on it.</p><p>There are drawbacks, however. If any of the tasks needs to wait for something, say, a peripheral or the network, then the processor will remain idle during this waiting period. Processing of further inputs will be delayed, thus reducing the overall system effectiveness.</p><p>Ideally, processor time could be spent advancing one task for the next input while the processing of the previous input is waiting. This way a shared resource — the processor — could be used efficiently by different computations. To the human observer, both computations would seem to be performed simultaneously, while in reality they were being interleaved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*VzS2sJgUn4XyQ3imH7TC7A.png" /><figcaption><em>Figure 2 — concurrent computing</em></figcaption></figure><p>We just defined concurrent computing while barely noticing it. Any two computations are concurrent whenever they share a common resource. They don’t occur at the same exact moment in time, but their lifespans overlap. Concurrent programming is the set of programming techniques that allow us to write programs that work in this manner.</p><p>The moment in time when computations happen is a subtle but important detail that allows us to distinguish between concurrent and parallel computing. With parallel computing, computations don’t always share resources and overlap at one or more moments in time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*qKSGTNQz5FyB8AZalZZwhQ.png" /><figcaption><em>Figure 3 — parallel computing</em></figcaption></figure><p>It follows that we need to employ different physical processors in order to actually run some parallel computing. Otherwise, such computations would not be parallel but concurrent. Multi-processor computers and distributed systems are examples of computational hardware that is capable of executing parallel computations. Parallel programming is thus a specialization of concurrent programming, using similar techniques, focused on ensuring that computations occur at the same physical time.</p><h3>Which operations fit a concurrent model best?</h3><p>Going back to our task that needs to wait for a resource, we concluded then that processing time was not being used efficiently. It is therefore logical that operations that need to wait for something else are prime candidates to be executed concurrently. It does not make sense to run them exclusively in parallel because resources will be wasted anyway whilst barely improving throughput.</p><p>Tasks that don’t wait are perfect candidates for parallelism. Think about data processing and mostly calculations. If it doesn’t need to wait nor share resources, then it will benefit the most from running in parallel.</p><p>For the remainder of this article, we will focus on concurrency instead of parallelism. On one hand, because the operating systems used to run most of our programs are concurrent by nature. On the other hand, because the most common challenges fall within this category. This does not mean that nothing of what will be said applies to parallel computing; most of it also applies, however one must choose the correct supporting framework.</p><h3>Preparing for concurrency</h3><p>First and foremost, we must be sure that the problem at hand can be solved properly using concurrent programming. The initial step is to decompose the problem in a sequence of separate tasks. Once this is done, we need to determine if most of those tasks will spend a long time waiting for some resource or operation to complete, say, I/O. If this is the case, then we found ourselves a good candidate for a concurrent solution. The tasks that need to wait can do so while relinquishing their CPU time for the duration of the waiting period.</p><h3>Concurrent programming models</h3><p>Concurrent tasks need to exchange data amongst themselves like any other. This data needs a vehicle, which leads us to the two common concurrent programming models: shared memory and message passing.</p><p>Both are quite easy to understand. When concurrent tasks read and write objects in memory to exchange data, then the model being used is one of shared memory. Memory, in this sense, doesn’t necessarily mean volatile memory. It can also be a file system where concurrent tasks exchange data inside files, for example.</p><p>With message passing, concurrent tasks are sending and receiving well defined chunks of data through a communications channel, regardless of their locality. Examples of such channels could be HTTP, EMS, shared sockets, etc.</p><p>Typically, shared memory models are used within the program itself, whilst message passing is used between different programs that may even be located in different computers.</p><h3>Concurrent programming is hard</h3><p>Concurrency is all about resource sharing. The most evident — and problematic — kind of sharing is writing to a shared data location. A classic example is the bank account deposit problem. If two tasks want to deposit one euro in the same bank account at the same time, it is possible for money to be lost depending on the way how the independent deposit steps are ordered. This is a problem known as a <em>race condition</em>, whereby the outcome is dependent on which concurrent operation finishes first.</p><p>Other well-known problems are <em>starvation </em>and <em>deadlocks</em>, which can occur when there are multiple tasks sharing a finite set of resources. As chance may have it, resources may be claimed by all tasks in such a way that each task still lacks resources to continue: no task makes any progress, which is a <em>deadlock</em>. If only a subset of tasks is blocked from continuing, we say that these tasks are being <em>starved</em>. The dining philosophers problem [2] is a good example of a system study where both problems may happen, depending on the solution chosen.</p><p>All this goes to show that concurrent systems possess non-deterministic characteristics, i.e., the time and order of operations is not always the same and depends on external conditions. Think about load on a web shop during a sale. The timing of operations is widely different during such peak times. Some defects (like the deposit problem) may cause trouble during these periods and remain harmless the rest of the time. These can be very hard to detect, fix, and test for.</p><p>Humans have a hard time wrapping their heads around parallel flows. That being said, [1] helps with further understanding of these basic problems related to concurrency.</p><h3>Implementation styles</h3><p>The basic premise of concurrency is the interleaving of tasks (see figure 2). In order to accomplish this on only one computer, some foundation must exist through which such interleaving becomes possible. A construct called a <em>thread </em>was thus developed.</p><p>A thread is the smallest unit of execution that can be managed by a hardware platform or operating system. From a physical point of view, one <em>hardware thread</em> corresponds to one CPU core. At the operating system level, <em>threads</em> (also known as <em>software threads</em>) are managed by a scheduler, which contains a mechanism that executes several threads alternatively on the same hardware thread.</p><p>This is how we can run a very high number of programs on the same computer concurrently: each program correlates to at least one thread. Then these threads share a reduced number of hardware threads, taking their turns to move ahead in doing their jobs. If this mechanism for alternating between threads did not exist, we would only be able to run concurrently as many programs as the number of CPUs in the computer.</p><p>The alternation mechanism can be <em>pre-emptive</em> or <em>cooperative</em>. The former means that the scheduler is free to decide for how long it will allow a thread to run on a hardware thread, whereas the latter means that threads themselves decide when they are ready to yield CPU time. (Can you see how cooperative systems can be led into starvation?) Operating systems don’t need to support exclusively one option, and in fact many platforms support both. The difference between pre-emption and cooperation is key when reasoning about concurrency.</p><p>Some platforms already support <em>fibers</em>, which can be seen as software threads that are scheduled cooperatively on the operating system threads. A second level of scheduling, or a higher level of abstraction, so to speak. These came into existence because they need less memory to save their state and execution takes less time to switch from one to the next, comparatively to threads.</p><h3>Tools for concurrency in the JVM</h3><p>Threads are implemented in the JVM as a direct correspondence to operating system threads. Their management can be simplified by using implementations of the ExecutorService interface, which decouples threads from runnable tasks. They also provide Future, which is an interface used to follow task execution state and obtain its result. Recent Java versions saw the rise of CompletableFuture, which expands on the concept of Future to ease task composition.</p><p>The difficulties that come along with resource sharing can be solved with judicious use of atomic variables, locks, specific concurrent implementations of collections, and synchronisation primitives. [3] provides a good overview on these basic constructs.</p><p>Upcoming versions of Java will bring new tools to the arsenal, including fibers.</p><h3>Concurrency frameworks on the JVM</h3><p>Naturally, we don’t want to rebuild the foundations every time we create a new concurrent application. There are several frameworks that we can use to build our applications upon. Some of these are:</p><p>Akka, a message-passing concurrency framework implementing the <em>actor model</em>. In this model, actors embody the logic necessary to process each message type and produce messages as result. [5] They can consequently run concurrently and independently from one another.</p><p>Vert.x, an event-driven framework, embraces the concept of <em>reactive programming</em>, which is about expressing behaviour as a reaction to external events. It also implements its own version of fibers, like “lightweight threads”.</p><p>RxJava allows us to express our concurrent flows as streams of objects or events. Leverages some functional programming concepts to simplify composition and expressivity of object processing pipelines. [4]</p><p>Kotlin is a language for the JVM which brings <em>coroutines</em> to the equation. Coroutines are a language construct which allows us to build concurrent tasks running cooperatively, much like fibers. Being built in the language itself, coroutines have the advantage of being concise and expressive.</p><h3>Where to go from here?</h3><p>This article was just a very quick overview of what concurrency is about, and how it can help with improving efficiency of specific problems. The interested reader will want to follow our reading suggestions to better understand the problems that concurrency brings and their solutions, as well as some frameworks designed to simplify the building of concurrent systems.</p><p>A second article about concurrency in the JVM addresses usage of coroutines in Kotlin:</p><p><a href="https://vasco-veloso.medium.com/concurrency-in-kotlin-d8579fd7a2e5">Concurrency in Kotlin</a></p><h3>Additional reading</h3><p>[1] <a href="https://web.mit.edu/6.005/www/fa14/classes/17-concurrency/#two_models_for_concurrent_programming">https://web.mit.edu/6.005/www/fa14/classes/17-concurrency/</a></p><p>[2] <a href="https://en.wikipedia.org/wiki/Dining_philosophers_problem">https://en.wikipedia.org/wiki/Dining_philosophers_problem</a></p><p>[3] <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/">https://docs.oracle.com/javase/tutorial/essential/concurrency/</a></p><p>[4] <a href="https://www.oreilly.com/library/view/reactive-programming-with/9781491931646/ch01.html">https://www.oreilly.com/library/view/reactive-programming-with/9781491931646/ch01.html</a></p><p>[5] <a href="https://doc.akka.io/docs/akka/current/typed/guide/actors-intro.html">https://doc.akka.io/docs/akka/current/typed/guide/actors-intro.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=133c9348cfd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency in Kotlin]]></title>
            <link>https://vasco-veloso.medium.com/concurrency-in-kotlin-d8579fd7a2e5?source=rss-425c13962028------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8579fd7a2e5</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Vasco Veloso]]></dc:creator>
            <pubDate>Thu, 19 Nov 2020 11:37:04 GMT</pubDate>
            <atom:updated>2020-11-19T13:59:31.316Z</atom:updated>
            <content:encoded><![CDATA[<p>How coroutines make it easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*BzSs3uCi37juHL7g5foLDg.png" /></figure><p>Concurrent software is hard to get right, as we’ve seen in the previous article, “When flows split”. Concurrency is even harder to express. Most concurrent code is being written in a way that makes our heads spin every time we need to read it. Let us see how Kotlin helps us write concurrent code that is clearer and more efficient from the start.</p><p>This help comes under the form of first-class support for coroutines.</p><p>Of course, now one question begs to be asked: what are coroutines?</p><p>We are all familiarised with the concept of a routine, also called a function or a method. Subroutines mean pretty much the same in our everyday work. But coroutines? They are not the same thing, but they are a deceptively simple concept. Coroutines are routines that cooperate with their execution environment and with each other.</p><p>This cooperation takes place in the sense that one coroutine can allow the control flow to be transferred to another coroutine without ending the first coroutine. Even potentially switching between execution contexts. Let it sink for a bit. This unusual definition means that one call from a coroutine to another may cause the original control flow to be suspended, only to resume later, maybe on a different thread.</p><p>Suspension in coroutines bears some similarities to suspension in threads. A thread becomes frozen in time and space once it is suspended. Then the OS scheduler selects another thread to unfreeze and resume execution, then suspends it, selects another, and so on until our first thread is selected to resume execution. At this point, it continues as if nothing had happened. The same concept applies to coroutines.</p><p>The big difference lies in the mechanics for suspending and resuming.</p><p>First of all, suspension in coroutines can only happen when another coroutine is being called, whereas threads can be suspended at any time. This is why we said earlier that a coroutine could “allow” the transfer of the control flow: a coroutine can never suspend if it does not call other coroutines.</p><p>Unlike traditional thread-based concurrency, there is no one-to-one relationship between coroutines and threads. So, the thread that was executing a coroutine that got suspended can move on to execute another coroutine. Coroutines are one level of abstraction higher than threads.</p><p>All this also means that calls to a coroutine will not necessarily restart its execution, as execution may continue from somewhere in its middle. It all depends on the state of the execution flow. That is right. Coroutines are stateful!</p><p>Just for completeness, we can define subroutines as a particular case of coroutines in the sense that they do not cooperate and are not stateful. One call from one subroutine to another never causes suspension of the execution flow, always continues in the same thread, and always runs from the beginning to the end of the subroutine. This is the behaviour we are used to seeing in non-concurrent programs.</p><h3>Writing coroutines</h3><p>Kotlin provides first-class support for coroutines. The language itself supports them, so there is no need for additional frameworks, plugins, or special libraries. The compiler and the runtime do all the work necessary to preserve and pass along the state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e70d14051d2d02ea443fa0ecdf7ba92/href">https://medium.com/media/3e70d14051d2d02ea443fa0ecdf7ba92/href</a></iframe><p><em>(listing 1)</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f78eacf9442c09b529f5626ec0421a9/href">https://medium.com/media/7f78eacf9442c09b529f5626ec0421a9/href</a></iframe><p><em>(listing 2)</em></p><p>Compare the Java code in snippet 1 with the Kotlin code in snippet 2. Java got better with CompletableFuture after version 8, but Kotlin is a bit more elegant, right? Coroutines were designed to streamline the expression of concurrency in our code.</p><p>As we can see when running them, the results of both code snippets are equivalent. Both the Java lambdas and the Kotlin coroutines ran concurrently and in different threads. Let us see how we can work with Kotlin’s coroutines in practice.</p><p>We define a coroutine by using the suspend keyword in its declaration:</p><p><strong>suspend fun </strong>doSomething(): Unit</p><p>This keyword allows the compiler to handle such functions differently. They now need to abide by the rules of coroutines. Rule number one: a coroutine may only be called from within a coroutine scope. Or in other words, from another coroutine.</p><p>It may seem like an impossible task. After all, a Kotlin program starts at the main() method, which is a regular subroutine, right? It turns out that there are constructs that allow us to create an initial coroutine scope.</p><p>runBlocking() comes to the rescue. It will take a lambda argument and run it within a coroutine. It will also not exit until the coroutine has completed. This is an essential detail because it is possible to schedule the execution of coroutines in different threads. As such, it is necessary to ensure that the main application thread does not exit until our coroutines have finished; otherwise, the application would terminate too soon.</p><h3>Making coroutines concurrent</h3><p>What about concurrency, then? Earlier, we mentioned concurrency and that coroutines make it easier to write concurrent code. We must keep in mind that just calling coroutines does not make them concurrent. Even though it is true that each call site of a coroutine is a suspension point, the execution flow is still sequential unless stated otherwise. Therefore, we must be explicit and say that we want a given coroutine to run concurrently. We use launch() to do so. Like runBlocking(), it will run a lambda function in a coroutine, but it will not block until its completion. Instead, it launches a child coroutine. The coroutine that called launch() will not complete until all its children do.</p><p>All of this becomes clearer if we look back at code snippets 1 and 2. In Java, we had to list explicitly all the futures we had created in order to wait for their results. The Kotlin version with coroutines is different. It automatically manages all calls made to concurrent coroutines within the runBlocking() scope.</p><p>Coroutine invocations using launch() return an instance of an object implementing Job. This interface represents the running state of a coroutine in Kotlin, much like Future represents the running state of an asynchronous task in Java. It is possible to join Jobs just like Futures. However, Job does not allow the caller to consume values returned by a coroutine. Fire-and-forget coroutines have their uses, but coroutines only achieve their full potential as functions when they can return values to their callers.</p><p>There is another interface that extends from Job, called Deferred. This interface contains the await() method, which we use to retrieve values returned by coroutines. We obtain instances of Deferred when async() is used instead of launch().</p><p>Now we can start coroutines concurrently and work with the values they return. We demonstrate this in code snippet 3.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a75c216bff26bf8b1567bf1a013d0a6/href">https://medium.com/media/0a75c216bff26bf8b1567bf1a013d0a6/href</a></iframe><p><em>(listing 3)</em></p><h3>Coroutines still need threads</h3><p>The mechanism that supports coroutines is not magic. Remember that just like any other code, coroutines need threads to run on: just because we are not creating them ourselves, that does not mean that they do not exist.</p><p>Entities known as dispatchers manage the threads allocated to the execution of coroutines. They keep track of which coroutines are running, together with their states, and assign coroutines to threads as necessary. Just like thread pools dedicated to coroutines.</p><p>Kotlin supports several built-in dispatchers, each with its purpose. There is a dispatcher meant for I/O tasks, another to ensure that coroutines run on a thread associated with the user interface, a pre-built thread pool, among others. One should consult the Kotlin documentation to obtain more details about them since some are platform-specific.</p><p>It is possible to assign coroutines to specific dispatchers, and it is often recommended to do so. We can specify the dispatcher as an argument to the launch(), async(), and withContext() functions. Once a dispatcher is defined, by default or explicitly, it will be used by subsequent launch() and async() calls unless these specify a different dispatcher.</p><p>Code snippet 3 demonstrates usage of withContext(). In this example, each coroutine ran in a different dispatcher. The caller code got suspended in each invocation of await() until the respective coroutine finished.</p><p>Looking back at code snippet 2, we see that runBlocking() determined the dispatcher to use. In the example, it was necessary to define a dispatcher explicitly in order to ensure that the outcome would be as displayed: each coroutine running in a thread other than the main thread. If left undefined, the dispatcher used may be the Unconfined dispatcher: each coroutine will run in the calling thread until they suspend. Because the coroutines in the example never suspend — the logging operation is blocking — then only the main thread would be used.</p><p>Regardless of the function utilised to change the dispatcher, the chosen dispatcher will remain in scope and will be used for new coroutine invocations until it is changed again later.</p><p>Dispatcher management can become as complicated as thread pool management. A useful piece of advice is to use withContext() to specify the desired dispatcher. Of course, that would be overkill to do in every coroutine. So, we should use our best judgment and always remember to ask ourselves if the coroutine we are writing can run in any dispatcher or should be constrained to a specific dispatcher.</p><p>We may create our dispatchers, for example, when we want to manage the resources used by a set of coroutines. Or when these need to be segregated from other coroutines due to their runtime characteristics. Usually, this is done by creating an ExecutorService. The dispatcher is then obtained through the asCoroutineDispatcher() Kotlin extension method. No further management on our part is required, except that we must remember to shut down the executor. See code snippet number 4 for an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72cd3ef5ab6fe19049cc5d20f808af16/href">https://medium.com/media/72cd3ef5ab6fe19049cc5d20f808af16/href</a></iframe><p><em>(listing 4)</em></p><h3>Error management</h3><p>Error management is an essential part of programming. It is relatively simple to reason about error flows in sequential code. However, it can be challenging to foresee all possible error flows once we start to use concurrent patterns.</p><p>For example, if we launch three concurrent tasks for one computation, and one of them fails, we need to ensure that all of them have completed before failing the entire computation. Some or all of the others may fail too, even at different moments in time. We must also handle all these possible failures. We even want to cancel tasks that have not failed instead of waiting for them to finish, since we already know that the computation as a whole will fail. All this management can add complexity that is unnecessary to the original purpose of the computation.</p><p>Structured concurrency is a paradigm whose purpose is to simplify the coordination of concurrent execution flows by enclosing them into execution scopes. These scopes are then handled automatically, terminating only when all enclosed concurrent execution flows reach their completion. In case an error occurs in one of them, then all others are cancelled.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ef67f5761fd115881bcb72b58c00b3a/href">https://medium.com/media/8ef67f5761fd115881bcb72b58c00b3a/href</a></iframe><p><em>(listing 5)</em></p><p>We want to simulate two non-blocking calculations in code snippet 5. Unfortunately, in this simulation, one of them fails with an exception. The runtime library cancels both coroutines after the failure. Even the coroutine that did not fail is not allowed to run to its completion. The exception is propagated to the code that called await(), so it can handle the error appropriately.</p><p>Not only does this make it easier to coordinate the execution of separate concurrent flows, but it also eases the cognitive burden of managing cancellations and failure cases explicitly. Without a structured approach, the overhead introduced by error and cancellation handling leads to quite a bit of boilerplate code that distracts us from the real purpose of the computation. Structured concurrency eliminates most of this added complexity.</p><h3>Is this all?</h3><p>We do not intend to present an exhaustive description of coroutines in this article. Much remains to be said about them. Instead, we hope to have shown that Kotlin has allowed concurrent code to become more readable by offering first-class support for coroutines. Form no longer obscures the intent. Happy concurrent coding!</p><h3>Additional reading</h3><ul><li>Alst, Paulien van (2020), Kotlin — Hype of een nieuwe speler op de markt?, <em>Java Magazine, 01/2020</em>, pp 22–24</li><li>Subramaniam, Venkat (2019), Programming Kotlin, Pragmatic Bookshelf</li><li>Kotlin documentation on coroutines, <a href="https://kotlinlang.org/docs/reference/coroutines-overview.html">https://kotlinlang.org/docs/reference/coroutines-overview.html</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8579fd7a2e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>