2

I'm fairly new to GWT and have never worked with Java Servlets before. I know how to make RPCs but I was wondering if there are any concurrency issues with declaring member variables in my RPC's ServiceImpl/RemoteServiceServlet class. I.e. From multiple "simultaneous" RPCs overwriting the same variable, similar to what happens with threads when a variable isn't declared volatile.

I also need to use an extra thread in my server side code, so I was wondering if there's any problems (outside of the usual thread safety problems) with declaring some of the servlet's members as static so the other thread can access the variables without a reference to the servlet instance. Is it possible for more than one instance of the same RemoteServiceServlet class to be running at the same time?

E.g.

public class MyServiceImpl extends RemoteServiceServlet implements MyService {

    // Which of these variable declarations are a bad idea in a servlet?
    private String someVariable;
    private static String anotherVariable;
    volatile private static String multiThreadedVariable;

    public void init() { ... }
    ...
}

Thanks.

1 Answer 1

3

A Servlet is a singleton, therefore there is only one instance of the MyServiceImpl class. By introducing these state variables you will run into thread-safety issues not because there might be more than one MyServiceImpl instance, but because there is only one instance that will service ALL of your requests. Unless you synchronize access to these variables, you will have thread-safety issues, so I recommend removing them completely (most likely you don't even need them).

Sign up to request clarification or add additional context in comments.

5 Comments

I see. Suppose I need them, though - how would I synchronize access to them? The same way I would with a regular multithreaded application, declaring the variables as volatile or adding the synchronized keyword to the RPC methods in MyService?
It's no different than any other java application. You can synchronize the method that's using that variable, or synchronize a block of code. Using 'volatile' (or anything else, for that matter) without actual synchronization doesn't solve your thread-safety issues.
public static synchronized changeMultiThreadedVariable() { do something here with multiThreadedVariable } is an example
btw, any kind of synchronization will slow up your application, so that's why you should consider not using state variables.
No different than any other java application...good to know. It's probably not an issue. Some of the variables are caches of XML files on disk, so they only change if the file changes. The others change only when an administrator logs into the web app and changes the settings, and I'm not expecting two admins to make changes at exactly the same time. The vast majority of the requests simply read the cached files without changing anything. Still, I'm going to be safe about it and synchronize the access. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.