Multi-threading

M

murali

Hi,
I need some help on multi-threading for my program in java.The
program generates a certain number of offsprings per generation and
chooses the best one at the end of each generation.I have to create
threads and generate certain number of offsprings per thread.The
number of threads per generation are specified.I have to compare the
offsprings of all the threads in a particular generation and choose
the best one.It would be great if I could get a general outline on
using threads for my program.
Thanks,
Murali.
 
H

Harald Hein

murali said:
I need some help on multi-threading for my program in java.The
program generates a certain number of offsprings per generation and
chooses the best one at the end of each generation.I have to create
threads and generate certain number of offsprings per thread.The
number of threads per generation are specified.I have to compare the
offsprings of all the threads in a particular generation and choose
the best one.It would be great if I could get a general outline on
using threads for my program.

Please give us the name of your professor at the University of Houston,
so we can send the solution directly to him. It would also help if you
give us the exact title and number of that homework.
 
M

murali

Please give us the name of your professor at the University of Houston,
so we can send the solution directly to him. It would also help if you
give us the exact title and number of that homework.
 
B

Brad BARCLAY

murali said:
Hi,
I need some help on multi-threading for my program in java.The
program generates a certain number of offsprings per generation and
chooses the best one at the end of each generation.I have to create
threads and generate certain number of offsprings per thread.The
number of threads per generation are specified.I have to compare the
offsprings of all the threads in a particular generation and choose
the best one.It would be great if I could get a general outline on
using threads for my program.

Let me guess -- a genetic AI assignment, right?

Briefly, using threads in Java is pretty simple. Just do the following:

1) Create a class that implements the java.lang.Runnable
interface,
2) Implement the following method:

public void run() { ... }

...putting in it the code you need to use to process a single
offspring. Add whatever other methods to your class you want to get and
set data elements, making them synchronized (via the "synchronized"
keyword) where necessary.

3) Create an array of java.lang.Thread and an array of your new object
of a fixed size -- probably the size of your "generation" (we'll call it
'n'),

4) Initialize n copies of your Runnable object with suitable starting data,

5) Initialize n Thread objects, each with the corresponding Runnable
object as its sole parameter,

6) Start each thread by calling each Thread instances "start()" method.

That's the general mechanism. Note that if you _are_ doing genetic AI,
however, that doing it multithreaded will cause a performance _decrease_
if you're not working in an SMP environment. Going multithreaded in a
single CPU environment for this sort of problem is generally a bad idea
as you lose CPU cycles to the thread scheduler. It's generally better
in such a case to simply process each member iteratively.

If you need some example code of an application that runs many
identical threads, take a look at:

http://cvs.sourceforge.net/cgi-bin/...rev=1.10&content-type=text/vnd.viewcvs-markup

HTH!

Brad BARCLAY
 
M

murali

Hi Brad,
Thanks very much for your response.Here's what I have implemented
in the program;

public static synchronized Thread launch() {
Runnable r = new Runnable() {
public void run() {
....//code for generating offsprings in a thread
...
};
Thread t = new Thread(r);
t.start();
return t;
}
public static void main() {
.....
....
Thread[] t = new Thread[threads];
for ( int g = generationCounter; g < numberOfGenerations; g++)
{
for (int i = 0; i < threads; i++) {
t = launch();
}
}
.....
}

I have one more question.How do I compare the threads after they
return from launch() method,to choose the one with the best offspring?

Thanks,
Murali.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top