<?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: thewantedpk</title>
    <description>The latest articles on DEV Community by thewantedpk (@kumarp03).</description>
    <link>https://dev.to/kumarp03</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%2F241523%2F8a185237-863b-41e5-9b30-08626379b1df.JPG</url>
      <title>DEV Community: thewantedpk</title>
      <link>https://dev.to/kumarp03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kumarp03"/>
    <language>en</language>
    <item>
      <title>ReEntrant Locks</title>
      <dc:creator>thewantedpk</dc:creator>
      <pubDate>Fri, 01 May 2020 22:19:13 +0000</pubDate>
      <link>https://dev.to/kumarp03/reentrant-locks-5cdn</link>
      <guid>https://dev.to/kumarp03/reentrant-locks-5cdn</guid>
      <description>&lt;p&gt;How many of you still need to use (for non-ownership lock release) or have a code base which uses semaphores (shorturl.at/fjzPW)  ?  At least, I have not seen a code base in last 8+ years with semaphores in it. Based on my discussion with few fresh dev interview candidates in the past , I thought it might be a good idea to share some light on the term reentrant locks. The word itself is self explanatory. A lock which can be entered again or acquired again. The only conditions is - "by the same thread". Can be better described with a code example &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private Lock lock = new ReentrantLock(true);

private void lockMyselfWithoutDeadLock(int counter){
    if (counter &amp;lt;= 0){
        return;
    }
    lock.lock();
    System.out.println("Call Myself");
    lockMyselfWithoutDeadLock(counter-1);
    lock.unlock();
}

