Archive for the ‘java’ Category

Use graphviz to print graph, list, tree in java

February 19, 2021

Graphviz is an open source graph visualization software. We can use it to print different kinds of diagrams. You can read more about Graphviz here. We will use a simple java class that creates the dot file (graphviz input file) and uses that dot file to output an image using various code instructions. You can download the source code for the graph printer here. Below, I have pasted the source code. Before executing following commands make sure graphviz is installed. You need to be able to run “dot” command from system environment. All source code is available in this github repository.

(more…)

Java Reflection

February 14, 2021

Reflection let’s you access and modify fields and methods of a class at runtime. In below example, we will try to get values of a private field and execute private method. Here we will get private field age and print it first. Then we will change it and print that value. Next, we will execute a method with parameters. In all cases, we need to execute setAccessible to true, so that we can execute them.

(more…)

Number of Islands – II (LC)

January 2, 2021

Problem: https://leetcode.com/problems/number-of-islands-ii/

We are given a 2D grid of size mxn. Initially the whole area is filled with water ‘0’. We are then given another set of array called ‘positions’. At each iteration we go through each value of position (contains position in above 2D grid), where we fill the 2D grid with land ‘1’. After each of these operations we need to determine how many islands the mxn 2D grid has. Our result will contain list of number of islands the 2D grid contains after adding land at each value of position array.

There are couple of things that can change by just adding one block of land at position. If that block is adjacent (horizontally or vertically) to a land, it does not add a new island. If that block is adjacent to two island, it could merge both of them into a single island (thus decreasing the total number of islands).

We will use union-find algorithm to join disjoint sets to determine when islands merge and so on.

(more…)

Java Memory Management and Threading

July 19, 2020

Memory and Threads

When JVM starts, it creates a heap memory space and a main thread to start program execution. Java objects live in heap memory. Each thread in java has its own stack space, program counter and byte code interpreter. However, all threads share heap space of the process they belong to. This is the reason it is important to protect the shared heap memory from corruption. Java hands over control of a thread to operating system scheduler, that have their own rule on when and how long a thread executes on the CPU. When CPU switches to different threads for execution, it can leave old thread in inconsistent state, as the calculated values might not have been re-written back to the main memory. Other threads in same java process can read this inconsistent value, and thus corrupt the result of calculation. Java concurrency provides methods to protect and manage synchronous access to shared resources. Multiple threads can thus be executed concurrently, while still following consistent access to shared resources. Each object in java have a token called Monitor. Threads claim access to an object using this monitor. Other threads are blocked from reading and or modifying the object until previous thread relinquish access to the object’s monitor.

More