Home > Java > Thread dalam Java Part 3

Thread dalam Java Part 3

February 24, 2011 Leave a comment Go to comments

Pada pada bagian ketiga akan berisi tentang sinkronisasi pada java. Sinkronisasi merupakan suatu proses untuk mengontrol suatu resource terhadap multiple thread sehingga hanya terdapat satu thread yang mengakses resource tersebut pada satu waktu. Dalam sinkronisasi suatu thread sebelum mengakses suatu resource, harus memperoleh lock ( saya belum mengerti sepenuhnya tentang lock ini😀 ). Setelah memperoleh lock, suatu thread dapat mengakses resource tersebut, sedangkan thread lain yang akan menggunakan resource tersebut harus menunggu hingga lock dilepaskan oleh thread yang sedang memegang lock tersebut.

Dalam java terdapat dua jenis synchronized yaitu :

  1. Synchronized method
  2. Synchronized block

Berikut ini contoh Synchronized method

Nama file SynchronizedMethod.java

package threadjavaempat;

/**
*
* @author Taeyeon
*/
public class SynchronizedMethod extends Thread {
static String[] msg = { “kukuh”, “utama”, “dan”,”teman”, “teman”};
public SynchronizedMethod(String id)
{
super(id);
}
void randomWait() {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
System.out.println(“Interrupted!”);
}
}
public synchronized void run() {
SynchronizedOutput.displayList(getName(), msg);
}

}

Nama file SynchronizedOutput.java

package threadjavaempat;

/**
*
* @author Taeyeon
*/
public class SynchronizedOutput {
public static synchronized void displayList(String name, String list[]) {
for (int i = 0; i < list.length; i++) {
SynchronizedMethod t = (SynchronizedMethod) Thread.currentThread();
t.randomWait();
System.out.println(name + list[i]);
}
}

}

Nama file Main.java

package threadjavaempat;

/**
*
* @author Taeyeon
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SynchronizedMethod thread1 = new SynchronizedMethod(“thread1: “);
SynchronizedMethod thread2 = new SynchronizedMethod(“thread2: “);
thread1.start();
thread2.start();
boolean t1IsAlive = true;
boolean t2IsAlive = true;
do {
if (t1IsAlive && !thread1.isAlive()) {
t1IsAlive = false;
System.out.println(“t1 is dead.”);
}
if (t2IsAlive && !thread2.isAlive()) {
t2IsAlive = false;
System.out.println(“t2 is dead.”);
}
} while (t1IsAlive || t2IsAlive);

}

}

Download Code

Dan berikut ini contoh synchronized block

Nama file Counter.java

package threadjavalima;

/**
*
* @author Taeyeon
*/
public class Counter {
long count = 0;

public void add(long value){
synchronized (this) {
this.count += value;
System.out.println(count);
}
}
}

Nama file CounterThread.java

public class CounterThread extends Thread {
protected Counter counter = null;

public CounterThread(Counter counter){
this.counter = counter;
}

public void run() {
for(int i=0; i<10; i++){
counter.add(i);
}
}

}

Nama file Main.java

public class CounterThread extends Thread {
protected Counter counter = null;

public CounterThread(Counter counter){
this.counter = counter;
}

public void run() {
for(int i=0; i<10; i++){
counter.add(i);
}
}

}
Download Code

Referensi : Dari berbagai sumber

Salah koreksi saya

Semoga bermanfaat

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: