Help to find solution with class Thread.
I have a program that needs some optimisation.
publicvoid init()
{
new Thread(new Proc1()).start();
new Thread(new Proc2()).start();
new Thread(new Proc3()).start();
new Thread(new Proc4()).start();
}
Every ProcX does the same type of operations, for example
class Proc1extends Frameimplements Runnable
{
publicvoid run()
{
long t1=new Date().getTime();
new Step1(0,3).pref();
new Step2(3,0,0).pref();
new Step2(7,4,4).pref();
new Step3(0,4,0,1).pref();
long t2=new Date().getTime();
System.out.println("time1="+(t2-t1));
}
class Proc2extends Frameimplements Runnable
{
publicvoid run()
{
long t1=new Date().getTime();
new Step1(4,7).pref();
new Step2(3,1,1).pref();
new Step2(7,5,5).pref();
new Step3(5,8,2,3).pref();
long t2=new Date().getTime();
System.out.println("time2="+(t2-t1));
}
ets...
The number of Steps is always the same for each Thread ProcX. But values in Steps are different, I want to make them variables.
But there is a problem. I need to make an X number of ProcX, not only 4, and X must be a variable.
In my dreams code should be smth like:
publicvoid init()
{
int X;
for(X = 1; X = 10; X++){
new Thread(new ProcX()).start();
}
}
Do you have any idea about it? Or it's unreal?
[3237 byte] By [
sootkina] at [2007-10-2 21:50:21]

> I have a program that needs some optimisation.
> > public void init()
>{
>new Thread(new Proc1()).start();
>new Thread(new Proc2()).start();
>new Thread(new Proc3()).start();
>new Thread(new Proc4()).start();
> }
>
>
> Every ProcX does the same type of operations, for
> example
>
> > class Proc1 extends Frame implements Runnable
> {
>public void run()
>
>long t1=new Date().getTime();
> new Step1(0,3).pref();
>new Step2(3,0,0).pref();
> new Step2(7,4,4).pref();
>new Step3(0,4,0,1).pref();
> long t2=new Date().getTime();
>System.out.println("time1="+(t2-t1));
>
> class Proc2 extends Frame implements Runnable
> {
> public void run()
>long t1=new Date().getTime();
> new Step1(4,7).pref();
>new Step2(3,1,1).pref();
> new Step2(7,5,5).pref();
>new Step3(5,8,2,3).pref();
> long t2=new Date().getTime();
>System.out.println("time2="+(t2-t1));
>
>
> ets...
>
> The number of Steps is always the same for each
> Thread ProcX. But values in Steps are different, I
> want to make them variables.
>
> But there is a problem. I need to make an X number of
> ProcX, not only 4, and X must be a variable.
>
> In my dreams code should be smth like:
> [code]
> public void init()
>{
> int X;
>
> for(X = 1; X = 10; X++){
> new Thread(new ProcX()).start();
> }
>}
> de]
>
> Do you have any idea about it? Or it's unreal?
well, why not just try your solution? ?
One way is to use reflection api:
String proc = "mypack.Proc";
Class<?> cls = getClass();
for( int i =1; i <= N; i++ ){
try{
Class<?> myClass = cls.forName(proc + i);
Runnable runnable = (Runnable) myClass.newInstance();
new Thread(runnable).start();
}catch(Exception e){
e.printStackTrace();
}
}