0% found this document useful (0 votes)
31 views

Thread: Testsinkronisasi - Java

The document discusses several examples of using threads in Java programs. It includes classes that create and manage threads, demonstrate thread priorities and daemon threads, and show how to synchronize access to shared resources using joins. Examples show threads performing tasks concurrently, sleeping for periods of time, and checking/modifying shared variables.

Uploaded by

Nur Kholis Majid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Thread: Testsinkronisasi - Java

The document discusses several examples of using threads in Java programs. It includes classes that create and manage threads, demonstrate thread priorities and daemon threads, and show how to synchronize access to shared resources using joins. Examples show threads performing tasks concurrently, sleeping for periods of time, and checking/modifying shared variables.

Uploaded by

Nur Kholis Majid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

THREAD

TestSinkronisasi.java
class TestSinkronisasi {
private java.util.Random random = new java.util.Random();
public void callMe(String data) {
System.out.print("[");
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(data);
try{
Thread.sleep(random.nextInt(200));
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("]");
}
}

ThreadBaru.java
class ThreadBaru extends Thread {
private String data;
private TestSinkronisasi obj;
public ThreadBaru(TestSinkronisasi obj,String data) {
this.obj = obj;
this.data = data;
start();
}
public void run() {
obj.callMe(data);
}
}

DemoThread.java
class DemoThread {
public static void main(String[] args) {
TestSinkronisasi obj = new TestSinkronisasi();
ThreadBaru thread1 = new ThreadBaru(obj,"Superman");
ThreadBaru thread2 = new ThreadBaru(obj,"Batman");
ThreadBaru thread3 = new ThreadBaru(obj,"Spiderman");
//tunggu hingga semua child thread selesai dieksekusi
try{
thread1.join();
thread2.join();
thread3.join();
}catch(InterruptedException e) {
System.out.println("Thread utama diinterupsi " + e);
}
}
}

ThreadDasar.java
public class ThreadDasar extends Thread {
private int hitungMundur = 5;
private static int jumlahThread = 0;

public ThreadDasar() {
super("Thread ke-" + ++jumlahThread);
start();
}

public void run() {


while (true) {
System.out.println( getName() + " : " + hitungMundur );
if (--hitungMundur == 0)
return;
}
}

/**
* @param args
*/
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new ThreadDasar();
}
}

PrioritasThread.java
public class PrioritasThread extends Thread {
private int hitungMundur = 5;
private volatile double d = 0; // No optimization

public PrioritasThread(int prioritas) {


setPriority(prioritas);
start();
}

public void run() {


while (true) {
for(int i = 1; i < 100000; i++)
d = d + (Math.PI + Math.E) / (double)i;
System.out.println(this.toString() + " : " + hitungMundur);
if (--hitungMundur == 0)
return;
}
}

/**
* @param args
*/
public static void main(String[] args) {
new PrioritasThread(Thread.MAX_PRIORITY);
for(int i = 0; i < 5; i++)
new PrioritasThread(Thread.MIN_PRIORITY);
}
}

ThreadDaemon.java
public class ThreadDaemon extends Thread {
public ThreadDaemon() {
setDaemon(true); // Harus dipanggil sebelum start
start();
}

public void run() {


while (true) {
try {
sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(this);
}
}

/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
new ThreadDaemon();
}

JoinDemo.java
class ThreadPemalas extends Thread {
private int waktu;

public ThreadPemalas(String namaThread, int waktuTidur) {


super(namaThread);
waktu = waktuTidur;
start();
}
public void run() {
try {
sleep(waktu);
} catch (InterruptedException e) {
System.out.println(getName() + " dibangunkan. "
+ "isInterrupted(): " + isInterrupted());
return;
}
System.out.println(getName() + " sudah bangun.");
}
}

class ThreadPenggabung extends Thread {


private ThreadPemalas sleeper;

public ThreadPenggabung(String namaThread, ThreadPemalas pemalas) {


super(namaThread);
this.sleeper = pemalas;
start();
}

public void run() {


try {
sleeper.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " selesai setelah " + sleeper.getName());
}
}

public class JoinDemo {


/**
* @param args
*/
public static void main(String[] args) {
ThreadPemalas brr = new ThreadPemalas("brr", 2000);
ThreadPemalas grr = new ThreadPemalas("grr", 2000);

ThreadPenggabung saya = new ThreadPenggabung("saya",brr);


ThreadPenggabung anda = new ThreadPenggabung("anda",grr);

grr.interrupt();
}

SelaluGenap.java
public class SelaluGenap {
private int i;

public void berikut() {


i++;
i++;
}

public int ambilNilai() {


return i;
}

public static void main(String[] args) {


final SelaluGenap genap = new SelaluGenap();

new Thread("Hansip") {
public void run() {
while (true) {
int nilai = genap.ambilNilai();
// Jika ganjil, keluar dan cetak nilainya
if (nilai % 2 != 0) {
System.out.println(nilai);
System.exit(0);
}
}
}
}.start();

while (true)
genap.berikut();
}
}

You might also like