diff --git a/.gradle/6.7/executionHistory/executionHistory.bin b/.gradle/6.7/executionHistory/executionHistory.bin index 6747f8a14bb08ec1a50956ff041170aba4532049..006db8acf48496a31e27edb848d4e9fde2ec3c9d 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.bin and b/.gradle/6.7/executionHistory/executionHistory.bin differ diff --git a/.gradle/6.7/executionHistory/executionHistory.lock b/.gradle/6.7/executionHistory/executionHistory.lock index 25fc1660df19a6424171748d9d04d149c1939b53..6003b25cf6b2e5c4e0678e4f2a9ed7752bccd0e3 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.lock and b/.gradle/6.7/executionHistory/executionHistory.lock differ diff --git a/.gradle/6.7/fileHashes/fileHashes.bin b/.gradle/6.7/fileHashes/fileHashes.bin index 9c1905f6c787641f693e99df291ebdf87adff2b2..5b38618419ddfc788f36a591b5c3bac19fc9bea2 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.bin and b/.gradle/6.7/fileHashes/fileHashes.bin differ diff --git a/.gradle/6.7/fileHashes/fileHashes.lock b/.gradle/6.7/fileHashes/fileHashes.lock index 73626d62011d0364eed1646de55c6b6a8c53d75a..e1c2d55b89a02a9aeb675b966c9c84b41b1ba939 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.lock and b/.gradle/6.7/fileHashes/fileHashes.lock differ diff --git a/.gradle/6.7/javaCompile/classAnalysis.bin b/.gradle/6.7/javaCompile/classAnalysis.bin new file mode 100644 index 0000000000000000000000000000000000000000..ba7436ee84478a68d42902afd9273664906afb93 Binary files /dev/null and b/.gradle/6.7/javaCompile/classAnalysis.bin differ diff --git a/.gradle/6.7/javaCompile/javaCompile.lock b/.gradle/6.7/javaCompile/javaCompile.lock index 35ba9bf0432ba916e33f6c5a856b4fc87dc462c6..08cc0b182a61abc80fc4680c6cebd2f7d7eeee1d 100644 Binary files a/.gradle/6.7/javaCompile/javaCompile.lock and b/.gradle/6.7/javaCompile/javaCompile.lock differ diff --git a/.gradle/6.7/javaCompile/taskHistory.bin b/.gradle/6.7/javaCompile/taskHistory.bin index 370e64289da699237ab29d76541262222e7e3cdc..5b6be10dccac7a7a3c180e10f272d4805fe7d8b2 100644 Binary files a/.gradle/6.7/javaCompile/taskHistory.bin and b/.gradle/6.7/javaCompile/taskHistory.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index be5109cf65a07c66599f2fc5db4bce04f2904bdf..b6688df6c1c50a8fe60092fa32015dd3ecb48d73 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index f205b0ddfe9c49ccd03b257269eef4658d48a994..e492cc146e32be5d9e2c086f4083fa9a005f9483 100644 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000000000000000000000000000000000000..433ed8ab65bd7dcf8bbfc2d09467c342b0c055fc --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +setup_SDP \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 61a9130cd9669c3843e6445dfe1fee2d493869bc..fb7f4a8a465d42b4a0390d464b83b99e8465bba7 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index ba1ec5c7e20c12eb5b893684698eab151c4a8803..611e7c8ad7b91e576987485e8678b0fdad4193ae 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index cba2c9c2e3029071f8bb2d64d18fe74316454674..7bf9a2a6591ca01328fdba1b04c2381f841f25ae 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,9 @@ -# Setup test SDP -This repository contains a simple example that enables you to test if the **IntelliJ** IDE works correctly. +# Lab2 Examples -* You can import the project directly from a **V**ersion **C**ontrol **S**ystem, by providing the following URL: -https://ewserver.di.unimi.it/gitlab/riccardopresotto/setup_test_sdp.git +* Threads Join - +* Wait & Notify -* Otherwise, take care to import the project as a Gradle Project - -* The testing files are located in the directory: *setup\_test\_sdp/src/main/java* +* Producer-Consumer diff --git a/build/classes/java/main/TCPClient.class b/build/classes/java/main/TCPClient.class deleted file mode 100644 index fb2030595ea89798deb9a5b050a65942ae1102c2..0000000000000000000000000000000000000000 Binary files a/build/classes/java/main/TCPClient.class and /dev/null differ diff --git a/build/classes/java/main/TCPServer.class b/build/classes/java/main/TCPServer.class deleted file mode 100644 index f81e36253e1100b775bf383b87e96b8eb2055fda..0000000000000000000000000000000000000000 Binary files a/build/classes/java/main/TCPServer.class and /dev/null differ diff --git a/build/classes/java/main/join/Main.class b/build/classes/java/main/join/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..2946019686eaf577e2b43de45880ddb9d13335a0 Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..dff7bcaab0e7f2ef5971449a0b71b8c0525d51cf Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..8d4380cff125d333e16aad1548d4d12f19d7ddc7 Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..7c87885d1ee0c579c875527e5f961084a20dee0c Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..6d346b3304e8c2e1f5348edd569ab3798813d841 Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..b155823de0a0ced6fb21c8929b331cf67bde4911 Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..6e7db4067f6e83ba23a282637a564fab022f557d Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..957a150b4844b534ed3f15cc240f35e41179407f Binary files /dev/null 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 new file mode 100644 index 0000000000000000000000000000000000000000..46ce2ecb0fd05ade46b044b47cf90acbfbaf1f36 Binary files /dev/null and b/build/classes/java/main/semaphore/Semaphore.class differ diff --git a/build/tmp/compileJava/source-classes-mapping.txt b/build/tmp/compileJava/source-classes-mapping.txt index a96fe92536f9f2604a5df3bc41c40bb8c40e00ce..2fa410a8a001bcda7792e26309de1c9c357efea8 100644 --- a/build/tmp/compileJava/source-classes-mapping.txt +++ b/build/tmp/compileJava/source-classes-mapping.txt @@ -1,4 +1,18 @@ -TCPServer.java - TCPServer -TCPClient.java - TCPClient +join/Main.java + join.Main +join/MyThread.java + join.MyThread +semaphore/Main.java + semaphore.Main +semaphore/MyThread.java + semaphore.MyThread +semaphore/Semaphore.java + semaphore.Semaphore +producerConsumer/Consumer.java + producerConsumer.Consumer +producerConsumer/Main.java + producerConsumer.Main +producerConsumer/Queue.java + producerConsumer.Queue +producerConsumer/Producer.java + producerConsumer.Producer diff --git a/src/main/java/TCPClient.java b/src/main/java/TCPClient.java deleted file mode 100644 index c6441695de2e538012832b82986c3cda818f1d3b..0000000000000000000000000000000000000000 --- a/src/main/java/TCPClient.java +++ /dev/null @@ -1,38 +0,0 @@ -import java.io.*; -import java.net.*; - -class TCPClient { - public static void main(String argv[]) throws Exception { - String sentence; - String modifiedSentence; - - /* Inizializza l'input stream (da tastiera) */ - BufferedReader inFromUser = - new BufferedReader(new InputStreamReader(System.in)); - - /* Inizializza una socket client, connessa al server */ - Socket clientSocket = new Socket("localhost", 6789); - - /* Inizializza lo stream di output verso la socket */ - DataOutputStream outToServer = - new DataOutputStream(clientSocket.getOutputStream()); - - /* Inizializza lo stream di input dalla socket */ - BufferedReader inFromServer = - new BufferedReader(new - InputStreamReader(clientSocket.getInputStream())); - - /* Legge una linea da tastiera */ - sentence = inFromUser.readLine(); - - /* Invia la linea al server */ - outToServer.writeBytes(sentence + '\n'); - - /* Legge la risposta inviata dal server (linea terminata da \n) */ - modifiedSentence = inFromServer.readLine(); - - System.out.println("FROM SERVER: " + modifiedSentence); - - clientSocket.close(); - } -} diff --git a/src/main/java/TCPServer.java b/src/main/java/TCPServer.java deleted file mode 100644 index 5e565ede1f704aee1d32ed2898de03a15bfaec93..0000000000000000000000000000000000000000 --- a/src/main/java/TCPServer.java +++ /dev/null @@ -1,43 +0,0 @@ -import java.io.*; -import java.net.*; - -class TCPServer { - - public static void main(String argv[]) throws Exception - { - String clientSentence; - String capitalizedSentence; - - /* Crea una "listening socket" sulla porta specificata */ - ServerSocket welcomeSocket = new ServerSocket(6789); - - while(true) { - /* - * Viene chiamata accept (bloccante). - * All'arrivo di una nuova connessione crea una nuova - * "established socket" - */ - Socket connectionSocket = welcomeSocket.accept(); - - /* Inizializza lo stream di input dalla socket */ - BufferedReader inFromClient = - new BufferedReader(new - InputStreamReader(connectionSocket.getInputStream())); - - /* Inizializza lo stream di output verso la socket */ - DataOutputStream outToClient = - new DataOutputStream(connectionSocket.getOutputStream()); - - /* Legge una linea (terminata da \n) dal client */ - clientSentence = inFromClient.readLine(); - - capitalizedSentence = clientSentence.toUpperCase() + '\n'; - - /* Invia la risposta al client */ - outToClient.writeBytes(capitalizedSentence); - - connectionSocket.close(); - - } - } -} diff --git a/src/main/java/join/Main.java b/src/main/java/join/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..fedf709e108f112645527d78f906be8ce1faabd5 --- /dev/null +++ b/src/main/java/join/Main.java @@ -0,0 +1,33 @@ +package join; + + +import java.util.ArrayList; +import java.util.Random; + +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 + for (Thread t : threads) { + t.join(); + } + + System.out.println("...All thread finished!"); + } + +} + diff --git a/src/main/java/join/MyThread.java b/src/main/java/join/MyThread.java new file mode 100644 index 0000000000000000000000000000000000000000..ac6ed1c0d58f803ff8048aa80cb47b3bfe2a7265 --- /dev/null +++ b/src/main/java/join/MyThread.java @@ -0,0 +1,30 @@ +package join; + +import java.util.Random; + +public class MyThread extends Thread { + private Random rnd; + + MyThread(Random r) { + rnd = r; + } + + public void run() { + int seconds = rnd.nextInt(10) + 1; + + System.out.println("A thread started!"); + + try + { + Thread.sleep(seconds * 1000); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + + System.out.println("A thread died!"); + + } + +} diff --git a/src/main/java/producerConsumer/Consumer.java b/src/main/java/producerConsumer/Consumer.java new file mode 100644 index 0000000000000000000000000000000000000000..9b14fba6d832dbc91bb151fcb07a49fd04ba8ae1 --- /dev/null +++ b/src/main/java/producerConsumer/Consumer.java @@ -0,0 +1,22 @@ +package producerConsumer; + +public class Consumer implements Runnable { + + private final Queue queue; + private final String id; + + public Consumer(String id, Queue q) { + this.id = id; + queue = q; + } + + public void run() { + while (true) { + consume(queue.take()); + } + } + + public void consume(String message) { + System.out.println("Cons. " + id + ": prelevato " + message); + } +} diff --git a/src/main/java/producerConsumer/Main.java b/src/main/java/producerConsumer/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..dee4cb909290f9dc249d541c78e966d2612cfefa --- /dev/null +++ b/src/main/java/producerConsumer/Main.java @@ -0,0 +1,17 @@ +package producerConsumer; + +public class Main { + public static void main(String[] args) { + Queue q = new Queue(); + Producer p1 = new Producer("p1", q); + Producer p2 = new Producer("p2", q); + Producer p3 = new Producer("p3", q); + Consumer c1 = new Consumer("c1", q); + Consumer c2 = new Consumer("c2", q); + new Thread(p1).start(); + new Thread(p2).start(); + new Thread(p3).start(); + new Thread(c1).start(); + new Thread(c2).start(); + } +} diff --git a/src/main/java/producerConsumer/Producer.java b/src/main/java/producerConsumer/Producer.java new file mode 100644 index 0000000000000000000000000000000000000000..628d4b52cc27f2b2f4f19417bca9c50a12ee0877 --- /dev/null +++ b/src/main/java/producerConsumer/Producer.java @@ -0,0 +1,31 @@ +package producerConsumer; + +public class Producer implements Runnable { + + private final String id; + private final Queue queue; + + public Producer(String id, Queue q) { this.id = id; queue = q; } + + public void run() { + while (true) { + String message = produce(); + System.out.println("Prod. " + id + ": inserisco " + message); + queue.put(message); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private int counter = 0; + + public String produce() { + counter++; + return "Messaggio da " + id + " n. " + counter; + } + +} diff --git a/src/main/java/producerConsumer/Queue.java b/src/main/java/producerConsumer/Queue.java new file mode 100644 index 0000000000000000000000000000000000000000..3ab60936026b41677f9a3febec5bb6dc50d93e7c --- /dev/null +++ b/src/main/java/producerConsumer/Queue.java @@ -0,0 +1,34 @@ +package producerConsumer; + +import java.util.ArrayList; + +public class Queue { + + public ArrayList buffer = new ArrayList(); + + public synchronized void put(String message) { + buffer.add(message); + notify(); + } + + public synchronized String take() { + String message = null; + + while(buffer.size() == 0) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + if(buffer.size()>0){ + message = buffer.get(0); + buffer.remove(0); + } + + + return message; + } + +} diff --git a/src/main/java/semaphore/Main.java b/src/main/java/semaphore/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..1315fcb9c50198b78c467582bdfb8ecf244117fb --- /dev/null +++ b/src/main/java/semaphore/Main.java @@ -0,0 +1,23 @@ +package semaphore; + +import java.util.ArrayList; +import java.util.Random; + +public class Main { + public static void main(String arg[]) throws Exception { + Random r = new Random(); + ArrayList threads = new ArrayList(); + Semaphore s = new Semaphore(4); + //create some threads + for (int i=0; i<10; i++) { + MyThread mt = new MyThread(r, i, s); + threads.add(mt); + } + + //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 new file mode 100644 index 0000000000000000000000000000000000000000..b15b661a4991216a734d2bb80e78cdd9fbaaf642 --- /dev/null +++ b/src/main/java/semaphore/MyThread.java @@ -0,0 +1,32 @@ +package semaphore; + +import java.util.Random; + +public class MyThread extends Thread { + private Random rnd; + private int id; + private Semaphore sem; + + MyThread(Random r, int i, Semaphore s) { + rnd = r; + id = i; + sem=s; + } + + public void run() { + wasteSomeTime(); //Simulate the thread is doing something else + 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 + System.out.println("Thread " + id + " is going to get out from the critical region"); + sem.exit(); + }//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 new file mode 100644 index 0000000000000000000000000000000000000000..f075e31a324242c6bdcc46cf721d4beffac57d39 --- /dev/null +++ b/src/main/java/semaphore/Semaphore.java @@ -0,0 +1,30 @@ +package semaphore;//Classe che implementa tramite wait e notify un Semaforo. +//Se non vi ricordate cosa sia un semaforo, google is your best friend. + +public class Semaphore { + private int maxNumber; //numero massimo di thread + private int threadsIn; //conteggio dei thread nell'area critica + + Semaphore(int max) { + maxNumber = max; + threadsIn = 0; + } + + public synchronized void enter() { + System.out.println("" + threadsIn + " in the critical region..."); + //quando abbiamo raggiunto il numero massimo di thread, chi vuole entrare aspetta + while (threadsIn >= maxNumber) { + try {this.wait();} + catch(InterruptedException ie) {ie.printStackTrace();} + } + + threadsIn++; + } + + public synchronized void exit() { + threadsIn--; + //quando un thread esce dall'area critica, sveglia qualcuno in attesa di entrare (se presente) + this.notify(); + } +} +