Chapter 9. Threading in Java
Chapter 9. Threading in Java
Multithreading
Disampaikan pada mata kuliah Object Oriented Programming
Konsep Dasar
Process based
Thread based
Dalam lingkup yang lebih kecil, di dalam process itu sendiri terdapat apa yang
disebut Thread
Konsep Dasar
Fungsi
String getName()
int getPriority()
boolean isAlive
void join()
void join (long millis)
void join (long millis, int
nanos)
Fungsi
void run()
Perhatikan :
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
}
8
10
11
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
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
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