Anda di halaman 1dari 18

Java Progamming

Multithreading
Disampaikan pada mata kuliah Object Oriented Programming

Eddy Muntina Dharma,ST,MT


Jurusan Teknik Informatika STT Telkom
aguseddy@stttelkom.ac.id

Konsep Dasar

Multitasking dapat mengeksekusi lebih dari satu buah program secara


bersamaan.
Dua tipe Multitasking :

Sebagai pengguna komputer, mungkin kita lebih mengenal multitasking yang


bertipe process based, karena pada dasarnya program yang dieksekusi dikenal
oleh Sistem Operasi sebagai process

Process based
Thread based

Contoh : Kita bisa menjalankan Ms.Word & sekaligus menjalankan Winamp

Dalam lingkup yang lebih kecil, di dalam process itu sendiri terdapat apa yang
disebut Thread

Contoh : Program Winamp memainkan musik sekaligus menampilan visualisasi


Equalizer pada Layar. Ini dapat dilakukan selama kedua aksi tersebut dieksekusi
dengan Thread yang berbeda.
Jadi Thread = komponen terkecil yang memungkinkan suatu aktivitas dapat dieksekusi
oleh CPU
2

Konsep Dasar

Java sendiri hanya mendukung Multitasking bertipe


thread based karena untuk Multitasking bertipe
process based secara otomatis sudah didukung oleh
sistem operasi.
CPU bisa mengeksekusi lebih dari satu aktivitas
secara bersamaan ?

Setiap aktivitas dibagi dalam time slice.


CPU secara cepat berpindah-pindah konteks untuk
mengeksekusi setiap aktivitas yang ada ada sesuai dengan
jatah time slice dari setiap thread yang ada.
3

Class Thread & Interface Runnable

Pada saat menjalankan Program, sebuah thread


utama akan dibuat oleh Java untuk mengeksekusi
Program tersebut, yang dimulai dengan memanggil
method main()

Untuk membuat thread baru yang terpisah dari


thread utama, harus membuat class yang merupakan
turunan dari class Thread atau
mengimplementasikan interface Runnable.
4

Class Thread & Interface Runnable

Beberapa method utama pada class Thread yang


sering digunakan :
Method

Fungsi

Static Thread currentThread()

Mendapatkan referensi ke objek Thread


yang sedang dieksekusi.

String getName()

Mendapatkan nama dari thread.

int getPriority()

Mendapatkan prioritas dari thread.

boolean isAlive

Untuk mengetes apakah thread masih


berjalan atau tidak.

void join()
void join (long millis)
void join (long millis, int
nanos)

Menunggu hingga thread ini selesai


dieksekusi.
5

Class Thread & Interface Runnable


Method

Fungsi

void run()

Method yang pertama kali akan dieksekusi


saat thread dibuat.

void setName (String name)

Menset nama dari thread.

static void sleep(long millis) Menangguhkan eksekusi dari thread yang


static void sleep(long millis, sedang berjalan untuk sementara waktu.
int nanos)
void start

Method yang dipanggil untuk membuat


thread dieksekusi

Class Thread & Interface Runnable

Perhatikan :

Method main() = Thread utama


Thread baru yang dibuat = child thread
Thread utama merupakan thread yang terakhir
dieksekusi, karena jika thread utama sudah selesai
dieksekusi, berarti program seharusnya selesai
dieksekusi.

Class Thread & Interface Runnable


Contoh 1 :
1.
2.
3.
4.
5.
6.
7.

8.

public class DemoThread1


