Hi Forum,
I'm a new member, My name is Enrico an I'm 23 , I'm Italy and i am a computer engineering's student at University of Pisa .
I have to carry out a project in Java.
First target of it is the implementation of semaphores. In particular blocked threads have to wake up in First in First Out modality.
I have written some code but I would have some feedback from you , suggests for example.
This is my code:
/* File: FairSemaphore.java */ import java.util.*; public class FairSemaphore { private int count_semaphore; /* Contatore del semaforo */ LinkedList <String> queue; /* Lista thread bloccati */ private Boolean execution ; /* Booleano per la mutua esclusione */ public int quanti() { return queue.size(); } public void sem_signal() throws InterruptedException { /* Mi assicuro che nessuno sta eseguendo questa funzione o la relativa wait */ System.out.println(Thread.currentThread().getName() + "Incipit la signal"); synchronized( execution ) { while( execution == true ) { System.out.println(Thread.currentThread().getName() + "Non puņ fa la signal"); execution.wait(); } execution = true; /* Acquisisco lock della funzione */ } /***** Signal Vera e propria, come se fossi a livello Kernel *****/ System.out.println(Thread.currentThread().getName() + "Dentro la signal"); if( ! queue.isEmpty()) /* Qualcuno bloccato ? */ { String thread_to_wake_up = queue.removeFirst(); synchronized( thread_to_wake_up) { thread_to_wake_up.notify(); /* Sveglio l'unico bloccato in questo oggetto */ } } else count_semaphore ++; synchronized(execution) { execution.notify(); /* Rilascio lock, svegliando altri Thread (se presenti ) */ execution = false; System.out.println(Thread.currentThread().getName() + "Finisce la signal"); } } public void sem_wait() throws InterruptedException { System.out.println(Thread.currentThread().getName() + "Incipit la wait"); /* Mi assicuro che nessuno sta eseguendo questa funzione o la relativa segnal */ synchronized(execution) { while( execution == true) { System.out.println(Thread.currentThread().getName() + "Non puņ fa' la wait"); execution.wait(); } execution = true; /* Lock */ } /***** Wait Vera e propria, come se fossi a livello Kernel *****/ if( count_semaphore == 0) { System.out.println(Thread.currentThread().getName() + "si blocca"); String name_thread = new String(Thread.currentThread().getName()); queue.addLast( name_thread); synchronized(name_thread) { synchronized(execution) { execution.notify(); execution = false; } /* Avendo rilasciato lock, qui puņ inserirsi una nuovo thread */ /* Tuttavia ormai le strutture dati sono aggiornate */ /* Nel caso mi superi una signal, la stessa si blocca quando estrae l'elemento */ name_thread.wait(); /* In wait sul proprio oggetto */ } System.out.println(Thread.currentThread().getName() + "si Sblocca"); } else { count_semaphore --; synchronized(execution) { execution.notify(); execution = false; /* Libero lock */ } } } public FairSemaphore( int init_value_semaphore) { count_semaphore = init_value_semaphore; queue = new LinkedList<String>(); execution = new Boolean(false); } }
Thanks for answers
Bye