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

Java 8 ile gelen Lambda Expressions özelliğine merhaba diyelim

Lambda ifadeleri C# dilinde yıllardır vardı ve sonunda Java diline de bu özellik eklendi. Peki bu lambda expression özelliği hangi amaçla kullanılıyor?
Amacımız şu: Belli bir işi yapma biçimini (fonksiyonaliteyi), bir metoda parametre olarak geçmek istiyoruz. Başka bir deyişle, kodu sanki veriymiş gibi parametre olarak geçmek istiyoruz. Peki eskiden bunu yapamıyormuyduk Java ile? Yapabiliyorduk ama Anonymous class yöntemi ile yapabiliyorduk. Aşağıda vereceğim basit örnekte aradaki farkı görebileceğiz:

public class Program {

    public static class Person {
        private int age;
        private String name;

        Person(String name, int age) {
            this.age = age;
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public String getName() {
            return name;
        }
    }

    public interface GreetingMethod {
        int howManyTimes(Program.Person p);
    }

    public static void main(String[] args) {

        Person bahadir = new Person("Bahadir", 32);
        Person bilge = new Person("Bilge", 2);

        Person anonymous = new Person("Anon", 18);

        // Lambda expression ile:
        new Program().greet(bahadir, p -> p.getAge() > 30 ? 2 : 1);
        new Program().greet(bilge, p -> p.getAge() > 20 ? 2 : 1);

        // Anonymous class ile pek de sade bir görünüm yok gibi:
        new Program().greet(anonymous, new GreetingMethod() {
            @Override
            public int howManyTimes(Person p) {
                if (p.getAge() > 10)
                    return 2;

                return 0;
            }
        });

    }

    public void greet(Person person, GreetingMethod greetingMethod) {

        for (int i = 0; i < greetingMethod.howManyTimes(person); ++i) {
            System.out.println("Hello " + person.getName() + "!");
        }

    }
}

Lambda ifadeleri, tek metotlu arayüzleri daha sade bir biçimde parametre olarak geçmemize olanak tanıyor.
Geçen gün izlediğim bir sunumda, konuşmacı lamda ifadesi içeren bir örnek verecekti ama önce Java programcılarını korkmamaları için uyarmıştı. Artık böyle bir uyarıya gerek kalmayacak.

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