1 / 25

Pemrograman Berorientasi Objek

Pemrograman Berorientasi Objek. Multi threading. MULTITHREADING. Multithreading adalah kemampuan yg memungkinkan kumpulan instruksi atau proses dpt dijalankan secara bersamaan dlm sebuah program. Thread adalah satu kumpulan instruksi yg akan dieksekusi secara independen

chars
Télécharger la présentation

Pemrograman Berorientasi Objek

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pemrograman Berorientasi Objek Multi threading

  2. MULTITHREADING • Multithreading adalah kemampuan yg memungkinkan kumpulan instruksi atau proses dpt dijalankan secara bersamaan dlm sebuah program. • Thread adalah satu kumpulan instruksi yg akan dieksekusi secara independen • Kegunaan thread pada permainan( game) dan web browser

  3. Multithreading Sebuah program Thread 1 Thread 2 Thread 3 Daur hidup sebuah thread waktu

  4. Penanganan Thread • Thread dengan menggunakan kelas thread • Thread melalui Runnable

  5. Menggunakankelas Thread • Cara ini dgn memperluas kelas thread dan menuliskan kembali kode pada metode run(). • Bentuk : class namakelas extends Thread{ public void run(){ … } }

  6. Contoh : Ujithread.java public class Ujithread{ public static void main(String[] args){ Mobil m1 = new Mobil("M-1"); Mobil m2 = new Mobil("M-2"); m1.start(); m2.start(); } } class Mobil extends Thread{ //konstruktor public Mobil(String id){ super(id); }

  7. Contoh : Ujithread.java //mendefinisikan sendiri run() public void run() { String nama = getName(); for (int i=0; i<5; i++) { try{ sleep(1000); //tunggu 1 detik } catch(InterruptedException ie){ System.out.println("terinterupsi"); } System.out.println("Thread " + nama + " :Posisi " + i); } } }

  8. Keterangan : Mobil m1 = new Mobil(“M-1”) m1.start(); class Mobil extends Thread{ … public void run() { }

  9. Hasil :

  10. Thread melaluiRunnable • Runnable sesungguhnya adalah sebuah interface. • Dengan mengimplementasikan interface ini, sebuah kelas yg menangani thread dapat diciptakan.

  11. Contoh : Ujithread2.java public class Ujithread2 { public static void main(String[] args){ Thread m1 = new Thread(new Mobil("M-1")); Thread m2 = new Thread(new Mobil("M-2")); m1.start(); m2.start(); } }

  12. Contoh : Ujithread2.java class Mobil implements Runnable{ String nama; //konstruktor public Mobil(String id) { nama=id; }

  13. Contoh : Ujithread2.java public void run() { for(int i=0; i<5; i++) { try{ Thread.currentThread().sleep(1000); } catch(InterruptedException ie){ System.out.println("terinterupsi");cr } System.out.println("Thread " + nama + " :Posisi " + i); } } }

  14. Hasil :

  15. Daur hidup Thread • Sebuah Thread terus dieksekusi sampai salah satu kondisi berikut terjadi : • Eksekusi terhadap run() berakhir • Terinterupsi oleh eksepsi yg tidak tertangkap • Metode stop() dipanggil

  16. Contoh : Ujithread3.java public class Ujithread{ public static void main(String[] args){ Mobil m1 = new Mobil("M-1"); Mobil m2 = new Mobil("M-2"); m1.start(); m2.start(); } } class Mobil extends Thread{ //konstruktor public Mobil(String id){ super(id); }

  17. Contoh : Ujithread3.java // beri komentar ketika thread berakhir boolean m1Berakhir = false; boolean m2Berakhir = false; do{ //cek keberadaan thread m1 if (!m1Berakhir && !m1.isAlive()) { m1Berakhir = true; System.out.println("Thread m1 berakhir"); }

  18. Contoh : Ujithread3.java // cek keberadaan thread m2 if (!m2Berakhir && !m2.isAlive()) { m2Berakhir = true; System.out.println("Thread m2 berakhir"); } }while (!m1Berakhir || !m2Berakhir);

  19. Contoh : Ujithread3.java //mendefinisikan sendiri run() public void run() { String nama = getName(); for (int i=0; i<5; i++) { try{ sleep(1000); //tunggu 1 detik } catch(InterruptedException ie){ System.out.println("terinterupsi"); } System.out.println("Thread " + nama + " :Posisi " + i); } } }

  20. Hasil :

  21. Sinkronisasi • Sinkronisasi merupakan suatu upaya agar kode tertentu dijalankan secara berurutan dengan jaminan kode tersebut tdk akan dijalankan oleh yg lain dlm waktu bersamaan.

  22. Contoh : Ujithread4.java public class Ujithread4{ public static void main(String[] args){ Mobil m1 = new Mobil("M-1"); Mobil m2 = new Mobil("M-2"); m1.start(); m2.start(); } }

  23. Contoh : Ujithread4.java class Mobil extends Thread{ //konstruktor public Mobil(String id){ super(id); } //mendefinisikan sendiri run() public void run() { String nama = getName(); SinkronisasiKeluaran.info(nama); } }

  24. Contoh : Ujithread4.java class SinkronisasiKeluaran{ public static synchronized void info(String nama){ for (int i=0; i<5; i++) { try{ Thread.sleep(1000); //tunggu 1 detik } catch(InterruptedException ie){ System.out.println("terinterupsi"); } System.out.println("Thread " + nama + " :Posisi " + i); } } }

  25. Hasil :

More Related