Observability of the Java Virtual Machine

Image
The JVM is one of the most observable runtimes. It provides us lots of tools for troubleshooting a JVM application in production. 1. Thread observability Threads are how the JVM actually does work. When something is wrong in production, the symptom is almost always a thread: stopped, blocked, leaking etc. Thread dumps work on any JVM with no  instrumentation, no agents, no restarts. <Example project link with /threaddump endpoint>         // (1) Deadlock — two threads grab the same pair of locks in opposite order.         new Thread(() -> grab(LOCK_A, LOCK_B), "deadlock-A-then-B").start();         new Thread(() -> grab(LOCK_B, LOCK_A), "deadlock-B-then-A").start(); http://localhost:8080/actuator/threaddump To list the JVMS, we can use the command below. PS C:\observe-jvm> jps -lv 25296 jdk.jcmd/sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Microsoft\jdk-21.0.3.9-hotspot -Xms8m -Djdk.module.main=...

More fun with left shift operator

Lets say we want to have sum of two integers. But we are not allowed to use + and - operators. Below is a way to do that:

public class Solution {

    public static int getSum(int a, int b) {

        if (a > 0 && b > 0) {
            return positiveSum(a, b);
        }

        if (a < 0 && b < 0) {
            return negate(positiveSum(Math.abs(a), Math.abs(b)));
        }

        if (a > 0 && b < 0) {
            return minus(a, Math.abs(b));
        } else {
            return minus(b, Math.abs(a));
        }
    }

    private static int minus(int a, int b) {
        boolean[] result = new boolean[32];

        boolean carry = false;

        for (int i = 0; i < 32; ++i) {

            boolean ba = ((a & (1 << i))) != 0;
            boolean bb = (b & (1 << i)) != 0;

            result[i] = ba ^ bb;

            if (carry) {
                result[i] = !result[i];
            }

            if (ba == true) {
                carry = false;
            }

            if (ba == false && bb == true) {
                carry = true;
            }

        }

        int intResult = getIntResult(result);

        return intResult;


    }

    private static int positiveSum(int a, int b) {
        boolean[] result = new boolean[32];

        boolean carry = false;

        for (int i = 0; i < 32; ++i) {

            boolean ba = ((a & (1 << i))) != 0;
            boolean bb = (b & (1 << i)) != 0;

            result[i] = ba ^ bb ^ carry;

            if (carry == true) {
                if (result[i] == true) {
                    carry = false;
                }
            }

            if ((a & (1 << i)) != 0) {
                if ((b & (1 << i)) != 0) {
                    carry = true;
                }
            }
        }

        int intResult = getIntResult(result);

        return intResult;
    }

    private static int getIntResult(boolean[] result) {
        int intResult = 0;
        for (int i = 0; i < result.length; ++i) {

            if (result[i] == true)
                intResult |= (1 << i);

        }
        return intResult;
    }



    // negate a positive value
    private static int negate(int value) {
        if (value < 0) {
            throw new RuntimeException();
        }

        return minus(0, value);
    }

 
    public static void main(String[] args) {

        System.out.println(getSum(-17, -10));
        System.out.println(getSum(17, -10));
        System.out.println(getSum(-10, 17));
        System.out.println(getSum(-15, 32));
        System.out.println(getSum(15, 32));
    }
}

Comments

Popular posts from this blog

The WeakReference class, monitoring memory leak and garbage collection in a Java application

Simplescalar Simulator - Part 2: sim-outorder.c

Notes on Java Performance