Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:27:00+00:00 2026-06-01T06:27:00+00:00

I have a server that receives various xml messages from clients (one thread per

  • 0

I have a server that receives various xml messages from clients (one thread per client) and routes the messages to different functions depending on the message type. Eg. if the first element in the messages contains the string ‘login’ it signifies that this is a login message so route the message to the login() function.

Anyway, I want to make this message so things don’t get messed up if multiple clients are connected and the dispatcher switches threads in middle of the message routing. So here is how I am routing the messages –

public void processMessagesFromClient(Client client)
{
    Document message;

    while (true)
    {
        try
        {
            message = client.inputStream.readObject();

            /*
             * Determine the message type
             */
            String messageType = getMessageType(message);

            // Route the message depending on its type
            switch (messageType)
            {
                case LOGIN:
                    userModel.handleLogin();
                ...
                ...
                ...
                etc...
             }
        } catch(Exception e) {}
   }

So how can I make this thread safe? I figure I need to put a synchronise statement in somewhere but Im not sure where. Also Ive been reading around on the subject and I found this post which says there is an issue with using synchronise on ‘this’ –
https://stackoverflow.com/a/416198/1088617

And another post here which says singletons aren’t suitable for using synchronise on (My class in the code above is a singleton) – https://stackoverflow.com/a/416202/1088617

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:27:01+00:00Added an answer on June 1, 2026 at 6:27 am

    Your class is already thread safe, because you are only using local variables.
    Thread safety only comes into play when you access class state (ie fields), which your code doesn’t (seem to) do.

    What you are talking about is serialization – you want to funnel all message processing through one point to guarantee that message processing is one-at-a-time (starts and finishes atomically). The solution is simple: Employ a static synchronized method:

    public void processMessagesFromClient(Client client) {
        Document Message;
    
        while (true) {
            processMessage(client);
        }
    }
    
    private static synchronized processMessage(Client client) {
        try {
            message = client.inputStream.readObject();
    
            String messageType = getMessageType(message);
    
            // Route the message depending on its type
            switch (messageType) {
                case LOGIN:
                    userModel.handleLogin();
                ...
                etc...
            }
        } catch(Exception e) {}
    }
    

    FYI static synchronized methods use the Class object as the lock. This code will make your code behave like a single thread, which your question seems to want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Server app that receives sound from client, i then broadcast
I have 1 activity that receives a json object from the server (database) and
I have a server application that receives information over a network and processes it.
I have a SWF file that connects to a remote flash server and receives
I have a server that connects to multiple clients using TCP/IP connections, using C
I have a viewModel with an observableArray that gets updated from the server, then
I have a server that sends bytes back to a client app, when the
I have a UDP server that reflects every ping message it receives (this works
I have a Python application that, to be brief, receives data from a remote
I have a server that sends data via a socket, the data is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.