creating threads based on information read from a configuration file

Hi. I'm writing a program where I need to use threading. I've done some threading in my past, but I'm no expert. What I need to do is create a number of threads dependant on how many are requested

So I have a conf file where:

SomeValue = String1, String2, String3, String4

I read those values and right now I stick them in an Array. So in this case I have four slots in the array filled.

How can I declare a simple unique thread for each of them?

I've searched the forums and found a few references to this, but I'm not able to make it work. I'm still googling for answers now. If anyone understands what I'm attempting and can help I'd really appreciate it.

Thanks,

James

[731 byte] By [jpifera] at [2007-10-2 13:17:44]
# 1

Thread[] threads = new Thread[numItems];

for (int ix = 0; ix < numItems; ix++) {

Runnable runnable = new MyRunnable(items[ix]);

threads[ix] = new Thread(runnable):

threads[ix].start();

}

http://java.sun.com/docs/books/tutorial/essential/threads/

jverda at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the reply. I'm looking at the tutorials, but I'm having some trouble understanding. It's been quite a while since I played with threads and I can't find the code I did them in to refresh my memory. I have a small application in one class with several funtions like:

public class MyProgram {

public void funtion1() {

does something

}

public void function2() {

does something

}

public MyProgram(String args[]){

calls functions;

}

public static void main(String[] arguments)

{

MyProgram instance = new MyProgram(arguments);

}

Where do I implement runnable in a case like this?

Do I get rid of public static void main somehow?

Does MyRunnable need to be a seperate class?

Back to the tutorials....

Thanks,

James

jpifera at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...
# 3
Think I might have it now....James
jpifera at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...
# 4

> Where do I implement runnable in a case like this?

Wherever you have a unit of work that you want to run concurrently with other processing.

> Do I get rid of public static void main somehow?

No.

> Does MyRunnable need to be a seperate class?

This class can be your Runnable, but good design practices suggest that it will not be. Don't be shy about creating new classes.

jverda at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...
# 5

Man, this is killing me. I know its the basics, but I'm not grasping it. I tried to create another class that is runnable, and it has my functions that do really do stuff.

The problem is I need to read some data from a file so I know how many threads to create. Then I guess I need to pass that data because the functions in the Runnable require it.

Do I reread the data from the Runnable class?

How do I pass data to the Runnable class?

Thanks,

James

jpifera at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...
# 6
Ok, finally got it. Everything is still in one class file, but I'll try to split that out. Also not sure how to do a couple other things, but it's looking better. Thanks for the help.James
jpifera at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...
# 7
You're welcome. I'm glad you're making progress. :-)If you get stuck again, post details of your problem/code.
jverda at 2007-7-13 10:50:50 > top of Java-index,Java Essentials,Java Programming...