<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Javi Hache</title>
    <description>The latest articles on DEV Community by Javi Hache (@javihache).</description>
    <link>https://dev.to/javihache</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F228178%2F79a0ea29-c442-41bf-a8c4-ba4e98b3a94e.png</url>
      <title>DEV Community: Javi Hache</title>
      <link>https://dev.to/javihache</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/javihache"/>
    <language>en</language>
    <item>
      <title>☕ Java Virtual Threads Specification</title>
      <dc:creator>Javi Hache</dc:creator>
      <pubDate>Wed, 02 Feb 2022 19:49:38 +0000</pubDate>
      <link>https://dev.to/javihache/java-virtual-threads-specification-1mid</link>
      <guid>https://dev.to/javihache/java-virtual-threads-specification-1mid</guid>
      <description>&lt;p&gt;I recently found out this interesting Java specification - &lt;a href="https://openjdk.java.net/jeps/8277131"&gt;The Java Virtual Threads&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On of the biggest concerns of threads implementation in Java is that they user a real OS threads whenever we use them in our applications.&lt;/p&gt;

&lt;p&gt;OS threads are limited and expensive if you don't manage them well. When building a high concurrence software you will need to create and manage a logic to reuse threads as much as possible, usually using thread pools.&lt;/p&gt;

&lt;p&gt;The theory is simple: your application should get one available thread of the pool when it needs it and return it once it finishes its job.&lt;/p&gt;

&lt;p&gt;But in practice, well.. The thread pools end up being shared between several modules with different responsibilities. That code starts loosing cohesion and it's more difficult to maintain. And at the end one thread cannot be identified with a single transaction because it will be in charge of very different functionalities during its lifetime.&lt;/p&gt;

&lt;p&gt;To mitigate this problem, the Java team has created the Virtual Threads. Java will be in charge behing the curtains to asign and liberate OS resources while you will have the possibility to represent single-resposibility transactions with separated Virtual Threads. No more headaches about optimizing the pool (intially) and no more spaguetti code because of that.&lt;/p&gt;

&lt;p&gt;Another great things of this new Thread implementation is that is been integrated into &lt;code&gt;Thread&lt;/code&gt; API. That way it won't require a big learning curve or it shouldn't have very big side effects if you would like to migrate your code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Virtual thread creation using Thread Builder interface&lt;/span&gt;
&lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofVirtual&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"virtual_thread_1"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;unstarted&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runnable&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Virtual thread factory&lt;/span&gt;
&lt;span class="nc"&gt;ThreadFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofVirtual&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks promising, but I'm expecting to try it out as soon as possible to check the potential of this thread implementation. Let's keep an eye on it!&lt;/p&gt;

&lt;p&gt;If you liked this article, feel free to follow me on &lt;a href="https://twitter.com/javihache_dev"&gt;twitter&lt;/a&gt;  and check out &lt;a href="https://javihache.dev/"&gt;my personal blog&lt;/a&gt;. Thank you! 😁&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>news</category>
      <category>backend</category>
    </item>
    <item>
      <title>🐵 Monkey Patches</title>
      <dc:creator>Javi Hache</dc:creator>
      <pubDate>Sat, 29 Jan 2022 12:12:23 +0000</pubDate>
      <link>https://dev.to/javihache/monkey-patches-1k6o</link>
      <guid>https://dev.to/javihache/monkey-patches-1k6o</guid>
      <description>&lt;p&gt;Do you know what Monkey Patches are? 🐒&lt;/p&gt;

&lt;p&gt;You have probably seen them or even implemented a Monkey Patch before, so let’s delve into details to know more about this way of introducing hotfixes or patches in our code.&lt;/p&gt;

&lt;p&gt;A Monkey Patch is an expression used for such changes that are made during runtime to functionalities that are already defined in our code or in external libraries/components.&lt;/p&gt;

&lt;p&gt;This is usually done to modify the logic of your own functionality for some corner cases (example written in Javascript):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a class with a defined behaviour&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Monkey&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;sayMyName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Create a couple of Monkey instances&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;monkeyAndrew&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Monkey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Andrew&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;monkeyRyan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Monkey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ryan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Monkey Patch the method "sayMyName" for the instance monkeyAndrew&lt;/span&gt;
&lt;span class="nx"&gt;monkeyAndrew&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayMyName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;monkeyAndrew&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; I&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Call to the method "sayMyName"&lt;/span&gt;
&lt;span class="nx"&gt;monkeyAndrew&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayMyName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;monkeyRyan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayMyName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Andrew I
Ryan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we applied a Monkey Patch to the instance &lt;code&gt;monkeyAndrew&lt;/code&gt; of our &lt;code&gt;Monkey&lt;/code&gt; class. That way, this change of behaviour was only applied to that object and not to the rest of &lt;code&gt;Monkey&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;Monkey patches can also be applied to external libraries or components if the language allows such modifications (example written in Python):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt;

&lt;span class="c1"&gt;# Print PI
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;
&lt;span class="mf"&gt;3.141592653589793&lt;/span&gt;

&lt;span class="c1"&gt;# Monkey patch PI and print it
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;
&lt;span class="mf"&gt;3.1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This practice can be useful if there is a specific case where you need to change the default behaviour. However, I think is not very recommendable. Introducing a Monkey Patch creates an unexpected logic and it can make error handling and identification much more difficult than it should be.&lt;/p&gt;

&lt;p&gt;I see the point of applying Monkey Patches in unit testing. When we are creating stubs we are just basically faking the real call to the method with the result that we need. In any other case, if it’s possible is better to extend the class or the module by inheritance or to wrap them adding the functionality you need.&lt;/p&gt;

&lt;p&gt;If you liked this article, please follow me on twitter &lt;a href="https://dev.to@javihache_dev"&gt;https://twitter.com/javihache_dev&lt;/a&gt; and check out my personal blog &lt;a href="//javihache.dev"&gt;https://javihache.dev/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>python</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
