<?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 Vusala Hasanli on Medium]]></title>
        <description><![CDATA[Stories by Vusala Hasanli on Medium]]></description>
        <link>https://medium.com/@hasanli.vusala.73?source=rss-84d6f2b6963------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Vusala Hasanli on Medium</title>
            <link>https://medium.com/@hasanli.vusala.73?source=rss-84d6f2b6963------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:08:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hasanli.vusala.73/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[How does singleton bean serve multiple requests at the same time in Spring?]]></title>
            <link>https://medium.com/@hasanli.vusala.73/how-does-singleton-bean-serve-multiple-requests-at-the-same-time-in-spring-f4c9d797dec9?source=rss-84d6f2b6963------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4c9d797dec9</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[singleton]]></category>
            <category><![CDATA[multitasking]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Vusala Hasanli]]></dc:creator>
            <pubDate>Sat, 23 Nov 2019 16:11:52 GMT</pubDate>
            <atom:updated>2020-04-14T20:11:11.558Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yNeGaUP37PXRlYsphtCxjg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@niko_photos?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">niko photos</a> on <a href="https://unsplash.com/s/photos/tree?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>When I started to learn Spring two “difficult” questions was mainly running in my mind:</p><ol><li>How is singleton bean created and then how that single bean autowired in different classes?</li><li>Imagine this case:</li></ol><p>There are 2 users which one of them wants to login and another one wants to create report in our application at the same time. And both of login and createReport methods use userService bean which is scoped as singleton. In this case do those methods use that singleton bean sequentially? Otherwise how does singleton bean serve multiple requests concurrently?</p><p>To answer them was not difficult as I think. Just it was needed to clarify simple, but important points. That is why I will try to describe them with basic code examples. Let’s begin:</p><p>1.It would be useful if we talk about Spring container at first. Because I think this will help you to describe process better in your mind.</p><p>Spring container creates beans in it. After creating needed beans it injects dependencies of them. The container gets its instructions by reading configuration metadata(XML or Java annotations). So after Spring container is initialized your application is ready to use as described in following picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Wrbjw8fAJJR5RaIIQb0g6g.png" /><figcaption><a href="https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/images/container-magic.png">Photo of container on Spring documentation</a></figcaption></figure><p>When you define a bean definition like following you tell container that it must create only one instance for that bean definition in the container:</p><pre>        &lt;bean id=”accountDao” class=”…” scope=”singleton”/&gt;</pre><p>This single instance is stored in a cache of such singleton beans. Then Spring container returns this cached object to all requests and references for beans with that bean definition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*yUCdcL_qFbe7gba2K2250Q.png" /><figcaption><a href="https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/images/singleton.png">Photo of singleton on Spring documentation</a></figcaption></figure><p>If we want show above example with new() operator for describing simplified view of what Spring container does on startup of application, we can code as following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/871f16b750bd10172002da2bc71da39a/href">https://medium.com/media/871f16b750bd10172002da2bc71da39a/href</a></iframe><p>I think there is no need for more explanation. Example shows how same bean is used for different classes.</p><p>2.In Spring every request is executed in separate thread. For example,when 2 users want to login at the same time, JVM creates 2 threads: one thread for first user, another one for second user.</p><p>For showing this, following simple code snippet can help us. If you sent two requests for login method concurrently and print current thread name(which the requested login method is executed), you will see two different thread names there. This shows us every request is executed in separate thread:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a573c6069be7bb042b1f456446fdee4/href">https://medium.com/media/4a573c6069be7bb042b1f456446fdee4/href</a></iframe><pre>Result:<br>http-nio-8080-exec-9 ----------- Ben ----------  Sat Nov 23 20:01:35<br>http-nio-8080-exec-6 ----------- Kate ---------- Sat Nov 23 20:01:35 </pre><pre>You can send concurrent requests with help of curl command like this:</pre><pre>curl  <a href="http://localhost:8080/login/Ben">http://localhost:8080/login/Ben</a> &amp; curl <a href="http://localhost:8080/login/Ben">http://localhost:8080/login/</a>Kate</pre><p>And these threads work with singleton bean separately. How? Let’s speak about memory allocation in Java a little bit.</p><p>In Java every object is created in heap. Heap is globally shared memory. That is why, every thread can access objects in heap.</p><p>But stack is used for execution of only one thread. In that thread when a method is invoked a new block is created in stack in LIFO( Last-In-First-Out) order . This block holds local primitive values and reference to other objects in the method. And stack memory cannot be accessed by other threads.</p><p>So when we create singleton bean, it resides in heap. Because of heap is accessible from anywhere of application every created thread can point to that singleton bean. And how is this happening? When the thread request the singleton bean, it is going to refer (with help of reference variable in stack) to the bytecode of singleton bean in heap. So multiple threads can refer singleton bean at the same time. The compiler is going to point to the same bytecode and simply execute it and store method specific values in corresponding blocks in stack separately. There is no restriction preventing the compiler from doing this. The only restriction that the Singleton class puts on the JVM is that it can have only one instance of this class in the heap. And this is why ideal singleton bean must be stateless. Otherwise concurrency issues can occur.</p><p>There is some points which can be added. But for not being too long I decided to write them in second part. Hope this will help you to understand process clearly.</p><p>And if you liked this article, click the 👏 button, please. So let me know how much it was useful for you. Happy coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4c9d797dec9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why do we need interfaces in Java?]]></title>
            <link>https://medium.com/@hasanli.vusala.73/why-do-we-need-interfaces-in-java-27f6f68096c5?source=rss-84d6f2b6963------2</link>
            <guid isPermaLink="false">https://medium.com/p/27f6f68096c5</guid>
            <category><![CDATA[interfaces]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming-to-interface]]></category>
            <category><![CDATA[oop]]></category>
            <dc:creator><![CDATA[Vusala Hasanli]]></dc:creator>
            <pubDate>Sat, 19 Jan 2019 13:50:16 GMT</pubDate>
            <atom:updated>2020-04-15T05:30:18.247Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why do we need interfaces in OOP?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*zVBX3yDKG9moFDDRxI7uoQ.png" /></figure><p>Hello:) Most of us feel confused about the usefulness of interfaces at first times. There are questions like “Why do we need interfaces? “, “What is an advantage of using interfaces? “ and etc. In this blog, we will try to find the answers to these questions.</p><p>Let’s begin with a simple example. Assume that you are a student and you must prepare for an exam. You know there will be several things which will distract you during exam preparation. We can give examples like mobile applications and friends. Let’s describe them as a class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98c1a340d6aecf60945c583614bb1237/href">https://medium.com/media/98c1a340d6aecf60945c583614bb1237/href</a></iframe><p>I think you will agree with me that not every friend and every mobile application can distract you. For example, we can have hardworking friends. Or we can install useful mobile applications for our smartphones. So adding distract() method to these classes is not good. That is why let’s describe these distractions more clear :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/196b0ae6f66ece3467e9d1592aedb91b/href">https://medium.com/media/196b0ae6f66ece3467e9d1592aedb91b/href</a></iframe><p>You know this exam is very difficult and you don’t have much time. That is why you decide to print a list of distractable things with a header like “SAY NO!” and stick on the wall in your room. How can you achieve this?</p><p>At the first time, you can think about inheritance. Because if we create superclass named as “Distraction” and both of Facebook class and AdventureLoverFriend class extend it, we can collect all distractable things in one list. Because we can refer subclass object with superclass reference variable. Then we can do the needed operation on this list. But Facebook and AdventureLoverFriend cannot extend Distraction class. Because one class cannot extend more than one class in Java.</p><p>At this time we can see how the interface is useful. Let’s create an interface named “Distractable” to demonstrate this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9a69ce8beef2edeb3e8d4043dd64204/href">https://medium.com/media/f9a69ce8beef2edeb3e8d4043dd64204/href</a></iframe><p>And implement it like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d39a715b0b7cd8ca6a1ee6f771b16d93/href">https://medium.com/media/d39a715b0b7cd8ca6a1ee6f771b16d93/href</a></iframe><p>As you see, interfaces allow us to define common behaviour that can be implemented by any class, regardless of its inheritance. Although AdventureLoverFriend class extends Friend class and Facebook class extends MobileApplication class, we can add common distractable behaviour to them by implementing Distractable interface. This means that we can “cut across” inheritance hierarchy to implement functionality as we see fit.</p><p>Since Java allows to refer implementation class object with interface reference variable, we can write following in ExamPreparation class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef37e267ca89842d5960705f0523ef8b/href">https://medium.com/media/ef37e267ca89842d5960705f0523ef8b/href</a></iframe><p>So we print the list as we want:</p><pre>                         SAY NO! <br>Go through your entire Facebook news feeds again and again :/<br>I’m having a party this weekend and would love for you to come ^_^</pre><p>And also consider that we only focus on their common distractable behaviour in <em>printList </em>method. We don’t care about their other behaviours. Because we look at them as Distractable object, not like Facebook or AdventureLoverFriend object in <em>printList </em>method. We can show it like this in code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/516dff009dd118ac88262e06ec704e4d/href">https://medium.com/media/516dff009dd118ac88262e06ec704e4d/href</a></iframe><p>There are other great details about interfaces in Java. I tried to explain just one of them. Hope it will be helpful:)</p><p>If you liked this article, click the 👏 button, please. So let me know how much it was useful for you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27f6f68096c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Encapsulation with simple example in Java]]></title>
            <link>https://medium.com/@hasanli.vusala.73/encapsulation-with-simple-example-in-java-b80f6df3885d?source=rss-84d6f2b6963------2</link>
            <guid isPermaLink="false">https://medium.com/p/b80f6df3885d</guid>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[encapsulation]]></category>
            <dc:creator><![CDATA[Vusala Hasanli]]></dc:creator>
            <pubDate>Sun, 16 Dec 2018 13:52:54 GMT</pubDate>
            <atom:updated>2020-04-15T05:37:31.265Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*rYrh5vEgqNEdugT7y1bjCQ.png" /></figure><p>Hello:) In this blog, I will try to explain encapsulation principle as simple as I can for beginners who cannot understand what is purpose of encapsulation.</p><p>If you do some research about it you know that encapsulation is one of 3 main principles of OOP. To achieve encapsulation in Java:</p><ol><li>declare variables of a class as private</li><li>provide public setter and getter methods to read and write the values of variables</li></ol><p>Like following example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6844a5996b109726263b3aceafaa25d/href">https://medium.com/media/d6844a5996b109726263b3aceafaa25d/href</a></iframe><p>Now let me write benefits of encapsulation:</p><ul><li>Other classes will not be able to access data directly because variables are private.</li><li>You can make the class as read-only(with creating only getter methods) or write-only(with creating only setter methods).</li><li>You can control your data.In setter and getter methods you can write logic for special conditions.</li><li>It improves flexibility and re-usability.</li></ul><p>After reading benefits of encapsulation, you can ask that how did you achieved these benefits by only declaring variables as private and setting setter&amp;getter methods for them?? Which problems can occur if we don’t follow encapsulation principle?</p><p>Most of beginners think about these questions at first time. But answers of them is not very difficult. Just we need to make our example more complex. Because it is very simple and cannot show how we prevent problems which can occur in future use of class. Let’s make some changes for it and explain above benefits one by one:</p><ol><li>Let’s assume that price of coat changes due to customer. If customer is student, he or she gets 20% discount. Then getPrice() and setPrice() methods should be like this:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/503ef99befd347dc47badf67e8465551/href">https://medium.com/media/503ef99befd347dc47badf67e8465551/href</a></iframe><p>Now think that there is no encapsulation. Coat class is as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e484865cb71770bbb14c611441d7cbf/href">https://medium.com/media/9e484865cb71770bbb14c611441d7cbf/href</a></iframe><p>And 2 programmers use your class. In that case these cannot be counted as wrong. Yes?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b47ade2becefe84bf4eac5fb5380450/href">https://medium.com/media/6b47ade2becefe84bf4eac5fb5380450/href</a></iframe><p>As you see, they don’t apply business logic which you define. But when you declare variables as private and users can access them with only getter&amp;setters they must apply only your business logic. That is why instance variables must be private.</p><p>2. Assume that your boss doesn’t want to change price and customer of coat after creating it with initial values. In this case code would be like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a2e7ac662e2fb9ac0c1ca0967f6d406/href">https://medium.com/media/2a2e7ac662e2fb9ac0c1ca0967f6d406/href</a></iframe><p>Now price of every coat is 100$ and customer of every coat is student. Instance variables are private and there is no setter methods for them. That is why users of Coat class cannot change price and customer of coat. Coat is read-only now.</p><p>3.As in case 1 we can write business logic as we need in getter and setter methods. At that time users of class will access only that data which you define business logic for it.</p><p>4. Assume that , later your boss decided that discount for students must be 30%. At that time you must change percent in setter method and your users doesn’t need any change in their code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acb3a5936fa49270f409269d1b30f73a/href">https://medium.com/media/acb3a5936fa49270f409269d1b30f73a/href</a></iframe><p>Hope this will be helpful for you. If you liked this article, click the 👏 button, please. So let me know how much it was useful for you.</p><p>Happy coding:)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b80f6df3885d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to format times like on Instagram in Java]]></title>
            <link>https://medium.com/@hasanli.vusala.73/how-to-format-times-like-on-instagram-in-java-9daa6f214694?source=rss-84d6f2b6963------2</link>
            <guid isPermaLink="false">https://medium.com/p/9daa6f214694</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Vusala Hasanli]]></dc:creator>
            <pubDate>Fri, 19 Oct 2018 04:53:47 GMT</pubDate>
            <atom:updated>2020-04-15T05:35:57.166Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I4G5q8LCWACTqPRG" /><figcaption>“person holding gray and beige roman numeral alarm clock” by <a href="https://unsplash.com/@lenneek?utm_source=medium&amp;utm_medium=referral">Elena Koycheva</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello :) This blog post is about formatting times like on Instagram:<br> -just now<br> -1 minute ago<br> -2 hours ago<br> -3 days ago and etc.</p><p>I will explain very simple solution in Java. Let’s start. First we need to know difference between times — fromDate and now for formatting times. There is very helpful API — <em>Duration.between(Temporal startInclusive, Temporal endExclusive)</em> for this in Java 8. Let’s find difference in days, hours and minutes with help of this API. We suppose that we have two LocalDateTime objects which named from and to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/620b9f1564877a5095e655b18ee40b99/href">https://medium.com/media/620b9f1564877a5095e655b18ee40b99/href</a></iframe><p>Then using above values we can begin to format times as following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04756b33583fe440edf42acc0d6832ba/href">https://medium.com/media/04756b33583fe440edf42acc0d6832ba/href</a></iframe><blockquote>Let’s look examples:<br>1.2018–10–19 08:09:01 — 2018–10–19 08:09:07<br>Just now</blockquote><blockquote>2.2018–10–19 08:02:09–2018–10–19 08:04:40<br> 2 minutes ago</blockquote><blockquote>3.2018–10–19 06:32:32 — 2018–10–19 08:10:30<br>1 hour ago</blockquote><blockquote>4.2018–10–13 09:03:26 — 2018–10–19 08:11:26<br>5 days ago</blockquote><blockquote>5.2018–10–03 13:27:31 — 2018–10–19 08:12:11<br>3 October</blockquote><p>So result code is following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91ad1894e9e407e5b9fad47ef6278e87/href">https://medium.com/media/91ad1894e9e407e5b9fad47ef6278e87/href</a></iframe><p>Hope, this will be helpful for you. If you liked this article, click the 👏 button, please. So let me know how much it was useful for you.</p><p>Happy coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9daa6f214694" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>