From: Jérôme Benoit Date: Tue, 6 Mar 2018 14:17:30 +0000 (+0100) Subject: Add exo1 code. X-Git-Url: https://git.piment-noir.org/?p=TD_SR.git;a=commitdiff_plain;h=7e42d822cf647a90586c45a7b2f5c649f8f81cb9 Add exo1 code. Signed-off-by: Jérôme Benoit --- diff --git a/TD1/exo2/ExThread.java b/TD1/exo2/ExThread.java new file mode 100644 index 0000000..387ea8d --- /dev/null +++ b/TD1/exo2/ExThread.java @@ -0,0 +1,28 @@ +import java.util.Random; + +public class ExThread implements Runnable { + String threadName; + + ExThread(String name) { + setName(name); + } + + public void setName(String name) { + Thread.currentThread().setName(name); + } + + public void run() { + for (int j = 0; j < 10; j++) { + Random rand = new Random(); + int sleep_time = rand.nextInt(201); + try { + Thread.sleep(sleep_time); + } + catch(InterruptedException e) { + // this part is executed when an exception (in this example InterruptedException) occurs + } + String threadName = Thread.currentThread().getName(); + System.out.println(threadName + " has sleept for " + sleep_time + " ms for the " + (j + 1) + " times"); + } + } +} diff --git a/TD1/exo2/Main.java b/TD1/exo2/Main.java new file mode 100644 index 0000000..d28e6b3 --- /dev/null +++ b/TD1/exo2/Main.java @@ -0,0 +1,25 @@ +class Main { + + public static void main(String[] args) { + final int THREADS_NUMBER = 3; + ExThread threadArray[] = new ExThread[THREADS_NUMBER]; + Thread t[] = new Thread[THREADS_NUMBER]; + + for (int i = 0; i < threadArray.length; i++) { + threadArray[i] = new ExThread(String.valueOf(i)); + t[i] = new Thread(threadArray[i]); + t[i].start(); + } + + for (int i = 0; i < threadArray.length; i++) { + try { + t[i].join(); + } + catch (InterruptedException e) { + // this part is executed when an exception (in this example InterruptedException) occurs + } + } + System.out.println("Threads execution finished"); + } + +} diff --git a/TD1/exo2/Makefile b/TD1/exo2/Makefile new file mode 100644 index 0000000..e589d5d --- /dev/null +++ b/TD1/exo2/Makefile @@ -0,0 +1,90 @@ +# define compiler and compiler flag variables +# define a variable for compiler flags (JFLAGS) +# define a variable for the compiler (JC) +# define a variable for the Java Virtual Machine (JVM) + +JFLAGS = -g +JC = javac +JVM = java + +# +# Clear any default targets for building .class files from .java files; we +# will provide our own target entry to do this in this makefile. +# make has a set of default targets for different suffixes (like .c.o) +# Currently, clearing the default for .java.class is not necessary since +# make does not have a definition for this target, but later versions of +# make may, so it doesn't hurt to make sure that we clear any default +# definitions for these +# + +.SUFFIXES: .java .class + + +# +# Here is our target entry for creating .class files from .java files +# This is a target entry that uses the suffix rule syntax: +# DSTS: +# rule +# DSTS (Dependency Suffix Target Suffix) +# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency +# file, and 'rule' is the rule for building a target +# '$*' is a built-in macro that gets the basename of the current target +# Remember that there must be a < tab > before the command line ('rule') +# + +.java.class: + $(JC) $(JFLAGS) $*.java + + +# +# CLASSES is a macro consisting of N words (one for each java source file) +# When a single line is too long, use \ to split lines that then will be +# considered as a single line. For example: +# NAME = Camilo \ + Juan +# is understood as +# NAME = Camilo Juan + +CLASSES = \ + ExThread.java \ + Main.java + +# +# MAIN is a variable with the name of the file containing the main method +# + +MAIN = Main + +# +# the default make target entry +# for this example it is the target classes + +default: classes + + +# Next line is a target dependency line +# This target entry uses Suffix Replacement within a macro: +# $(macroname:string1=string2) +# In the words in the macro named 'macroname' replace 'string1' with 'string2' +# Below we are replacing the suffix .java of all words in the macro CLASSES +# with the .class suffix +# + +classes: $(CLASSES:.java=.class) + + +# Next two lines contain a target for running the program +# Remember the tab in the second line. +# $(JMV) y $(MAIN) are replaced by their values + +run: $(MAIN).class + $(JVM) $(MAIN) + +# this line is to remove all unneeded files from +# the directory when we are finished executing(saves space) +# and "cleans up" the directory of unneeded .class files +# RM is a predefined macro in make (RM = rm -f) +# + +clean: + $(RM) *.class