{
public static void main(String[] args)
{
Thread tr = Thread.currentThread();
System.out.println("Nama Thread saat ini
: "+tr.getName());
tr.setName("Thread Utama");
System.out.println("Nama Thread setelah diubah : "+tr.getName());
try {
for (int i=0;i<5;i++ ) {
System.out.println(i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Program di interrupt");
}

9.
10.
11.
12.
13.
14.
15.
16.

17.
18.

}
8

Membuat & Mengeksekusi Thread Sendiri

Membuat child thread ada 2 cara :

Membuat class yang mengimplementasikan


interface Runnable dan mengimplemtasikan
method run()
Membuat class yang merupakan turunan dari
class Thread dan meng-override method run().

Mengimplementasikan Interface Runnable


Bandingkan contoh 2 dan contoh 3/contoh4 berikut :

Contoh 2 (tanpa thread):


1.
class HelloRunner {
2.
int i;
3.
public void tampil() {
4.
i = 0;
5.
while (i<5) {
6.
System.out.println("Hello " + i++);
7.
}
8.
}
9.
}
10. public class DemoThread2 {
11.
public static void main(String args[]) {
12.
HelloRunner r1 = new HelloRunner();
13.
HelloRunner r2 = new HelloRunner();
14.
System.out.println("Jalankan Hello Runner 1");
15.
r1.tampil();
16.
System.out.println("Jalankan Hello Runner 2");
17.
r2.tampil();
18.
System.out.println("Selesai");
19.
}
20. }

10

Mengimplementasikan Interface Runnable

Contoh 3 (dengan Thread) :


1. class HelloRunner implements Runnable{
2.
int i;
3.
public void run() {
4.
Thread tr = Thread.currentThread();
5.
i = 0;
6.
while (i<5) {
7.
System.out.println(tr.getName()+" : Hello " + i++);
8.
try {
9.
Thread.sleep(500);
10.
}
11.
catch (InterruptedException e) {
12.
System.out.println("Program di interrupt");
13.
}
14.
}
15.
}
16. }
17. public class DemoThread3 {
18.
public static void main(String args[]) {
19.
HelloRunner r1 = new HelloRunner();
20.
HelloRunner r2 = new HelloRunner();
21.
Thread t1 = new Thread(r1,"Thread ke-1");
22.
Thread t2 = new Thread(r2,"Thread ke-2");
23.
t1.start();
24.
t2.start();
25.
}
26. }

11

Mengimplementasikan Interface Runnable

Contoh 4 (dengan Thread) :


1. class HelloRunner implements Runnable{
2.
public HelloRunner(String threadName) {
3.
Thread newThread = new Thread (this,threadName);
4.
newThread.start();
5.
}
6.
public void run() {
7.
Thread tr = Thread.currentThread();
8.
int i = 0;
9.
while (i<5) {
10.
System.out.println(tr.getName()+" : Hello " + i++);
11.
try {
12.
Thread.sleep(500);
13.
}
14.
catch (InterruptedException e) {
15.
System.out.println("Program di interrupt");
16.
}
17.
}
18.
}
19. }
20. public class DemoThread4 {
21.
public static void main(String args[]) {
22.
HelloRunner r1 = new HelloRunner("Thread ke-1");
23.
HelloRunner r2 = new HelloRunner("Thread ke-2");
24.
}
25. }
12

Membuat Class Turunan dari Class Thread

Contoh 5 :
1. class HelloRunner extends Thread{
2.
int i;
3.
public void run() {
4.
Thread tr = Thread.currentThread();
5.
i = 0;
6.
while (i<5) {
7.
System.out.println(tr.getName()+" : Hello " + i++);
8.
try {
9.
Thread.sleep(500);
10.
}
11.
catch (InterruptedException e) {
12.
System.out.println("Program di interrupt");
13.
}
14.
}
15.
}
16. }
17. public class DemoThread5 {
18.
public static void main(String args[]) {
19.
HelloRunner r1 = new HelloRunner();
20.
HelloRunner r2 = new HelloRunner();
21.
Thread t1 = new Thread(r1,"Thread ke-1");
22.
Thread t2 = new Thread(r2,"Thread ke-2");
23.
t1.start();
24.
t2.start();
25.
}
26. }

13

Membuat Class Turunan dari Class Thread

Contoh 6 :
1. class HelloRunner extends Thread{
2.
public HelloRunner(String threadName) {
3.
setName(threadName);
4.
start();
5.
}
6.
public void run() {
7.
Thread tr = Thread.currentThread();
8.
int i = 0;
9.
while (i<5) {
10.
System.out.println(tr.getName()+" : Hello " + i++);
11.
try {
12.
Thread.sleep(500);
13.
}
14.
catch (InterruptedException e) {
15.
System.out.println("Program di interrupt");
16.
}
17.
}
18.
}
19. }
20. public class DemoThread6 {
21.
public static void main(String args[]) {
22.
HelloRunner r1 = new HelloRunner("Thread ke-1");
23.
HelloRunner r2 = new HelloRunner("Thread ke-2");
24.
}
25. }
14

Prioritas Thread

Sistem operasi akan menjalankan thread yang


memiliki prioritas lebih tinggi.
Jika kita ingin memberikan prioritas tertentu
pada suatu thread, maka dapat dilakukan
dengan perintah :

setPriority (int newPriority)

15

Prioritas Thread

Contoh 7 :
1. class HelloRunner extends Thread {
2.
public HelloRunner(String threadName,int p) {
3.
setPriority(p);
4.
setName(threadName);
5.
start();
6.
}
7.
public void run() {
8.
Thread tr = Thread.currentThread();
9.
int i = 0;
10.
while (i<50) {
11.
System.out.println(tr.getName()+" : Hello " + i++);
12.
try {
13.
Thread.sleep(1);
14.
}
15.
catch (InterruptedException e) {
16.
System.out.println("Program di interrupt");
17.
}
18.
}
19.
}
20. }
21. public class DemoThread7 {
22.
public static void main(String args[]) {
23.
HelloRunner r1 = new HelloRunner("Thread ke-1",1);
24.
HelloRunner r2 = new HelloRunner("Thread ke-2",10);
25.
}
26. }

16

Sinkronisasi

Contoh 8 :
1. class TestSinkronisasi {
2.
public synchronized void callMe(String namaThread) {
3.
int i = 0;
4.
while (i<50) {
5.
System.out.println(namaThread+" : Hello " + i++);
6.
try {
7.
Thread.sleep(1);
8.
}
9.
catch (InterruptedException e) {
10.
System.out.println("Program di interrupt");
11.
}
12.
}
13.
}
14. };
15. class HelloRunner extends Thread {
16.
private TestSinkronisasi ts;
17.
public HelloRunner(TestSinkronisasi t,String threadName) {
18.
ts = t;
19.
setName(threadName);
20.
start();
21.
}
22.
public void run() {
23.
Thread tr = Thread.currentThread();
24.
ts.callMe(tr.getName());
25.
}
26. }

17

Sinkronisasi

Contoh 8 (cont):
27.//Program Utama
28.public class DemoThread8 {
29.
public static void main(String args[]) {
30.
TestSinkronisasi t = new TestSinkronisasi();
31.
HelloRunner r1 = new HelloRunner(t,"Thread ke-1");
32.
HelloRunner r2 = new HelloRunner(t,"Thread ke-2");
33.
}
34.}

18

Anda mungkin juga menyukai