Commit faaf159f authored by Michele Fiori's avatar Michele Fiori

Minor changes on comments

parent 956ddbc7
...@@ -9,19 +9,22 @@ public class Main { ...@@ -9,19 +9,22 @@ public class Main {
public static void main(String arg[]) throws Exception { public static void main(String arg[]) throws Exception {
Random r = new Random(); Random r = new Random();
ArrayList<Thread> threads = new ArrayList<Thread>(); ArrayList<Thread> threads = new ArrayList<Thread>();
//create some threads //create some threads
for (int i=0; i<10; i++) { for (int i=0; i<10; i++) {
MyThread mt = new MyThread(r); MyThread mt = new MyThread(r);
threads.add(mt); threads.add(mt);
} }
System.out.println("All threads have been created."); System.out.println("All threads have been created.");
//start all the threads //start all the threads
for (Thread t: threads) { for (Thread t: threads) {
t.start(); t.start();
} }
System.out.println("All threads have been started."); System.out.println("All threads have been started.");
System.out.println("Start waiting for all thread to finish..."); 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) { for (Thread t : threads) {
t.join(); t.join();
} }
......
...@@ -8,13 +8,14 @@ public class Main { ...@@ -8,13 +8,14 @@ public class Main {
Random r = new Random(); Random r = new Random();
ArrayList<Thread> threads = new ArrayList<Thread>(); ArrayList<Thread> threads = new ArrayList<Thread>();
Semaphore s = new Semaphore(4); Semaphore s = new Semaphore(4);
//create some threads
//Create some threads
for (int i=0; i<10; i++) { for (int i=0; i<10; i++) {
MyThread mt = new MyThread(r, i, s); MyThread mt = new MyThread(r, i, s);
threads.add(mt); threads.add(mt);
} }
//start all the threads //Start all the threads
for (Thread t: threads) { for (Thread t: threads) {
t.start(); t.start();
} }
......
...@@ -10,23 +10,27 @@ public class MyThread extends Thread { ...@@ -10,23 +10,27 @@ public class MyThread extends Thread {
MyThread(Random r, int i, Semaphore s) { MyThread(Random r, int i, Semaphore s) {
rnd = r; rnd = r;
id = i; id = i;
sem=s; sem = s;
} }
public void run() { 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"); System.out.println("Thread " + id + " wants to enter in the critical region");
sem.enter(); sem.enter();
System.out.println("Thread " + id + " entered in the critical region!"); 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"); System.out.println("Thread " + id + " is going to get out from the critical region");
sem.exit(); sem.exit();
}//end run }
//End run
private void wasteSomeTime() { private void wasteSomeTime() {
int seconds = rnd.nextInt(10) + 1; int seconds = rnd.nextInt(10) + 1;
try {Thread.sleep(seconds*1000);} try {Thread.sleep(seconds*1000);}
catch(Exception ex) {ex.printStackTrace();} catch(Exception ex) {ex.printStackTrace();}
} }
} //end class }
package semaphore;//Classe che implementa tramite wait e notify un Semaforo. package semaphore;
//Se non vi ricordate cosa sia un semaforo, google is your best friend. /*
This class implements a samafor through the use of wait and notify
*/
public class Semaphore { public class Semaphore {
private int maxNumber; //numero massimo di thread //Maximum number of threads
private int threadsIn; //conteggio dei thread nell'area critica private int maxNumber;
//Number of threads in the critical region
private int threadsIn;
Semaphore(int max) { Semaphore(int max) {
maxNumber = max; maxNumber = max;
...@@ -12,7 +16,8 @@ public class Semaphore { ...@@ -12,7 +16,8 @@ public class Semaphore {
public synchronized void enter() { public synchronized void enter() {
System.out.println("" + threadsIn + " in the critical region..."); 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) { while (threadsIn >= maxNumber) {
try {this.wait();} try {this.wait();}
catch(InterruptedException ie) {ie.printStackTrace();} catch(InterruptedException ie) {ie.printStackTrace();}
...@@ -23,7 +28,7 @@ public class Semaphore { ...@@ -23,7 +28,7 @@ public class Semaphore {
public synchronized void exit() { public synchronized void exit() {
threadsIn--; 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(); this.notify();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment