Contoh Thread
Contoh Thread
CONTOH 1
Thread thread;
int jumlah = 7;
void proses_satu(){
thread = new Thread(){
public void run(){
try{
for(int w=1; w<=jumlah; w++){
System.out.println("Nomor: "+w);
sleep(1000); //Waktu Pending
}
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
};
thread.start();
}
void proses_dua(){
thread = new Thread(){
public void run(){
try{
for(int w=1; w<=jumlah; w++){
System.out.println("Salam Programmer");
sleep(1000); //Waktu Pending
}
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
};
thread.start();
}
}
Contoh thread 3
CONTOH KE 4
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");
}
System.out.println("Thread " + nama + " :Posisi " + i);
}
}
}