<?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: Piyush</title>
    <description>The latest articles on DEV Community by Piyush (@zirea3l).</description>
    <link>https://dev.to/zirea3l</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%2F783946%2F62bbcd6b-93e0-4f65-90a1-a39f9a115585.jpeg</url>
      <title>DEV Community: Piyush</title>
      <link>https://dev.to/zirea3l</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zirea3l"/>
    <language>en</language>
    <item>
      <title>Deadlocking</title>
      <dc:creator>Piyush</dc:creator>
      <pubDate>Fri, 07 Jan 2022 14:08:41 +0000</pubDate>
      <link>https://dev.to/zirea3l/deadlocking-3jkd</link>
      <guid>https://dev.to/zirea3l/deadlocking-3jkd</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Understanding Locks&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In Java, Lock is an interface available in the Java.util.concurrent.locks package. Java lock acts as thread synchronization mechanisms that are similar to the synchronized blocks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When threads in a process share and update the same data, their activities must be synchronized to avoid errors. &lt;strong&gt;&lt;em&gt;In Java&lt;/em&gt;&lt;/strong&gt;, this is done with the "synchronized" keyword, or with &lt;em&gt;wait&lt;/em&gt; and &lt;em&gt;notify&lt;/em&gt;. Synchronization is achieved by the use of locks, each of which is associated with an object by the JVM (Java Virtual Machine). For a thread to work on an object, it must have control over the lock associated with it, it must "hold" the lock. Only one thread can hold a lock at a time. If a thread tries to take a lock that is already held by another thread, then it must wait until the lock is released. When this happens, there is so called "contention" for the lock.&lt;br&gt;
There are four kinds of locks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Fat Locks:&lt;/em&gt; A fat lock is a lock with a history of contention (several threads trying to take the lock simultaneously), or a lock that has been waited on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Thin Locks:&lt;/em&gt; A thin lock is a lock that does not have any contention.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Recursive Locks:&lt;/em&gt; A recursive lock is a lock that has been taken by a thread several times without having been released.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Lazy Locks:&lt;/em&gt; A lazy lock is a lock that is not released when a 'critical section' is excited. Once a lazy lock is acquired by a thread, other threads that try to acquire the lock have to ensure that the lock is, or can be, released.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Spinning and Sleeping
&lt;/h2&gt;

&lt;p&gt;Spinning occurs when a thread that wants a specific lock continuously checks that lock to see if it is still taken, instead of yielding CPU-time to another thread.&lt;br&gt;
Alternatively, a thread that tries to take a lock that is already held waits for notification from the lock and goes into a sleeping state. The thread will then wait passively for the lock to be released.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lock Chains
&lt;/h2&gt;

&lt;p&gt;Several threads can be tied up in what is called &lt;em&gt;lock chains&lt;/em&gt;. Although they appear somewhat complex, lock chains are fairly. They can be defined as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Threads A and B form a lock chain if thread A holds a lock that thread B is trying to take. If A is not trying to take a lock, then the lock chain is 'open'.&lt;/li&gt;
&lt;li&gt;If A-&amp;gt;B is a lock chain, and B-&amp;gt;C is a lock chain, then A-&amp;gt;B-&amp;gt;C is a more complete lock chain.&lt;/li&gt;
&lt;li&gt;If there is no additional thread waiting for a lock held by C, then A-&amp;gt;B-&amp;gt;C is a complete and open lock chain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Lock Chain Types
&lt;/h2&gt;

&lt;p&gt;There are three possible kinds of lock chains: open, Deadlock and Blocked lock chains.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open Chains&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open lock chains represent a straight dependency, thread A is waiting for B which is waiting for C, and so on. If we have long open lock chains, the application might be wasting time waiting for locks. We then want to consider how locks are used for synchronization in the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deadlock Chains&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A deadlocked, or circular, lock chain consists of a chain of threads, in which the first thread in the chain is waiting for the last thread in the chain. In the simplest case, thread A is waiting for thread B, while thread B is waiting for thread A. &lt;br&gt;
&lt;em&gt;"A deadlocked chain has no head."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blocked Chains&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A blocked lock chain is made up of a lock chain whose head thread is also part of another lock chain, which can be either open or deadlocked.&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Thread Congestion In Java</title>
      <dc:creator>Piyush</dc:creator>
      <pubDate>Sun, 02 Jan 2022 14:19:24 +0000</pubDate>
      <link>https://dev.to/zirea3l/thread-congestion-in-java-5chc</link>
      <guid>https://dev.to/zirea3l/thread-congestion-in-java-5chc</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thread Congestion&lt;/em&gt;&lt;/strong&gt; can occur when two or more threads are trying to access the same, guarded data structure at the same time. "Guarded" mean that the data structure is guarded using synchronized blocks or a concurrent data structure so that the data structure is thread safe. The resulting thread congestion means that the threads trying to access the shared data structure are spending a high amount of time waiting in line to access the data structure - wasting valuable execution time on waiting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s4i-6fEP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdddv22hqzeujqgde51b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s4i-6fEP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdddv22hqzeujqgde51b.png" alt="Image description" width="700" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thread Blocking Data Structure May Cause Thread Congestion
