In Coding

Thread Anti Virus


LISTING PROGRAM

class Database{
            public static synchronized void aksesDb(String objnm){
                                    try{Thread.sleep(1000);}catch(Exception e){}
                                    System.out.println("Thread "+objnm+" is running!");
            }
}
public class AntiVirus{
            public static void main(String[] args){
                        Thread update = new Thread(){
                                    public void run(){
                                                while(true){
                                                            try{Thread.currentThread().sleep(5000);}catch(Exception e){}
                                                            Database.aksesDb("update");
                                                            }
                                                }
                                    };
                        Thread scan_virus = new Thread(){
                                    public void run(){
                                                while(true){
                                                try{Thread.currentThread().sleep(3000);}catch(Exception e){}
                                                for(int i=0; i<5; i++){
                                                            Database.aksesDb("scanning for virus.....");
                                                            try{Thread.currentThread().sleep(1000);}catch(Exception e){}
                                                            }
                                                }
                                    }
                        };
                        scan_virus.start();
                        update.start();
            }
}

LOGIKA PROGRAM

class Database{
            public static synchronized void aksesDb(String objnm){
                                    try{Thread.sleep(1000);}catch(Exception e){}
                                    System.out.println("Thread "+objnm+" is running!");
            }
}
            Perintah diatas merupakan class yang diberi nama Database dimana di dalam blok prgram diatas terdapat perintah sinkronisasi dan blok try catch dengan thread sleepnya sebesar 1000 ms atau 1 sekon.
public class AntiVirus{
            public static void main(String[] args){
                        Thread update = new Thread(){
                                    public void run(){
                                                while(true){
                                                            try{Thread.currentThread().sleep(5000);}catch(Exceptione){}
                                                            Database.aksesDb("update");
                                                            }
                                                }
                                    };
            Perintah diatas merupakan fungsi utama dari program. Dalam program diatas terdapat pembuatan objek dengan nama update, kemudian akan menjalankan fungsi run. Dimana thread akan mengupdate setiap 5000 ms atau 5 sekon. 
                        Thread scan_virus = new Thread(){
                                    public void run(){
                                                while(true){
                                                try{Thread.currentThread().sleep(3000);}catch(Exception e){}
                                                for(int i=0; i<5; i++){
                                                            Database.aksesDb("scanning for virus.....");
                        try{Thread.currentThread().sleep(1000);}catch(Exceptione){}
                                                            }
                                                }
                                    }
                        };
            Perintah diatas terdapat pembuatan objek baru lagi yang diberi nama scan_virus, kemudian memanggil fungsi run. Dimana thread akan menscanning virus setiap 3000 ms atau 3 sekon. Kemudian setelah 3 sekon program akan mengupdate lagi setelah 1000 ms atau 1 sekon.          
                        scan_virus.start();
                        update.start();
            }
}
            Perintah diatas digunakan untuk menjalankan program yang telah dibuat, perintah scan_virus.start() digunakan untuk melakukan scanning virus, sedangkan perintah update.start() digunakan untuk mengupdate thread. 
OUTPUT PROGRAM
 

Related Articles

0 komentar:

Posting Komentar