Java Virtual Machine menggunakan prioritas preemtive berdasarkan algoritma penjadualan.
Jika terjadi dua thread dengan prioritas sama maka digunakan algoritma First In First Out.
Thread lain dijalankan bila terjadi hal-hal berikut ini:
Thread yang sedang dieksekusi keluar dari status runnable misalnya diblok atau berakhir
Thread dengan prioritas yang lebih tinggi dari thread yang sedang dieksekusi memasuki status runnable. Maka thread dengan prioritas yang lebih rendah ditunda eksekusinya dan digantikan oleh thread dengan prioritas lebih tinggi.
Time slicing tergantung pada implementasinya. Sebuah thread dapat memberi kontrol pada yield() method. Saat thread memberi sinyal pada CPU untuk mengontrol thread yang lain dengan prioritas yang sama maka thread tersebut dijadualkan untuk dieksekusi. Thread yang memberi kontrol pada CPU disebut Cooperative Multitasking.
Saat thread baru dibuat ia mempunyai prioritas yang sama dengan thread yang menciptakannya. Prioritas thread dapat diubah dengan menggunakan setpriority() method.
Gambar 2-40. Round Robin. Sumber: . . .
public class Scheduler extends Thread { public Scheduler() { timeSlice = DEFAULT_TIME_SLICE; queue = new Circularlist(); } public Scheduler(int quantum) { timeSlice = quantum; queue = new Circularlist(); } public addThread(Thread t) { t.setPriority(2); queue.additem(t); } private void schedulerSleep() { try{ Thread.sleep(timeSlice ); } catch (InterruptedException e){}; } public void run(){ Thread current; This.setpriority(6); while (true) { // get the next thread current = (Thread)qeue.getnext(); if ( current != null) && (current.isAlive())) { current.setPriority(4); schedulerSleep(); current.setPriority(2) } } } private CircularList queue; private int timeSlice; private static final int DEFAULT_TIME_SLICE = 1000; } public class TesScheduler{ public static void main()String args[]) { Thread.currentThread().setpriority(Thread.Max_Priority); Schedular CPUSchedular = new Scheduler (); CPUSchedular.start() TestThread t1 = new TestThread("Thread 1"); t1.start() CpuSchedular.addThread(t1); TestThread t2 = new TestThread("Thread 2"); t2.start() CpuSchedular.addThread(t2); TestThread t3 = new TestThread("Thread 1"); t3.start() CpuSchedular.addThread(t3); } } |
Gambar 2-41. Sinkronisasi. Sumber: . . .
public synchronized void enter(Object item) { while (count == BUFFER_SIZE) ; Thread.yeild(); ++count; buffer[in] = item; in = (in+1) % BUFFER_SIZE; } public synchronized void remove (){ Object item; while (count == 0) ; Thread.yeild(); --count; item = buffer[out] out = (out+1) % BUFFER_SIZE; return item } |
Thread akan memanggil method wait() saat:
Thread akan memanggil method notify() saat: Thread yang dipilih diambil dari thread yang ada pada himpunan wait. Dengan cara:
Pindahkan thread yang dipilih dari wait set ke entry set.
Atur status dari thread yang dipilih dari blocked menjadi runnable.
Gambar 2-42. Contoh Wait() dan Notify(). Sumber: . . .
public synchronized void enter(Object item){ while (count == BUFFER_SIZE) { try{ wait(); } catch (InterruptedException e) {} } // add an item to the buffer ++count; buffer[in] = item; in = (in+1) % BUFFER_SIZE; notify(); } public synchronized void remove(Object item){ while (count == 0) { try { wait(); } catch (InterruptedException e) {} } // remove an item to the buffer --count; item = buffer[out]; out = (out+1) % BUFFER_SIZE; notify(); return item; } |