public static void main(String[] args){
    ReEntrantLock reEntrantLock = new ReEntrantLock();
    reEntrantLock.lockMyselfWithoutDeadLock(5);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Any thread which calls above method with a positive number and is able to acquire lock once, will be able to call itself, until returned and thus re-acquiring the lock multiple times. &lt;/p&gt;

&lt;p&gt;TO explain a non-reentrant lock,I would use an example of Semaphore. Think of it like a ticketed entry point which can allow a certain number of permits. A thread which has a valid permit can use that permit once to enter. If a thread can get one more permit, it can enter the lock one more time. Main point is that there is a limit. Its not like re-entrant locks where same thread can gain the lock any number of times. Following example will showcase the scenario &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   private Semaphore lock = new Semaphore(2); // 2 Permits

   private void lockMyselfWithDeadLock(int counter) throws 
                                        InterruptedException 
   {
     if (counter &amp;lt;= 0){
         return;
     }
     lock.acquire();
     System.out.println("Call Myself");
     lockMyselfWithDeadLock(counter-1);
     lock.release();
   }

   public static void main(String[] args) throws InterruptedException {
     NonReEntrantLock nonReEntrantLock = new NonReEntrantLock();
     nonReEntrantLock.lockMyselfWithDeadLock(5);
   }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above program will be in a deadlock if called with an argument greater than 2 as semaphore holds only 2 permits. I hope this will help someone trying to learn concurrency !!&lt;/p&gt;

</description>
      <category>java</category>
      <category>multithreading</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Beginners Guide to Redux App</title>
      <dc:creator>thewantedpk</dc:creator>
      <pubDate>Mon, 07 Oct 2019 03:26:52 +0000</pubDate>
      <link>https://dev.to/kumarp03/beginners-guide-to-redux-app-a0g</link>
      <guid>https://dev.to/kumarp03/beginners-guide-to-redux-app-a0g</guid>
      <description>&lt;p&gt;I am new to React and as of now only use it for my side projects. While learning React, there were plenty of tutorials on React as Beginner, still I had hard time understanding how all pieces together solve the puzzle. So I am writing this blog for someone like me, who is just looking to understand, how all the pieces go together in a redux based app. The application that we will create here is a non fancy application which puts a random number in the store with each click and toggles the state flag from empty to non-empty.  &lt;/p&gt;

&lt;p&gt;In any redux application, we need following pieces :&lt;/p&gt;

&lt;h2&gt;
  
  
  Store
&lt;/h2&gt;

&lt;p&gt;This is an object that represents the main state of the application. Based on the application requirement, it can be a simple key-valued object or a very heavy nested object. In our case , we have a simple object with only two attributes :&lt;br&gt;
    • randomNums : An array of numbers&lt;br&gt;
    • isEmpty : A flag used to show if the above array is empty or not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const defaultState = {
               randomNums : [],
               isEmpty : true
    };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Action
&lt;/h2&gt;

&lt;p&gt;They are the medium through which an application interacts with the store. They are the payloads/objects transferring data from application to store. All actions are plain JavaScript objects with a mandatory attribute named ‘type’ which has string value describing the action to perform on the store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
        type : 'REPLACE',     -----This is the action
        randomNum            ----- this is the actual payload
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  ActionCreators
&lt;/h2&gt;

&lt;p&gt;These are the functions returning action objects as defined in step 2. All action creators can be combined in a single file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function replace(randomNum){
    return {
        type : 'REPLACE',
        randomNum
    }
}

export function toggleFlag(){
    return {
        type : 'TOGGLE'
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Reducers
&lt;/h2&gt;

&lt;p&gt;Reducers define how an application state changes, in response to the action being sent to the store. They use the action’s  ‘type’ to determine its impact on the store. You can think of reducer taking an action and state as input and producing a new state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const appendItem = (state=[],action) =&amp;gt; {
    switch(action.type){
        case 'REPLACE':
            return {randomNums : [action.randomNum],
            isEmpty : false};
        case 'TOGGLE':
            return {randomNums : [],
            isEmpty : true};           
        default : 
            return state;
    }
}

export default appendItem;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now the question is, how our components can access and modify state. What that means is , our components should be able to access the state and call the type of action (defined in actionCreators) as suitable. So to achieve that task, I will be overriding following two functions &lt;/p&gt;

&lt;h2&gt;
  
  
  mapStateToProps
&lt;/h2&gt;

&lt;p&gt;This method maps the state to the application props. Return the object with only properties needed by the components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    function mapStateToProps(state) {
            return {
               randomNums : state.randomNums,
               isEmpty : state.isEmpty
            }
        }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  mapDispatchToProps
&lt;/h2&gt;

&lt;p&gt;This method binds the dipatch function to the Props. Dispatch is the function of redux store which propagates an action and causes the state change. BindActionCreators binds the actions to be dispatched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      function mapDispatchToProps(dispatch){
          return bindActionCreators(actionCreators,dispatch);
      }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;react-redux module provides a connect function to bind dispatch and state to our component. As we can see below, it can take two arguments :&lt;br&gt;
  a.) mapStateToProps&lt;br&gt;
  b.) mapDispatchToProps&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     export default connect(mapStateToProps,mapDispatchToProps)(Lister);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Lister Component
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import createReactClass from 'create-react-class';
import App from '../App.css'


const Lister = createReactClass({
    render(){
       return (
            &amp;lt;div className="App"&amp;gt;
                &amp;lt;button onClick={() =&amp;gt; this.props.append(Math.random()*100)}&amp;gt;Append&amp;lt;/button&amp;gt;
                &amp;lt;button onClick={() =&amp;gt; this.props.toggleFlag()}&amp;gt;Clear&amp;lt;/button&amp;gt;
                &amp;lt;p&amp;gt;The state is  : {this.props.isEmpty ? 'Empty':'Populated'}&amp;lt;/p&amp;gt;
            {this.props.randomNums.map((randomNum,i)=&amp;gt; {
                return (
                    &amp;lt;div className='App' key={i}&amp;gt;
                        &amp;lt;p&amp;gt;
                            &amp;lt;strong&amp;gt;
                                {randomNum}
                            &amp;lt;/strong&amp;gt;
                        &amp;lt;/p&amp;gt;
                    &amp;lt;/div&amp;gt;
                )
            })}           
        &amp;lt;/div&amp;gt;
       )
    }
});

export default Lister;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now lets create our store using the redux createStore function. Lets provide it with a default state and reducer which can actually change the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const defaultState = {
    randomNums : [],
    isEmpty : true
  };
  
const store = createStore(appender,defaultState);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Last piece to complete the application is to provide the store to the application. React-redux module provides the Provider tag which provides access of store to the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(
    &amp;lt;Provider store={store}&amp;gt;
        &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
