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=...

Revisiting Java Generics and Wildcard Types

Generics were added with Java 5. Before that you had to cast every object you read from a collection. With generics you tell the compiler what types of objects are permitted in collections. Then the compiler tells you when there is a problem.

If a class or interface has type parameters, then it is called a generic class/interface. Generic classes or interfaces can be used as "raw types". This is primarily for backward compatibility. We get a warning message from compiler when we use raw types.

List<Object> can be used but you cannot assign List<String> to this type. They are both subtypes of raw List type.
Set<Integer> set1 = new HashSet<>();
Set<Object> set3 = new HashSet<>();
// compile error.
set1 = set3;
But what if you don't care about the actual type of the collection elements? Lets say you want to have a method with 2 sets as parameter.

We can use Set<?> which means "set of any type".
public static void main(String[] args) {
        Set<Integer> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();

        calculate(set1, set2);

        Set<?> mySet = new HashSet<>();

        // compile error. You cannot do that when the type is uncertain.
        // you can do that by using raw types but you get a warning and it is not reccomended!
        // this means, wildcard type is useful to assign a reference to another
        mySet.add(new Integer(1));

        Set myRawSet = new HashSet();

        myRawSet.add(new Integer(2));

        // legal
        mySet = set1;
        mySet = set2;
    }

    private static void calculate(Set<?> set1, Set<?> set2) {
    }

Regarding arrays, it is illegal to create an array of a generic type. In Java, arrays and generics don't mix very well. 

Below is an example that illustrates the syntax of generics:
public class Test {

    // generic method
    public <T> T doSomething(T a, T b) {
        return null;
    }

    // generic method with bounded type
    public <T extends Comparable> T doSomething2(T a, T b) {
        return null;
    }

    class MyGenericClass<K> {

        public K operation() {
            return null;
        }
    }

    private void wildCardUnbounded() {
        MyGenericClass<?> variable = new MyGenericClass<>();

        MyGenericClass<Integer> variable2 = new MyGenericClass<>();

        variable = variable2;
    }

    private void wildCardBounded() {
        MyGenericClass<? extends Comparable> variable = new MyGenericClass<>();

        MyGenericClass<Object> variable2 = new MyGenericClass<>();

        // compile error: not comparable
        variable = variable2;

    }

    private void wildCardBounded2() {
        MyGenericClass<? super Comparable> variable = new MyGenericClass<>();

        MyGenericClass<Object> variable2 = new MyGenericClass<>();

        // legal because Object is super class of Comparable
        variable = variable2;
    }
}


Below is an example that uses bounded wildcard types:

public class UnionExample {

    public static <E> Set<E> union(Set<? extends E> s1,
                                   Set<? extends E> s2) {
        //..
        return null;
    }

    public static void main(String[] args) {
        Set<Integer> integers = Set.of(1, 2, 3);

        Set<Double> doubles = Set.of(1.2, 2.3, 4.5);

        Set<UnionExample> examples = new HashSet<>();

        // valid
        Set<Number> result = union(integers, doubles);

        // compile error: UnionExample is not a subclass of Number
        result = union(integers, examples);
    }
}

Differences between Java Generics and C# Generics

The most important difference seems to be the "type erasure" concept. In Java, generics are a compile time structure, in runtime the type of the object is erased (erasure) so you cannot have the type information at runtime. In C#, generics are also present at the CLR level, and you can get the type information at runtime.

It is an error in C# to create a Generic Type without providing any Type Arguments. Java permits creating Generic Types without providing any Type Arguments; these are called raw types. This is because of backward compatibility. Compiler gives a warning and using raw types is a bad practice for modern java code.

In Java you cannot use your type variable of a class inside a static method:

public class GenericWithStaticMethod<Tgt;{

    // compile error!
    public static void doSomething(T a) {

    }
}

You see this famous error message:

error: non-static class T cannot be referenced from a static context







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