Selon vos besoins, j'ai ajouté les codes ci-dessous pour vous. Essayez avec cela et voyez si vous travaillez pour vous. Gardez à l'esprit que votre serveur doit disposer d'un nombre suffisant de processeurs pour les processus concurrents. Si vous confondez encore voir un exemple de programme à la fin: -
private int numberOfFileProcessed = 0;
private int numberOfThreadAlive = 0 ;
private int numberOfThreadAlive = 0;
private int numberOfThreadToBeAllowed = 10;//Change this value to control number of thread to be run concurrently
for (int i = 0; i < files.length; i++){
Object obj = parser.parse(new FileReader(files[i]));
JSONObject obj1 = METHOD_A(obj);
try{
Thread t = new Thread(new ReadFiles(obj1));
t.start();
numberOfThreadAlive++;
}catch (Exception e) {
//do something
}
while(numberOfThreadAlive > numberOfThreadToBeAllowed){//This while loop will control number of thread to be not more than 10
try{Thread.sleep(100);}catch(Exception e){}//Release the processor
}
}
private final synchronized void jobCompleted(){
numberOfFileProcessed++;
numberOfThreadAlive--;
}
while(numberOfFileProcessed < files.length){
//wait till last thread complete it's task
//I am not using thread.join() for performance
try{Thread.sleep(100);}catch(Exception e){}//Release the processor
}
private class ReadFiles implements Runnable {
JSONObject jobj;
public ReadFiles(JSONObject obj) {
jobj = obj;
}
@SuppressWarnings("unchecked")
public void run() {
try{
PrintWriter writer = new PrintWriter(...);
writer.println(jobj);
writer.close();
jobCompleted();
} catch (IOException e) {
// do something
}
}
}
est Ci-dessous un fichier de test que vous pouvez utiliser pour comprendre
package com.test.threadtest;
public final class ThreadTest {
private int numberOfFileProcessed = 0;
private int numberOfThreadAlive = 0 ;
int numberOfThreadToBeAllowed = 10;
public void processFiles(){
for (int i = 0; i < 50; i++){
try{
Thread t = new Thread(new ReadFiles(i));
t.start();
numberOfThreadAlive++;
}catch (Exception e) {
//do something
}
while(numberOfThreadAlive > numberOfThreadToBeAllowed){//This while loop will control number of thread to be not more than 10
try{Thread.sleep(100);}catch(Exception e){}//Release the processor
System.out.println("Reached maximum");
}
}
while(numberOfFileProcessed < 50){
//wait till last thread complete it's task
//I am not using thread.join() for performance
System.out.println("Number of file processed :" + numberOfFileProcessed);
try{Thread.sleep(100);}catch(Exception e){}
}
}
private final synchronized void jobCompleted(){
numberOfFileProcessed++;
System.out.println("numberOfFileProcessed :" + numberOfFileProcessed);
numberOfThreadAlive--;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest test = new ThreadTest();
test.processFiles();
System.out.println("Exit from the process");
System.exit(0);
}
private class ReadFiles implements Runnable {
int i;
public ReadFiles(int val) {
i = val;
}
@SuppressWarnings("unchecked")
public void run() {
try{
System.out.println("I am Thread : " + i);
Thread.sleep(1000);
jobCompleted();
} catch (Exception e) {
// do something
}
}
}
}
Utilisez un pool exécuteur fil. Je vois écrire, mais pas lire, dans votre exemple. Pourquoi écrivez-vous le contenu? J'espère que c'est juste une démo. – duffymo
Ouais c'est juste une démo. Je vois que je peux utiliser le pool d'exécuteurs de thread. Des exemples que vous pouvez fournir? – Gavy
https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html – duffymo