|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4423
|
[Java] reflection + threads
...posto una porzione di codice che unisce un esempio di reflection con un sistema a thread...che ne pensate di tale implementazione?...cercavo un modo per eseguire tramite thread multipli un istanza senza che questa fosse stata prevista per l'uso a thread e senza doverla rismanacciare...
...MultiThread è la sola classe da configurare...presenta informazioni come il numero di thread...la classe da lanciare ed eventuali argomenti da passare...come tipo e valore... Codice:
public MultiThread(){
int firstThread = 1;
int lastThread = 60;
int threads = 3;
String instance = "it.ally.test.Test";
Class[] classTypeArguments = new Class[] {String.class};
Object[] classArguments = new Object[] {"una stringa"};
long startTime = System.currentTimeMillis();
try {
this.threadHandler = new ThreadHandler(firstThread, lastThread);
for (int i = 0; i < threads; i++){
this.threadWorker = new ThreadWorker(("Thread "+i), this.threadHandler,instance,classTypeArguments,classArguments);
this.threadWorker.start();
}
while(this.threadWorker.isAlive())
Thread.sleep(30);
}
catch (Exception ex) {
System.err.println("Error:" + ex);
ex.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("All thread launch in " + (endTime - startTime)+" waiting for ending of instance!");
}
Codice:
public class ThreadHandler implements Enumeration {
int _firstInstance = 0;
int _lastInstance = 0;
int _nextInstance = 0;
public ThreadHandler(int first, int last){
this._firstInstance = first;
this._nextInstance = first;
this._lastInstance = last;
}
public boolean hasMoreElements(){
return (this._nextInstance <= this._lastInstance);
}
public Object nextElement(){
return new Threads(this._nextInstance++);
}
}
Codice:
public class ThreadWorker extends Thread {
ThreadHandler _threadHandler = null;
String _instanceName = null;
Class[] _classTypeArguments = null;
Object[] _classArguments = null;
public ThreadWorker(String name, ThreadHandler threadHandler ,String instanceName,Class[] classTypeArguments,Object[] classArguments)
{
super(name);
this._threadHandler = threadHandler;
this._instanceName = instanceName;
this._classTypeArguments = classTypeArguments;
this._classArguments = classArguments;
}
public void run() {
Threads thread = null;
boolean quit = false;
while (!quit) {
thread = null;
synchronized(this._threadHandler)
{
if (!this._threadHandler.hasMoreElements()) {
quit = true;
return;
}
thread = (Threads) this._threadHandler.nextElement();
}
if (null != thread) thread.threadMethod(this.getName(),this._instanceName,this._classTypeArguments,this._classArguments);
}
}
}
Codice:
public class Threads {
int _thread = -1;
public Threads(int thread){
this._thread = thread;
}
void threadMethod(String name,String instanceMethod,Class[] classTypeArguments,Object[] classArguments) {
try {
System.out.println(this._thread+" "+name+" instance method = "+instanceMethod);
Class reflectionClass;
Constructor intArgsConstructor;
reflectionClass = Class.forName(instanceMethod);
intArgsConstructor = reflectionClass.getConstructor(classTypeArguments);
intArgsConstructor.newInstance(classArguments);
}
catch (Exception e) {
System.out.println(e);
}
}
}
...ciao Andrea... |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Ciao ally,
come esperimento mi sembra interessante, potresti postare un piccolo "caso d'uso" a mo' di esempio? Appena ho un po' di tempo (stasera) studio con calma la tua realizzazione (e se vuoi ne scambio/posto una mia su un altro argomento, su cui sto "smanacciando" in questi giorni, appena l'ho rifinita).
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#3 | |
|
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4423
|
Quote:
...ciao Andrea... |
|
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Non ho ancora approfondito perchè questo wend ho dedicato il tempo libero a questa cosa (sei hai osservazioni in merito non esitare Torno non appena provo il tuo codice, ho postato giusto per farti sapere che ci sono Ciao.
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:49.




















