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]
# 1

> 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? ?

georgemca at 2007-7-14 1:06:08 > top of Java-index,Java Essentials,New To Java...
# 2
Couse ProcX is just a name of class, it cannot be a variable and I'm not sure that it is possible to create classes using cycle.I would be greatful if anyone tell how it possible.
sootkina at 2007-7-14 1:06:08 > top of Java-index,Java Essentials,New To Java...
# 3
well, you're not creating classes, you're creating instances of a class. if you're not sure, why not just try it anyway? always the best policy! be brave!I'm not 100% sure what you're trying to achieve, but it looks like you're on the right path to me
georgemca at 2007-7-14 1:06:08 > top of Java-index,Java Essentials,New To Java...
# 4

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();

}

}

raindropa at 2007-7-14 1:06:08 > top of Java-index,Java Essentials,New To Java...