Solved

Communications between threads

Posted on 1998-07-02
6
174 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
  • 3
  • 2
6 Comments
 

Author Comment

by:lmh99
Comment Utility
Edited text of question
0
 
LVL 6

Expert Comment

by:gadio
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:lmh99
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now