How do I create a thread by extending Thread class?

There are two ways that we can use tho create a thread. First is by extending the java.lang.Thread class and the second way is by creating a class that implements the java.lang.Runnable interface. See How do I create a thread by implementing Runnable interface?

In this example we’ll extend the Thread class. To run a code in a thread we need to provide the run() method in our class. Let’s see the code below.

package org.kodejava.lang;

public class NumberPrinter extends Thread {
    private final String threadName;
    private final int delay;

    public NumberPrinter(String threadName, int delay) {
        this.threadName = threadName;
        this.delay = delay;
    }

    // The run() method will be invoked when the thread is started.
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Thread [" + threadName + "] = " + i);

            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        NumberPrinter printerA = new NumberPrinter("A", 1000);
        NumberPrinter printerB = new NumberPrinter("B", 750);

        printerA.start();
        printerB.start();
    }
}

The example result of our code is:

Thread [B] = 0
Thread [A] = 0
Thread [B] = 1
Thread [A] = 1
Thread [B] = 2
Thread [A] = 2
Thread [B] = 3
Thread [B] = 4
Thread [A] = 3
Thread [B] = 5
Thread [A] = 4
Thread [B] = 6
Thread [A] = 5
Thread [B] = 7
Thread [B] = 8
Thread [A] = 6
Thread [B] = 9
Thread [A] = 7
Thread [A] = 8
Thread [A] = 9

How do I get Java Home directory?

To get Java Home directory we can obtain it from system properties using the java.home key.

package org.kodejava.lang;

public class JavaHomeDirectory {
    public static void main(String[] args) {
        String javaHome = System.getProperty("java.home");
        System.out.println("JAVA HOME = " + javaHome);
    }
}

On my computer this code give me the following output:

JAVA HOME = C:\Program Files\Java\jdk-17

How do I get the username of operating system active user?

This example show you how to get operating system active user’s login name. We can obtain the username of current user by reading system properties using the user.name key.

package org.kodejava.lang;

public class GettingUserName {
    public static void main(String[] args) {
        String username = System.getProperty("user.name");
        System.out.println("username = " + username);
    }
}

How do I clear system property?

The System.clearProperty(String key) method enables you to remove a system property. The key must not be an empty string or a null value because it will cause the method to throw an IllegalArgumentException or a NullPointerException.

It will also check if a SecurityManager exists and if you don’t have a write permission to the system property a SecurityException is going to be thrown.

package org.kodejava.lang;

public class ClearProperty {
    public static void main(String[] args) {
        String key = "user.dir";
        System.out.println(key + " = " + System.getProperty(key));

        // The System.clearProperty() method available since Java 1.5
        System.clearProperty(key);
        System.out.println(key + " = " + System.getProperty(key));
    }
}

The code snippet above give us the following output:

user.dir = F:\Wayan\Kodejava\kodejava-example
user.dir = null

How do I get file separator symbol?

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\" (backslash) while Linux use "/" (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.

package org.kodejava.lang;

public class FileSeparatorExample {
    public static void main(String[] args) {
        // file.separator system property return the correct file 
        // separator for each different platform (Windows = \), 
        // (Linux = /)
        String dataFolder = System.getProperty("user.dir") +
                System.getProperty("file.separator") + "data";

        System.out.println("Data Folder = " + dataFolder);
    }
}