1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| import java.util.Scanner;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import maxDiviseursAvecExecutor.Resultat;
import maxDiviseursAvecThreads.diviseurCompteurThread;
public class maxDiviseursAvecThreadPool {
private final static int MAX = 25000;
private static ConcurrentLinkedQueue<sousTache> tacheQueue;
private static LinkedBlockingQueue<Resultat> resultatQueue;
private static class sousTache {
static int min, max; // début et fin de la plage des entiers Ã* traiter
static int maxDiviseursTh=0,nombreAvecMaxTh=0;
static int nbdiv=0;
public sousTache(int min, int max) {
this.min=min;
this.max=max;
}
public static void calcul() {
for(int i=min;i<max;i++) {
nbdiv=diviseurCompteur(i);
if(nbdiv>maxDiviseursTh){
maxDiviseursTh=nbdiv;
nombreAvecMaxTh=i;
}
}
resultatQueue.add( new Resultat(maxDiviseursTh, nombreAvecMaxTh) );
//
}
}
//Une classe pour représenter le résultat d'une sous-tâche.
private static class Resultat {
int maxDiviseurParSousTache; // Nombre maximal de diviseurs.
int nombreAvecMaxSousTache;
public Resultat(int maxDiviseursTh, int nombreAvecMaxTh) {
maxDiviseurParSousTache=maxDiviseursTh;
nombreAvecMaxSousTache=nombreAvecMaxTh;
}
}
private static class diviseurCompteurThread extends Thread {
public void run() {
while (true) {
tacheQueue.poll();
sousTache.calcul();
}
}
}
private static void diviseurCompteurAvecThreadPool(int numberOfThreads) {
long tDebut = System.nanoTime();
resultatQueue = new LinkedBlockingQueue<Resultat>();
tacheQueue = new ConcurrentLinkedQueue<sousTache>();
diviseurCompteurThread[] th = new diviseurCompteurThread[numberOfThreads];
int div = MAX/numberOfThreads;
for (int i = 0; i < numberOfThreads; i++) {
diviseurCompteurThread thtest = new diviseurCompteurThread();
th[i]=thtest;
tacheQueue.add(new sousTache((div*(i)),(div*(i+1))) );
}
// ajouter les sous-tâches Ã* tacheQueue --> tacheQueue.add(new sousTache(debut,fin));
// Démmarer les nombreDeThread threads --> Les threads exécuteront les tâches et les résultats seront placés dans la Queue des résultats resultatQueue.
// Calculer le résultat final --> lire tous les résultats de la Queue des résultats et combinez-les pour donner la réponse finale.
for (int i = 0; i < numberOfThreads; i++) {
th[i].start();
try {
Resultat resultat = resultatQueue.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long tempsEcoule = System.nanoTime()-tDebut;
System.out.println("le nombre qui a le plus de diviseurs est "+ ?????????? +" avec " + ????+ "diviseurs");
System.out.println("Temps écoule : "+tempsEcoule*Math.pow(10,-9) + " secondes");
}
public static void main(String[] args) {
int nombreDeThread;
@SuppressWarnings("resource")
Scanner clavier = new Scanner (System.in);
System.out.print("Entrer un nombre de Threads : ");
nombreDeThread = clavier.nextInt();
diviseurCompteurAvecThreadPool(nombreDeThread);
}
public static int diviseurCompteur(int N) {
int nbdivdeN=0;
for(int i=1;i<N;i++) {
if((N%i)==0) {
nbdivdeN++;
}
}
return nbdivdeN;
}
} |
Partager