JAVA: Perbedaan antara Thread.start() dan Thread.run()

From OnnoWiki
Revision as of 09:54, 18 May 2022 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search

Dalam konsep multi-threading Java, start() dan run() adalah dua method yang paling penting. Berikut adalah beberapa perbedaan antara metode Thread.start() dan Thread.run():


Pembuatan New Thread:

Ketika sebuah program memanggil method start(), sebuah thread baru dibuat dan kemudian method run() dijalankan. Tetapi jika kita secara langsung memanggil method run() maka tidak ada thread baru yang akan dibuat dan method run() akan dieksekusi sebagai pemanggilan method normal pada thread panggilan saat ini itu sendiri dan tidak ada multi-threading yang akan terjadi.

Mari kita pahami dengan sebuah contoh:


class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
        System.out.println("run() method called");
    }
}
  
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.start();
    }
}

Output:

Current thread name: Thread-0
run() method called

Seperti yang dapat kita lihat pada contoh di atas, ketika kita memanggil method start() dari instance class thread kita, thread baru dibuat dengan nama default Thread-0 dan kemudian method run() dipanggil dan semua yang ada di dalamnya dieksekusi pada thread yang baru dibuat.

Sekarang, mari kita coba memanggil method run() secara langsung bukan method start():


class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
  
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.run();
    }
}

Output:

Current thread name: main
run() method called

Seperti yang dapat kita lihat pada contoh di atas, ketika kita memanggil method run() dari class MyThread kita, tidak ada thread baru yang dibuat dan method run() dijalankan pada thread saat ini, yaitu main thread. Oleh karena itu, tidak ada multi-threading yang terjadi. Metode run() dipanggil sebagai pemanggilan fungsi normal.


Multiple invocation:

In Java’s multi-threading concept, another most important difference between start() and run() method is that we can’t call the start() method twice otherwise it will throw an IllegalStateException whereas run() method can be called multiple times as it is just a normal method calling.

Let us understand it with an example:

Dalam konsep multi-threading Java, perbedaan terpenting lainnya antara metode start() dan run() adalah bahwa kita tidak dapat memanggil metode start() dua kali jika tidak, metode ini akan mengeluarkan IllegalStateException sedangkan metode run() dapat dipanggil beberapa kali sebagai itu hanya pemanggilan metode biasa.

Mari kita pahami dengan sebuah contoh:


class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
  
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.start();
        t.start();
    }
}

Output:

Current thread name: Thread-0
run() method called
Exception in thread "main" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:708)
    at GeeksforGeeks.main(File.java:11)

As we can see in the above example, calling start() method again raises java.lang.IllegalThreadStateException.

Now, let us try to call run() method twice:

Seperti yang dapat kita lihat pada contoh di atas, memanggil metode start() kembali memunculkan java.lang.IllegalThreadStateException.

Sekarang, mari kita coba memanggil metode run() dua kali:


class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.run();
        t.run();
    }
}

Output:

Current thread name: main
run() method called
Current thread name: main
run() method called

As we can see in the above example, calling run() method twice doesn’t raise any exception and it is executed twice as expected but on the main thread itself.


As we can see in the above example, calling run() method twice doesn’t raise any exception and it is executed twice as expected but on the main thread itself.


Summary
start() run()
Creates a new thread and the run() method is executed on the newly created thread. No new thread is created and the run() method is executed on the calling thread itself.
Can’t be invoked more than one time otherwise throws java.lang.IllegalStateException Multiple invocation is possible
Defined in java.lang.Thread class. Defined in java.lang.Runnable interface and must be overriden in the implementing class.




Ringkasan
Mulailah() Lari()
Membuat utas baru dan metode run() dijalankan pada utas yang baru dibuat. Tidak ada utas baru yang dibuat dan metode run() dijalankan pada utas panggilan itu sendiri.
Tidak dapat dipanggil lebih dari satu kali jika tidak, akan memunculkan java.lang.IllegalStateException Beberapa permintaan dimungkinkan
Didefinisikan dalam kelas java.lang.Thread. Didefinisikan dalam antarmuka java.lang.Runnable dan harus diganti di kelas implementasi.



Referensi