Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

JAva Threads.

Presentasi serupa


Presentasi berjudul: "JAva Threads."— Transcript presentasi:

1 JAva Threads

2 Konsep Thread Thread merupakan unit eksekusi individual dan independen yang merupakan bagian dari proses Multiple thread dapat bekerja sama untuk menyelesaikan satu tugas tertentu Java menyediakan built-in support untuk multithreading

3 Konsep thread a) Multiple thread pada multiple CPU
b) Multiple thread pada single CPU

4 Manfaat Mudah untuk diprogram - 1 thread per task
Dapat memberikan performa yang lebih baik - Thread dijalankan hanya bila dibutuhkan Multiple thread dapat berbagi resource Mengutilisasi multi prosessor jika tersedia

5 Kelemahan Desain sistem menggunakan multiple thread yang kurang baik dapat menimbulkan deadlock Overhead dalam melakukan switching diantara thread

6 Impelementasi thread Extending Class Thread
- harus menggunakan method run() - thread berakhir ketika method run() selesai dieksekusi - panggil start() untuk memulai menjalankan thread Implement runnable interface - Interface runnable harus di implemented oleh class yang instancenya akan dieksekusi oleh thread. Class harus mendefinisikan method run() - memanggil konstruktor thread dengan instance dari runnable

7 Extending Thread Class
class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); public class Main2 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start();

8 output thread2: hello world thread1: hello world Output bervariasi pada run time, hal ini disebut race condition (dianggap bug pada program) See the variation in output.

9 Implement Runnable class Output implements Runnable {
private String toSay; public Output(String st) { toSay = st; } public void run() { try { for(;;) { System.out.println(toSay); Thread.sleep(1000); } catch(InterruptedException e) { System.out.println(e); class Program { public static void main(String [] args) { Output out1 = new Output(“Hello”); Output out2 = new Output(“There”); Thread thr1 = new Thread(out1); Thread thr2 = new Thread(out2); thr1.start(); thr2.start();

10 Java Thread Control _.start(): Memulai eksekusi thread
wait() and notify(): Untuk sinkronisasi _.stop(): kills spesifik thread(deprecated) _.suspend() and resume(): deprecated _.join(): menunggu thread spesifik selesai dieksekusi _.setPriority(): 0 to 10 (MIN_PRIORITY to MAX_PRIORITY); 5 is default (NORM_PRIORITY)

11 Join Example

12 … hello world1 Thread is done!
output

13 Java thread scheduling
Thread prioritas tertinggi dijalankan - jika lebih dari satu, arbitrary yield(); thread berjalan melepaskan cpu agar proses lain dengan prioritas yang sama bisa berjalan Sleep(ms): stop eksekusi untuk waktu yang ditentukan - thread prioritas rendah dapat berjalan

14 class MyThread extends Thread {
private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); yield(); public class Main3 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start();

15 Output thread1: hello world thread2: hello world
Run on Mac OS X, 3/14/08

16 Java thread state new: thread diciptakan tapi belum berjalan
runnable: thread telah diciptakan dan dimulai, telah dapat dijalankan blocked: telah diciptakan dan dimulau tapi tidak dapat berjalan sebab dalam keadaan menunggu suatu event terjadi dead: thread selesai dieksekusi atau dihentikan

17 States of Java Threads runnable new dead blocked stop(),
end of run method start() runnable new dead wait(), I/O request, suspend() notify(), I/O completion, resume() blocked

18 Basic Tools untuk Synchronization dalam java
Metode sinkronisasi Objek sinkronisasi Methods wait notify notifyAll

19 Synchronized Methods: Monitors
Menggunakan synchronized keyword pada method Cth., public synchronized void SetValue() { // Update instance data structure. // ketika thread dieksekusi disini, akan secara eksklusif memiliki monitor lock } Menyediakan mekanisme mutual exclusion Kunci secara implisit disediakan- mengijinkan paling banyak satu thread yang mengakses satu method pada satu waktu Digunakan per method, tidak semua method dalam class otomatis menggunakan metode ini

20 Example Construct a queue (FIFO) data structure that can be used by two threads to access the queue data in a synchronized manner Producer thread: Adds data into queue Consumer thread: Removes data from queu For one instance of the queue, only one thread should be able to modify the queue, i.e., we should have mutual exclusion on methods of one instance of the queue

21

22

23 Point out that this is a polling solution– should really make the consumer block to wait for the queue to be non-empty

24

25 Output

26

27 Error

28 Hal ini merupakan salah satu masalah race condition
Masalah pada implementasi ini, consumer memprint hasil kelayar sehingga melambatkannya dengan signifikan dibanding proses produser yang lebih cepat, akhirnya proses produser akan memenuhi queue, dan menyebabkan ruang heap terpakai habis Hal ini merupakan salah satu masalah race condition Hasil bergantung dari kecepatan dari proses Solusi: memblok proses produser (wait) ketika queue sudah mencapai batas tertentu

29 Bahan lanjut Networking


Download ppt "JAva Threads."

Presentasi serupa


Iklan oleh Google