public class PrioritasThread {
    public static void main(String[] args) throws InterruptedException {
        int[] hasil = new int[3];
        int[] tingkat = {Thread.MIN_PRIORITY, Thread.NORM_PRIORITY, Thread.MAX_PRIORITY};
        String[] label = {"rendah", "normal", "tinggi"};

        System.out.println("prioritas thread main: " + Thread.currentThread().getPriority());

        Thread[] pekerja = new Thread[3];
        for (int i = 0; i < pekerja.length; i++) {
            int indeks = i;
            pekerja[i] = new Thread(() -> {
                int jumlah = 0;
                int awal = indeks * 1000 + 1;
                int akhir = (indeks + 1) * 1000;
                for (int n = awal; n <= akhir; n++) {
                    jumlah += n;
                }
                hasil[indeks] = jumlah;
            });
            pekerja[i].setName("pekerja-" + label[i]);
            pekerja[i].setPriority(tingkat[i]);
            pekerja[i].start();
        }

        for (Thread t : pekerja) {
            t.join();
        }

        for (int i = 0; i < pekerja.length; i++) {
            System.out.println(pekerja[i].getName() + " (prioritas "
                    + pekerja[i].getPriority() + ") menjumlahkan "
                    + (i * 1000 + 1) + "-" + ((i + 1) * 1000) + ": " + hasil[i]);
        }
    }
}
