Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Communications between threads

Posted on 1998-07-02
6
Medium Priority
?
225 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 300 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

636 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