&lt;/h2&gt;

&lt;p&gt;A data structure which blocks threads from accessing it - depending on what other threads are currently accessing it, may cause thread congestion. If more than one thread accesses such a data structure at the same time, one or more of the threads may be queued up waiting to access the data structure. The queueing of threads is not visible in code. This queueing happens inside the JVM. Therefore thread congestion is not easy to spot simply by looking at the code. We need profiling tools to detect thread congestion.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Blocked Thread Loses Execution Time
&lt;/h2&gt;

&lt;p&gt;While a thread is blocked trying to execute a blocking data structure, it cannot do anything. While being blocked the thread loses possible execution time. The longer the thread is blocked, the more potential execution time it loses.&lt;/p&gt;

&lt;h2&gt;
  
  
  More Threads - Higher Congestion
&lt;/h2&gt;

&lt;p&gt;The more the number of threads that attempts to access a shared data structure, the higher the risk of thread congestion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alleviating Thread Congestion
&lt;/h2&gt;

&lt;p&gt;To lessen thread congestion we must reduce the number of threads trying to access the blocking data structure at the same time. There are several ways to reduce thread congestion namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple Data Structures&lt;/li&gt;
&lt;li&gt;Non-blocking Concurrency Algorithms&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Multiple Data Structures&lt;br&gt;
One way to alleviate thread congestion is to give each consuming thread its own queue, and have the producing thread distribute the objects among those data structures.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Non-blocking Concurrency Algorithms&lt;br&gt;
Another way is to use "non-blocking concurrency algorithms" where the threads accessing the data structure are never blocked.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Exceptions "against" Errors</title>
      <dc:creator>Piyush</dc:creator>
      <pubDate>Sun, 02 Jan 2022 08:26:59 +0000</pubDate>
      <link>https://dev.to/zirea3l/exceptions-against-errors-1007</link>
      <guid>https://dev.to/zirea3l/exceptions-against-errors-1007</guid>
      <description>&lt;h2&gt;
  
  
  Common misconception during programming
&lt;/h2&gt;

&lt;p&gt;In java, both 'Exceptions' and 'Errors' are subclasses of "java.lang.throwable" class.&lt;br&gt;
&lt;strong&gt;Error&lt;/strong&gt; refers to an illegal operation performed by the user which results in abnormal behavior of the program. Programming errors many a time remain undetected until the program is executed or compiled. Some errors inhibit the program from getting executed. Thus making it necessary to remove errors before compiling or executing. Errors are of three types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile-time errors&lt;/li&gt;
&lt;li&gt;Run-time errors&lt;/li&gt;
&lt;li&gt;Logical errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Compile-time errors&lt;/strong&gt;&lt;br&gt;
Compile-time errors are those errors which prevent the code from running because of an incorrect syntax such as a missing semicolon at the end of a statement or a missing bracket, etc. These errors are detected by the java compiler and an error message is displayed onto the screen while compiling. Compile-time errors are also referred to as "Syntax errors".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GMJyoclS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxwmb2f2r6mapu0ibeds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GMJyoclS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxwmb2f2r6mapu0ibeds.png" alt="Image description" width="424" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aquH2EGM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpjpitdpuausk63cgfd1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aquH2EGM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpjpitdpuausk63cgfd1.png" alt="Image description" width="488" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run-time errors&lt;/strong&gt;&lt;br&gt;
Run-time errors occur during the execution of the program. Sometimes these are discovered when the user enters an invalid data or data which is not relevant. Run-time errors occur when a program does not contain any syntax errors but asks the computer to do something that the computer is unable to reliably do. The compiler has no technique to detect these kind of errors. It is the JVM (Java Virtual Machine) which detects it while the program is running.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iCWOJC_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4evijzud90iug4r9yi0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iCWOJC_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4evijzud90iug4r9yi0.png" alt="Image description" width="612" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logical errors&lt;/strong&gt;&lt;br&gt;
When the program compiles and executes, but does the wrong thing or returns an incorrect result or shows no output; it is a logical error. These errors are neither detected by compiler nor by JVM. The system has no idea what the program is supposed to do, so it provides an additional information to help find the error. Logical errors are also called Semantic errors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rgWHQnU7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86ntfiw8y2abgvvpdrqp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rgWHQnU7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86ntfiw8y2abgvvpdrqp.png" alt="Image description" width="526" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nJIMv8Aa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sytfh7a2nmzu1nfjiq51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nJIMv8Aa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sytfh7a2nmzu1nfjiq51.png" alt="Image description" width="174" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exceptions&lt;/strong&gt; in java refer to an unwanted event, which occurs during the execution of a program, i.e. at run-time, that disrupts the normal flow of the program's instructions. Exceptions are the conditions that occur at runtime and may cause the termination of the program. But they are recoverable using try, catch and throw keywords. Exceptions are divided into two categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checked exceptions&lt;/li&gt;
&lt;li&gt;Un-checked exceptions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Checked exceptions&lt;/em&gt; are like IOException known to the compiler at compile time .&lt;br&gt;
&lt;em&gt;Un-checked exceptions&lt;/em&gt; are like ArrayIndexOutOfBoundException known to the compiler at runtime, mostly caused by the program written by programmer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sgD1RHQV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kvxzwpb725f8vp6bcip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sgD1RHQV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kvxzwpb725f8vp6bcip.png" alt="Image description" width="389" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Kadane's Algorithm</title>
      <dc:creator>Piyush</dc:creator>
      <pubDate>Sat, 01 Jan 2022 14:32:41 +0000</pubDate>
      <link>https://dev.to/zirea3l/kadanes-algorithm-58h6</link>
      <guid>https://dev.to/zirea3l/kadanes-algorithm-58h6</guid>
      <description>&lt;h2&gt;
  
  
  Maximum Subarray Sum: Kadane's Algorithm
