diff --git a/build/classes/java/main/join/Main.class b/build/classes/java/main/join/Main.class index 2946019686eaf577e2b43de45880ddb9d13335a0..2ae0d963ee70256e5cb64139da6cf3db2c71dfd7 100644 Binary files a/build/classes/java/main/join/Main.class and b/build/classes/java/main/join/Main.class differ diff --git a/build/classes/java/main/join/MyThread.class b/build/classes/java/main/join/MyThread.class index dff7bcaab0e7f2ef5971449a0b71b8c0525d51cf..168cc296e629ae37d7f503beb85e8b1c59d08d52 100644 Binary files a/build/classes/java/main/join/MyThread.class and b/build/classes/java/main/join/MyThread.class differ diff --git a/build/classes/java/main/producerConsumer/Consumer.class b/build/classes/java/main/producerConsumer/Consumer.class index 8d4380cff125d333e16aad1548d4d12f19d7ddc7..7348d91d359ab31923d40a7ddb13a09bf007aa82 100644 Binary files a/build/classes/java/main/producerConsumer/Consumer.class and b/build/classes/java/main/producerConsumer/Consumer.class differ diff --git a/build/classes/java/main/producerConsumer/Main.class b/build/classes/java/main/producerConsumer/Main.class index 7c87885d1ee0c579c875527e5f961084a20dee0c..e65e0e682846e063e95ce5ea2117952ce1eb2c9e 100644 Binary files a/build/classes/java/main/producerConsumer/Main.class and b/build/classes/java/main/producerConsumer/Main.class differ diff --git a/build/classes/java/main/producerConsumer/Producer.class b/build/classes/java/main/producerConsumer/Producer.class index 6d346b3304e8c2e1f5348edd569ab3798813d841..cc232c50b4c818e510686290b74d0139e0da68cd 100644 Binary files a/build/classes/java/main/producerConsumer/Producer.class and b/build/classes/java/main/producerConsumer/Producer.class differ diff --git a/build/classes/java/main/producerConsumer/Queue.class b/build/classes/java/main/producerConsumer/Queue.class index b155823de0a0ced6fb21c8929b331cf67bde4911..5977dbcb6949408042cea2f9b3c544f07f823081 100644 Binary files a/build/classes/java/main/producerConsumer/Queue.class and b/build/classes/java/main/producerConsumer/Queue.class differ diff --git a/build/classes/java/main/semaphore/Main.class b/build/classes/java/main/semaphore/Main.class index 6e7db4067f6e83ba23a282637a564fab022f557d..0f9e9656816af4523b12333b30582afe6cf38686 100644 Binary files a/build/classes/java/main/semaphore/Main.class and b/build/classes/java/main/semaphore/Main.class differ diff --git a/build/classes/java/main/semaphore/MyThread.class b/build/classes/java/main/semaphore/MyThread.class index 957a150b4844b534ed3f15cc240f35e41179407f..ecf3f4842a41f05edb10860a161b4c01a7252a12 100644 Binary files a/build/classes/java/main/semaphore/MyThread.class and b/build/classes/java/main/semaphore/MyThread.class differ diff --git a/build/classes/java/main/semaphore/Semaphore.class b/build/classes/java/main/semaphore/Semaphore.class index 46ce2ecb0fd05ade46b044b47cf90acbfbaf1f36..ac7fd47119a94b494d163a7f5076377e46f69ee2 100644 Binary files a/build/classes/java/main/semaphore/Semaphore.class and b/build/classes/java/main/semaphore/Semaphore.class differ diff --git a/src/main/java/join/Main.java b/src/main/java/join/Main.java index fedf709e108f112645527d78f906be8ce1faabd5..aefad1c6386d50335332d76f2b2510816b553552 100644 --- a/src/main/java/join/Main.java +++ b/src/main/java/join/Main.java @@ -9,19 +9,22 @@ public class Main { public static void main(String arg[]) throws Exception { Random r = new Random(); ArrayList threads = new ArrayList(); + //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 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 threads 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..3e82d2b4bd9a88f78ec4b876113b95216f6dcaf8 100644 --- a/src/main/java/semaphore/Main.java +++ b/src/main/java/semaphore/Main.java @@ -8,13 +8,14 @@ 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..0209e0a475385ac07b39d401a76db2933e09d55f 100644 --- a/src/main/java/semaphore/MyThread.java +++ b/src/main/java/semaphore/MyThread.java @@ -10,23 +10,27 @@ public class MyThread extends Thread { MyThread(Random r, int i, Semaphore s) { rnd = r; id = i; - sem=s; + sem = s; } public void run() { - wasteSomeTime(); //Simulate the thread is doing something else + //Simulate the thread is doing something else + wasteSomeTime(); 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 + + //It takes some times to complete the work in the critical region + wasteSomeTime(); System.out.println("Thread " + id + " is going to get out from the critical region"); sem.exit(); - }//end run + } + //End run private void wasteSomeTime() { int seconds = rnd.nextInt(10) + 1; try {Thread.sleep(seconds*1000);} catch(Exception ex) {ex.printStackTrace();} } -} //end class +} diff --git a/src/main/java/semaphore/Semaphore.java b/src/main/java/semaphore/Semaphore.java index f075e31a324242c6bdcc46cf721d4beffac57d39..a932afc2f861afc1c4374b7f356c9ebe0f657624 100644 --- a/src/main/java/semaphore/Semaphore.java +++ b/src/main/java/semaphore/Semaphore.java @@ -1,9 +1,13 @@ -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; +/* +This class implements a samafor through the use of wait and notify + */ public class Semaphore { - private int maxNumber; //numero massimo di thread - private int threadsIn; //conteggio dei thread nell'area critica + //Maximum number of threads + private int maxNumber; + //Number of threads in the critical region + private int threadsIn; Semaphore(int max) { maxNumber = max; @@ -12,7 +16,8 @@ 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 the maximum number of threads is reached, new threads have to wait while (threadsIn >= maxNumber) { try {this.wait();} catch(InterruptedException ie) {ie.printStackTrace();} @@ -23,7 +28,7 @@ public class Semaphore { public synchronized void exit() { threadsIn--; - //quando un thread esce dall'area critica, sveglia qualcuno in attesa di entrare (se presente) + // When a thread exits the critical region, it wakes another threads (if there is one) that's waiting to enter the critical region this.notify(); } }