Commit faaf159f authored by Michele Fiori's avatar Michele Fiori

Minor changes on comments

parent 956ddbc7
......@@ -9,19 +9,22 @@ public class Main {
public static void main(String arg[]) throws Exception {
Random r = new Random();
ArrayList<Thread> threads = new ArrayList<Thread>();
//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();
}
......
......@@ -8,13 +8,14 @@ public class Main {
Random r = new Random();
ArrayList<Thread> threads = new ArrayList<Thread>();
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();
}
......
......@@ -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
}
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();
}
}
......
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