Commit 0f6799fa authored by Michele Fiori's avatar Michele Fiori

Switched comments to english

parent 956ddbc7
No preview for this file type
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" />
<bytecodeTargetLevel target="15" />
</component>
</project>
\ No newline at end of file
......@@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
......@@ -9,19 +9,19 @@ public class Main {
public static void main(String arg[]) throws Exception {
Random r = new Random();
ArrayList<Thread> threads = new ArrayList<Thread>();
//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();
}
......
......@@ -8,13 +8,13 @@ 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();
}
......
......@@ -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
......
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();
}
}
......
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