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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" /> <bytecodeTargetLevel target="15" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" /> <file type="web" url="file://$PROJECT_DIR$" />
</component> </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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -9,19 +9,19 @@ public class Main { ...@@ -9,19 +9,19 @@ 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 thread to finish
for (Thread t : threads) { for (Thread t : threads) {
t.join(); t.join();
} }
......
...@@ -8,13 +8,13 @@ public class Main { ...@@ -8,13 +8,13 @@ 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();
} }
......
...@@ -18,7 +18,7 @@ public class MyThread extends Thread { ...@@ -18,7 +18,7 @@ public class MyThread extends Thread {
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 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"); System.out.println("Thread " + id + " is going to get out from the critical region");
sem.exit(); sem.exit();
}//end run }//end run
......
package semaphore;//Classe che implementa tramite wait e notify un Semaforo. package semaphore;//Class that implements a semaphore, thanks to wait() and notify()
//Se non vi ricordate cosa sia un semaforo, google is your best friend. //If you need to recap what a semaphore is, Google is your best friend.
public class Semaphore { public class Semaphore {
private int maxNumber; //numero massimo di thread private int maxNumber; //Maximum number of threads
private int threadsIn; //conteggio dei thread nell'area critica private int threadsIn; //Number of threads in the critical region
Semaphore(int max) { Semaphore(int max) {
maxNumber = max; maxNumber = max;
...@@ -12,7 +12,7 @@ public class Semaphore { ...@@ -12,7 +12,7 @@ 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 we reach the maximum number of threads, new threads need 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 +23,7 @@ public class Semaphore { ...@@ -23,7 +23,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) //Whena thread exits the critical region, it awakens another thread that is waiting (if there is one)
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