diff --git a/.DS_Store b/.DS_Store index a9c42ff4dba4e7e5df43e20a23bc5a0897baa943..85518c22e9915c664387292c1ff657ead931ec66 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fb7f4a8a465d42b4a0390d464b83b99e8465bba7..b73660a62542f6ee77a17dd92aa341f42f3f2518 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index be6c998276fdd0e11962f9eed4e6543a3d833ecf..bf3cfc92ef1a08084469ea0fcda64655fe3e2528 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/src/main/java/join/Main.java b/src/main/java/join/Main.java index fedf709e108f112645527d78f906be8ce1faabd5..26e497cff9d18f748430439af672e01dfd8b85a4 100644 --- a/src/main/java/join/Main.java +++ b/src/main/java/join/Main.java @@ -9,19 +9,19 @@ public class Main { public static void main(String arg[]) throws Exception { Random r = new Random(); ArrayList threads = new ArrayList(); - //create some threads + //Create some threads for (int i=0; i<10; i++) { MyThread mt = new MyThread(r); threads.add(mt); } System.out.println("All threads have been created."); - //start all the threads + //Start all the threads for (Thread t: threads) { t.start(); } System.out.println("All threads have been started."); System.out.println("Start waiting for all thread to finish..."); - //wait all the thread to finish + //Wait all the thread to finish for (Thread t : threads) { t.join(); } diff --git a/src/main/java/semaphore/Main.java b/src/main/java/semaphore/Main.java index 1315fcb9c50198b78c467582bdfb8ecf244117fb..73e675124fca69c38fe76e375d7857a95941aef5 100644 --- a/src/main/java/semaphore/Main.java +++ b/src/main/java/semaphore/Main.java @@ -8,13 +8,13 @@ public class Main { Random r = new Random(); ArrayList threads = new ArrayList(); Semaphore s = new Semaphore(4); - //create some threads + //Create some threads for (int i=0; i<10; i++) { MyThread mt = new MyThread(r, i, s); threads.add(mt); } - //start all the threads + //Start all the threads for (Thread t: threads) { t.start(); } diff --git a/src/main/java/semaphore/MyThread.java b/src/main/java/semaphore/MyThread.java index b15b661a4991216a734d2bb80e78cdd9fbaaf642..bb3202e920e82486884fbd3fc6352a525d38b7cf 100644 --- a/src/main/java/semaphore/MyThread.java +++ b/src/main/java/semaphore/MyThread.java @@ -18,7 +18,7 @@ public class MyThread extends Thread { System.out.println("Thread " + id + " wants to enter in the critical region"); sem.enter(); System.out.println("Thread " + id + " entered in the critical region!"); - wasteSomeTime(); //it takes some times to compleate the work in the critical region + wasteSomeTime(); //It takes some times to compleate the work in the critical region System.out.println("Thread " + id + " is going to get out from the critical region"); sem.exit(); }//end run diff --git a/src/main/java/semaphore/Semaphore.java b/src/main/java/semaphore/Semaphore.java index f075e31a324242c6bdcc46cf721d4beffac57d39..1fe4ff527bde27e6a9ef96b80f1515b487a298c4 100644 --- a/src/main/java/semaphore/Semaphore.java +++ b/src/main/java/semaphore/Semaphore.java @@ -1,9 +1,9 @@ -package semaphore;//Classe che implementa tramite wait e notify un Semaforo. -//Se non vi ricordate cosa sia un semaforo, google is your best friend. +package semaphore;//Class that implements a semaphore, thanks to wait() and notify() +//If you need to recap what a semaphore is, Google is your best friend. public class Semaphore { - private int maxNumber; //numero massimo di thread - private int threadsIn; //conteggio dei thread nell'area critica + private int maxNumber; //Maximum number of threads + private int threadsIn; //Number of threads in the critical region Semaphore(int max) { maxNumber = max; @@ -12,7 +12,7 @@ public class Semaphore { public synchronized void enter() { System.out.println("" + threadsIn + " in the critical region..."); - //quando abbiamo raggiunto il numero massimo di thread, chi vuole entrare aspetta + //When we reach the maximum number of threads, new threads need to wait while (threadsIn >= maxNumber) { try {this.wait();} catch(InterruptedException ie) {ie.printStackTrace();} @@ -23,7 +23,7 @@ public class Semaphore { public synchronized void exit() { threadsIn--; - //quando un thread esce dall'area critica, sveglia qualcuno in attesa di entrare (se presente) + //Whena thread exits the critical region, it awakens another thread that is waiting (if there is one) this.notify(); } }