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

10 Multithreading Java

The document discusses multi-threading in Java. It explains that multi-threading allows instructions or processes to run concurrently within a program. It provides examples of using the Thread class by extending it and implementing the Runnable interface to create and run multiple threads. It also discusses thread lifecycles and the use of synchronization to ensure thread-safe execution of critical sections of code.

Uploaded by

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

10 Multithreading Java

The document discusses multi-threading in Java. It explains that multi-threading allows instructions or processes to run concurrently within a program. It provides examples of using the Thread class by extending it and implementing the Runnable interface to create and run multiple threads. It also discusses thread lifecycles and the use of synchronization to ensure thread-safe execution of critical sections of code.

Uploaded by

witeh35535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Multi threading

 Multithreading adalah kemampuan yg


memungkinkan kumpulan instruksi atau proses
dpt dijalankan secara bersamaan dlm sebuah
program.
 Thread adalah satu kumpulan instruksi yg akan
dieksekusi secara independen
 Kegunaan thread pada permainan( game) dan
web browser
Sebuah program

Thread 1

Thread 2

Thread 3

Daur hidup sebuah thread


waktu
 Thread dengan menggunakan kelas thread
 Thread melalui Runnable
 Cara ini dgn memperluas kelas thread dan
menuliskan kembali kode pada metode run().
 Bentuk :
class namakelas extends Thread{
public void run(){

}
}
public class Ujithread{
public static void main(String[] args){
Mobil m1 = new Mobil("M-1");
Mobil m2 = new Mobil("M-2");

m1.start();
m2.start();
}
}
class Mobil extends Thread{
//konstruktor
public Mobil(String id){
super(id);
}
//mendefinisikan sendiri run()
public void run() {
String nama = getName();
for (int i=0; i<5; i++) {
try{
sleep(1000); //tunggu 1 detik
}
catch(InterruptedException ie){
System.out.println("terinterupsi");
}
System.out.println("Thread " + nama + " :Posisi " + i);
}
}
}
Mobil m1 = new Mobil(“M-1”) class Mobil extends Thread{
m1.start();

public void run() {

}
 Runnable sesungguhnya adalah sebuah
interface.
 Dengan mengimplementasikan interface ini,
sebuah kelas yg menangani thread dapat
diciptakan.
public class Ujithread2 {
public static void main(String[] args){
Thread m1 = new Thread(new Mobil("M-1"));
Thread m2 = new Thread(new Mobil("M-2"));

m1.start();
m2.start();
}
}
class Mobil implements Runnable{
String nama;

//konstruktor
public Mobil(String id) {
nama=id;
}
public void run() {
for(int i=0; i<5; i++) {
try{
Thread.currentThread().sleep(1000);
}
catch(InterruptedException ie){
System.out.println("terinterupsi");cr
}
System.out.println("Thread " + nama + " :Posisi " + i);
}
}
}
 Sebuah Thread terus dieksekusi sampai salah
satu kondisi berikut terjadi :
1. Eksekusi terhadap run() berakhir
2. Terinterupsi oleh eksepsi yg tidak tertangkap
3. Metode stop() dipanggil
public class Ujithread{
public static void main(String[] args){
Mobil m1 = new Mobil("M-1");
Mobil m2 = new Mobil("M-2");

m1.start();
m2.start();
}
}
class Mobil extends Thread{
//konstruktor
public Mobil(String id){
super(id);
}
// beri komentar ketika thread berakhir
boolean m1Berakhir = false;
boolean m2Berakhir = false;

do{
//cek keberadaan thread m1
if (!m1Berakhir && !m1.isAlive()) {
m1Berakhir = true;
System.out.println("Thread m1 berakhir");
}
// cek keberadaan thread m2
if (!m2Berakhir && !m2.isAlive()) {
m2Berakhir = true;
System.out.println("Thread m2 berakhir");
}
}while (!m1Berakhir || !m2Berakhir);
//mendefinisikan sendiri run()
public void run() {
String nama = getName();
for (int i=0; i<5; i++) {
try{
sleep(1000); //tunggu 1 detik
}
catch(InterruptedException ie){
System.out.println("terinterupsi");
}
System.out.println("Thread " + nama + " :Posisi " + i);
}
}
}
 Sinkronisasi merupakan suatu upaya agar kode
tertentu dijalankan secara berurutan dengan
jaminan kode tersebut tdk akan dijalankan oleh
yg lain dlm waktu bersamaan.
public class Ujithread4{
public static void main(String[] args){
Mobil m1 = new Mobil("M-1");
Mobil m2 = new Mobil("M-2");

m1.start();
m2.start();
}
}
class Mobil extends Thread{
//konstruktor
public Mobil(String id){
super(id);
}

//mendefinisikan sendiri run()


public void run() {
String nama = getName();
SinkronisasiKeluaran.info(nama);
}
}
class SinkronisasiKeluaran{
public static synchronized void info(String nama){
for (int i=0; i<5; i++) {
try{
Thread.sleep(1000); //tunggu 1 detik
}
catch(InterruptedException ie){
System.out.println("terinterupsi");
}
System.out.println("Thread " + nama + " :Posisi " + i);
}
}
}

You might also like