&lt;/h2&gt;

&lt;p&gt;If you are competitive programming or preparing for campus placements or technical interviews, you might have probably come across something like this:&lt;br&gt;
*&lt;em&gt;Given an array arr[] of N integers. Find the contiguous sub-array which has the maximum sum and return its sum. *&lt;/em&gt;&lt;br&gt;
If not, so does the "Kadane's Algorithm" rings any bell?&lt;br&gt;
&lt;em&gt;Kadane's algorithm is an iterative dynamic programming approach in which we search for a maximum sum contiguous subarray within a one-dimensional numeric array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working of Kadane's Algorithm&lt;/strong&gt;&lt;br&gt;
Some may think okay it's just the sum of all elements in an array. But an array may have negative integer elements, which decreases the total array's sum.&lt;br&gt;
Thus we can say that the sum of all array elements isn't always the case.&lt;br&gt;
A simple idea of Kadane's algorithm is to look for all positive contiguous segments of array and keep track of the maximum sum contiguous subarray among all positive segments.&lt;br&gt;
An array is a contiguous memory block. So, a subarray is a slice of contiguous array that maintains the order of the elements.&lt;br&gt;
Let us consider an array:&lt;br&gt;
&lt;strong&gt;arr={1,2,3,4}&lt;/strong&gt;&lt;br&gt;
For this array, the sub-arrays are:&lt;br&gt;
&lt;strong&gt;For elements at 0th index&lt;/strong&gt;   {1},{1,2},{1,2,3},{1,2,3,4}&lt;br&gt;
&lt;strong&gt;For elements at 1st index&lt;/strong&gt;   {2},{2,3},{2,3,4}&lt;br&gt;
&lt;strong&gt;For elements at 2nd index&lt;/strong&gt;   {3},{3,4}&lt;br&gt;
&lt;strong&gt;For elements at 3rd index&lt;/strong&gt;   {4}&lt;br&gt;
Now we know sub-arrays for an array.&lt;br&gt;
&lt;em&gt;Brute Force Approach&lt;/em&gt;&lt;br&gt;
The brute force solution calculates the sum of each sub-array and then compares the results to determine the maximum sum of all sub-arrays sums. This method is straight forward, but it is not used commonly.&lt;br&gt;
&lt;strong&gt;This is because it has a time complexity of O(N^3) and O(N) space complexity.&lt;/strong&gt;&lt;br&gt;
As we know, while writing any program, Time and Space Complexity plays a vital role in choosing the algorithm. Therefore, Kadane's algorithm is used because of its advantage considering time and space complexity.&lt;br&gt;
&lt;em&gt;Kadane's Algorithm&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we will consider two elements, one which stores the maximum end of the subarray and another which stores the maximum sum far.&lt;/li&gt;
&lt;li&gt;Let the two elements be named variables as max_end and max_far, respectively.&lt;/li&gt;
&lt;li&gt;We will initialise both as 0.&lt;/li&gt;
&lt;li&gt;Each time we get a positive sum, we compare it with max_end and update max_far if it is greater than it.
The Time and Space complexity are as:
&lt;strong&gt;Time Complexity: O(N)&lt;/strong&gt;
&lt;strong&gt;Space Complexity: O(1)&lt;/strong&gt;
Comparing the "Time and Space Complexity" of 'Brute Force Approach' and of 'Kadan's Algorithm', the latter is better when it comes to solving the same problem.
Hence, Kadane's algorithm is preferred method when it comes to finding the maximum contiguous sub-array sum.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_3pkr1ft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nk5ukpr0rvpnae3h7aoa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_3pkr1ft--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nk5ukpr0rvpnae3h7aoa.png" alt="Image description" width="800" height="313"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Special Case&lt;/strong&gt;&lt;br&gt;
For all the negative numbers in an array, all the elements in array are less than 0. So, we compare the sum of the consecutive elements with the current element from the array.&lt;/p&gt;

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