[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: JAVA multithread questions.
Eccential what you are tring to do is build a multithreaded Master Thread\Slave
thread arrangment.
I'm go describe a this system with psuedo-java notation. ( I'm in a Hurry!!).
The main MASTER thread of execution ( your PS ) will be responsible for
recieving requests from Q client and
starting up all the SLAVE threads .
I assume Q is a sockets based client and will talk to PS through a server side
socket interface.( This could just
as well be a serial port interface as well). In this strategy the slave threads
work in sync with the master by
using WAIT/NOTIFY monitor calls. This could easily be modified using a queue
mechanism to alow
async operations between the master and slave threads.
Please check Doug Lea's Book "Concurrent programmin in Java" (Addison and Wesley
Publishers) for
a more indepth descusion of the subject.
BTW in the future you should post these kinds of questions to the general
comp.java.programmer news grousp
Cheers
Chris
The pseudo code looks like this
// Systems Bootstrap class
public class PS_SERVER {
public static main(String [] args ) {
Thread mainThread = new Thread("main",new PS_MASTER()); // creat the
master thread object
mainThread.start(); // now start the ball rolling
}
}
// Master controller Runnable Class
public class PS_MASTER implements Runnable{
private RunnableSlave R1 ;
private RunnableSlave R2;
private RunnableSlave R3;
public PS_MASTER()
{
// create the runnable slaves and start them in new threads
R1 = new RunnableSlave();
R2 = new RunnableSlave();
R3 = new RunnableSlave();
Thread TR1 = new Thread("Slave_1",R1);
Thread TR2 = new Thread("Slave_2",R2);
Thread TR3 = new Thread("Slave_3",R2);
TR1.start();
TR2.start();
TR3.start();
}
public run()
{
while(true){
// accept request and dispatch to the appropriate slave
// thread
Object request = serversocket.accept() // pseudo server side socket
if( request_for_slave_1 )
R1.doSomeWorkNow(request);
if( request_for_slave_2 )
R2.doSomeWorkNow(request);
if( request_for_slave_3 )
R3.doSomeWorkNow(request);
// get the results now
Obect = R1.getResults();
............ more code .......
............ more code .......
............ more code .......
}
}
}
// Worker Slave Runnable Class
public class RunnableSlave implements Runnable{
private Object workToDo;
private Object resultOfCall;
public RunnableSlave() {
}
public synchronized run(){
while(true) {
wait() ; // wait until called to do some work
// do the work requested in workToDo Object
// call connected servers or whatever
// create result object for later retrieval by master
resultOfCall = new Object();
}
}
public synchronized doSomeWorkNow(Object work) {
workToDo = work; // tell thread what to do
notify(); // wake the thread up to do the work
}
public Object getResults(){
return resultsOfCall;
}
}
Chien-Lung Wu wrote:
> Hi,
>
> I am doing java networking program. Since my project have a little trick
> architecture, I am planning to use JAVA as my design language.
>
> The Questions are:
> 1. one of my machine called PS have to connect to 3 different servers (R1,
> R2, and R3) to collect info. So PS have to make connect with R1, R2, and
> R3. I want PS to connect Servers independently, that means, the
> connection is concurrent, not sequence. How can I do that?
>
> 2. I think PS is a very important part in my project. As I mentioned on 1.
> PS is a client to communicate with 3 servers. However, PS is also a serevr
> to be trigger by Q. How can I deal with this situation? Is JAVA
> multithread can solve my question?
>
> The configuration I try to solve is as following.
>
> +-<-------------->R1
> /
> Q<---------> PS-<---------------> R2
> \
> +---<-----------> R3
>
> How can I use java multithread to desigm my program, such that PS can
> communicate with R1, R2, and R3 respectly, and PS also can receive Q's
> triger to satisfy Q's request?
>
> Can anyone give me some hints or guideline to do it? Do anyone can point
> out some material about JAVA multithread and java network programming?
>
> Any suggestion will be appreciated.
>
> C. L.
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to java-linux-request@xxxxxxxxxxxxxxxxxx
> with a subject of "unsubscribe". Trouble? Contact listadm@xxxxxxxxxxxxxxxxxx
- --------------------------------------------------------------------
For more information on Linux in India visit http://www.linux-india.org/
Please do not post HTML email to this mailing list. HTML mails will be
thoroughly ignored and derisively sniggered at in private.
------------------------------