wanting for example for thread control for agent

L

Lucia

Hello,

I'm programming an agent. Several threads will be started in my agent.
Each thread saves the result in an object: tasks. It's something like
an array list. And I want to implement a thread control, which is able
to :

1. start or stop these threads for need
2. choose one task from one of the thread to do. ( you can think of
the tasks as messages, do a task is nothing else e.g. sending this
message to server)

Because I haven't a lot of experience of threads, I need help. Has
anyone already some code examples for study??

thanks and best regards

lucia
 
D

Dave Rudolf

Lucia said:
Hello,

I'm programming an agent. Several threads will be started in my agent.
Each thread saves the result in an object: tasks. It's something like
an array list. And I want to implement a thread control, which is able
to :

1. start or stop these threads for need

The easiest way I can think of is to subclass Thread and give it flags to
determine if the thread is suspended or killed or whatever. Then, in your
run() method, you would have
to periodically check these flags. If the thread is suspended, then you
could do a wait(), and when another thread unsuspends it, then have it
notify(). If the thread has been killed, then stop whatever you are doing,
clean up anything that the run method used (e.g. sockets, streams, data
structures, etc.), and then exit the run method. This is the "soft" way to
implement thread suspension and termination.
2. choose one task from one of the thread to do. ( you can think of
the tasks as messages, do a task is nothing else e.g. sending this
message to server)

I'm not totally clear on what you are asking, but if you model each task as
a thread (or wrap it in one), then you can have these lists of task threads
and start them when you like. Alternatively, you could have a singleton that
manages all tasks, and have some number of threads that are always running.
These threads can get tasks from the singleton and process them.
Because I haven't a lot of experience of threads, I need help. Has
anyone already some code examples for study??

thanks and best regards

lucia

Hope that helps.

Dave
 
L

Lucia

hi, Dave,

thank you for your answer.

I try to explain the point 2 again. The tasks will be generated by the
threads. But exact one task can be executed at one time. So I need a
control to choose one task from the task list which created by the
different threads. And while executing the task, all threads must be
stopped. These threads can continue to run until the task is
completed.

hope you can help me again

lucia
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top