Solved

Communications between threads

Posted on 1998-07-02
6
214 Views
Last Modified: 2010-03-30
Hi, this is novice in Java writing.
I've been making 2 layered socket program.
A lot of clients, a mid server, and a server.
Clients connect to the mid server.
Mid server receives all data from clients, sends them to server,
receives data from server, and sends them back to the clients.
Server is a server.
Each process is on different system.
What I was wondering is about the structure of mid server.
How can I make it?
And which classes or methods do I have to use for it?

Thanks in advance.
0
Comment
Question by:lmh99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:lmh99
ID: 1224363
Edited text of question
0
 
LVL 6

Expert Comment

by:gadio
ID: 1224364
What is the main function of the server? Is it just to recive and store data and enable query on that data? If so, the architecture that you describe is very similar to the big databases (oracle informix, etc.) general design. They have the server and the and the mid server layers inside them. If a database is not what you need or you want to add another tier, you have to be more specific and describe some requeirments of the system. Where do you think that there is going to be bottelnecks (in the mid server I guess)? What sort of main services does the server give? Is it heavy cpu tasks (cpu bound) or information server (disk/io bound)?
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
ID: 1224365
As you correctly noted, the mid-server should have at least two threads - one is the server thread which listens to the client, the other is the client thread which communicates with the server. Your question, I guess, is how to synchronize these two threads.

I can think of two ways of doing that. The first one is probably easier but is more specific to your synchronization problem.  You can use the piped streams where one thread (the server thread which listens to the client) writes to the output pipe while the other (the client one which talks to the server) reads form it. The reading thread will block until there is data in the input pipe (i.e. once the server thread starts writing).

The second option is to use wait/notify combination.  Here you would have some common storage, let's say a stack. The client thread goes into the loop checking whetehr the stack is not empty calling wait() if it is. The server thread puts something on the stack and calls (notify) thus waking the client thread. Which empties the stack send the data to the server and continues to wait.

Let me know which method you want to use and whetehr you need code samples.
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

Author Comment

by:lmh99
ID: 1224366
Thanks for your answer.
I have some questions about your answer.
The two ways that you suggested are good for me. And I need sample codes of them.
With your two ways, is it possible that I make "mid server" send responses from server
to "proper clients"?
I want to know how I can do that.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1224367
Yes, I got your question. You do not have to spend any more points to get the code samples. I'll try to post them later today. Sorry I did not answer earlier because of the long weekend.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1224368
1. Piped streams:

            PipedInputStream inputPipe = new PipedInputStream();
            DataInputStream pipeDataInput = new DataInputStream(new BufferedInputStream(inputPipe));

            PipedOutputStream outputPipe = new PipedOutputStream(inputPipe);
            Data OutputStream pipeDataOutput = new DataOutputStream(new BufferedOutputStream(outputPipe)

Now pass the pipeDataOutput to the thread which gets data from the client and write that data into this output stream.  Use pipeDataInput in the thread which has to send data to the server. It will read the data from the input stream blocking until they become available.

2.
    // This code resides in the thread which communicates with the server.
    synchronized (stack)
    {
        while (stack.isEmpty())
        {
            try
            {
                wait();
            }
            catch (InterruptedException e)
            {
            }

            // We get here only when the stack becomes non-emty
            data = stack.pop();
        }
    }
    // Send the data


    // This code is in the thread which gets data from the client

    // Get data from client

    synchronized (stack)  // (they should be synchronized on the same object)
    {
        //  Put the data on the stack
        stack.push(data);
   
        // Notify the waiting thread
        notify();
    }
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question