Solved

Communications between threads

Posted on 1998-07-02
6
209 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 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