dimanche 12 juin 2016

How to start thread via reflection?

I have dosens of classes that implements Runnable and have public Thread field inside of each one. I wish to run all of them one by one in several parallel threads. For example, there are 6 processors in my system. 1st processor : run 1st class, finish and run 2nd class. And so on... All classes are loaded in an Object[] array (just load, not start) for simply iterate over them. And iterating I run each selected class. For example, declaration of one class:

public class Example implements Runnable {
   public static int need;
   public static Map <etype, Set<ArtCof>>[] scale;
   public Thread $thr;

   public void run () {
       need = need();
       Scale.scale(scale);
   }

   public int need() {
   .... etc

Because all of them have different names I can't call ClassName.thread.start(). That's why it seemed to me a good way is to run them via Reflection API. But I don't get how to get .start() or may be .run() method.





Aucun commentaire:

Enregistrer un commentaire