THREAD
[Link]
class TestSinkronisasi {
private [Link] random = new [Link]();
public void callMe(String data) {
[Link]("[");
try{
[Link]([Link](200));
}catch(InterruptedException e) {
[Link]();
}
[Link](data);
try{
[Link]([Link](200));
}catch(InterruptedException e) {
[Link]();
}
[Link]("]");
}
}
[Link]
class ThreadBaru extends Thread {
private String data;
private TestSinkronisasi obj;
public ThreadBaru(TestSinkronisasi obj,String data) {
[Link] = obj;
[Link] = data;
start();
}
public void run() {
[Link](data);
}
}
[Link]
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{
[Link]();
[Link]();
[Link]();
}catch(InterruptedException e) {
[Link]("Thread utama diinterupsi " + e);
}
}
}
[Link]
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) {
[Link]( getName() + " : " + hitungMundur );
if (--hitungMundur == 0)
return;
}
}
/**
* @param args
*/
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new ThreadDasar();
}
}
[Link]
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 + ([Link] + Math.E) / (double)i;
[Link]([Link]() + " : " + 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);
}
}
[Link]
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);
}
[Link](this);
}
}
/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
new ThreadDaemon();
}
[Link]
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) {
[Link](getName() + " dibangunkan. "
+ "isInterrupted(): " + isInterrupted());
return;
}
[Link](getName() + " sudah bangun.");
}
}
class ThreadPenggabung extends Thread {
private ThreadPemalas sleeper;
public ThreadPenggabung(String namaThread, ThreadPemalas pemalas) {
super(namaThread);
[Link] = pemalas;
start();
}
public void run() {
try {
[Link]();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
[Link](getName() + " selesai setelah " + [Link]());
}
}
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);
[Link]();
}
[Link]
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 = [Link]();
// Jika ganjil, keluar dan cetak nilainya
if (nilai % 2 != 0) {
[Link](nilai);
[Link](0);
}
}
}
}.start();
while (true)
[Link]();
}
}