?
Solved

Communications between threads

Posted on 1998-07-02
6
Medium Priority
?
219 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

801 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