Modul C: Sinkronisasi Statis dengan Contoh Penggunaan Kelas Anonim
Sinkronisasi Statis dengan Contoh Penggunaan Kelas Anonim
disusun untuk memenuhi
tugas mata kuliah Sistem Operasi
Oleh:
ALFI ZAMRIZA
2208107010080
SEMESTER 3
JURUSAN INFORMATIKA
FAKULTAS METEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS SYIAH KUALA
DARUSSALAM, BANDA ACEH
2023
Soal:
Static Synchronization
- TestSynchronization4.java
Example of static synchronization by Using the anonymous class
- TestSynchronization5.java
Penyelesaian:
1. TestSynchronization4.java
- Angka
Code:
class Table {
synchronized static void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
}
}
}
}
class MyThread1 extends Thread {
public void run() {
Table.printTable(1);
}
}
class MyThread2 extends Thread {
public void run() {
Table.printTable(10);
}
}
class MyThread3 extends Thread {
public void run() {
Table.printTable(100);
}
}
class MyThread4 extends Thread {
public void run() {
Table.printTable(1000);
}
}
public class TestSynchronization4 {
public static void main(String t[]) {
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
MyThread3 t3 = new MyThread3();
MyThread4 t4 = new MyThread4();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Output:
class Table {
synchronized static void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
}
}
}
}
class MyThread1 extends Thread {
public void run() {
Table.printTable(1);
}
}
class MyThread2 extends Thread {
public void run() {
Table.printTable(10);
}
}
class MyThread3 extends Thread {
public void run() {
Table.printTable(100);
}
}
class MyThread4 extends Thread {
public void run() {
Table.printTable(1000);
}
}
public class TestSynchronization4{
public static void main(String t[]) {
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
MyThread3 t3 = new MyThread3();
MyThread4 t4 = new MyThread4();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Output:
1
2
3
4
5
6
7
8
9
10
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
10
20
30
40
50
60
70
80
90
100
100
200
300
400
500
600
700
800
900
1000
- Huruf
Code:
class Table {
synchronized static void printTable(int n) {
for (int i = 0; i < 26; i++) {
char letter = (char) ('A' + i);
System.out.println(letter);
try {
Thread.sleep(400);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread {
public void run() {
Table.printTable(1);
}
}
class MyThread2 extends Thread {
public void run() {
Table.printTable(10);
}
}
class MyThread3 extends Thread {
public void run() {
Table.printTable(100);
}
}
class MyThread4 extends Thread {
public void run() {
Table.printTable(1000);
}
}
public class TestSynchronization4 {
public static void main(String t[]) {
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
MyThread3 t3 = new MyThread3();
MyThread4 t4 = new MyThread4();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Output:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Kode tersebut merupakan contoh penggunaan sinkronisasi dalam bahasa pemrograman Java. Dalam kode tersebut, kita memiliki kelas `Table` yang memiliki metode `printTable` yang mencetak huruf alfabet dari 'A' hingga 'Z'. Metode ini diatur sebagai `synchronized static`, yang berarti hanya satu thread yang dapat mengaksesnya pada satu waktu, memastikan penggunaan yang aman. Selanjutnya, terdapat empat thread yang didefinisikan sebagai `MyThread1`, `MyThread2`, `MyThread3`, dan `MyThread4`. Masing-masing thread menjalankan metode `printTable` dengan parameter yang berbeda, sehingga mereka mencetak huruf-huruf dalam urutan yang berbeda berdasarkan parameter yang diberikan. Kode ini mengilustrasikan penggunaan sinkronisasi untuk mengatur akses bersama ke metode yang dibagikan, sehingga thread-thread tersebut mencetak huruf alfabet dalam urutan yang koordinatif dengan penundaan antar huruf.
2. TestSynchronization5.java
- Angka
Code:
class Table {
synchronized static void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
}
}
}
}
public class TestSynchronization5 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
Table.printTable(1);
}
};
Thread t2 = new Thread() {
public void run() {
Table.printTable(10);
}
};
Thread t3 = new Thread() {
public void run() {
Table.printTable(100);
}
};
Thread t4 = new Thread() {
public void run() {
Table.printTable(1000);
}
};
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Output:
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
100
200
300
400
500
600
700
800
900
1000
1
2
3
4
5
6
7
8
9
10
10
20
30
40
50
60
70
80
90
100
- Huruf
Code:
class Table {
private static char currentChar = 'A';
synchronized static void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(currentChar);
currentChar = (char) (currentChar + 1);
if (currentChar > 'Z') {
currentChar = 'A';
}
try {
Thread.sleep(400);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class TestSynchronization5 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
Table.printTable(1);
}
};
Thread t2 = new Thread() {
public void run() {
Table.printTable(10);
}
};
Thread t3 = new Thread() {
public void run() {
Table.printTable(100);
}
};
Thread t4 = new Thread() {
public void run() {
Table.printTable(1000);
}
};
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Output:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
B
C
D
E
F
G
H
I
J
K
L
M
N
Kode tersebut adalah contoh penggunaan sinkronisasi dalam Java untuk mencetak huruf alfabet dalam urutan A hingga Z.
Kelas `Table` memiliki metode `printTable`, yang diatur sebagai `synchronized static`, sehingga hanya satu thread yang dapat mengaksesnya pada satu waktu. Metode ini mencetak huruf-huruf dalam urutan dan menggunakan variabel `currentChar` untuk melacak huruf saat ini yang harus dicetak. Setiap huruf dicetak dengan penundaan 400 ms di antara setiap huruf.
Dalam `main` method, empat thread (`t1`, `t2`, `t3`, `t4`) diciptakan, masing-masing menjalankan metode `printTable` dengan parameter yang berbeda. Hal ini menghasilkan output huruf alfabet yang dicetak dalam urutan yang berbeda berdasarkan parameter yang diberikan kepada setiap thread, dan thread-thread tersebut mencetak huruf bergantian dengan penundaan antar huruf.

Comments
Post a Comment