Resource Class Thread Safety?

895 views
Skip to first unread message

Jesse Lancaster

unread,
Sep 17, 2013, 4:25:54 PM9/17/13
to dropwiz...@googlegroups.com
I'm a little confused about thread safety of resources in dropwizard.

http://dropwizard.codahale.com/getting-started/ has the following warning:

Warning

Resource classes are used by multiple threads concurrently. In general, we recommend that resources be stateless/immutable, but it’s important to keep the context in mind.

But in the examples its not clear to me if the methods that service requests need to be thread safe, or the resource level variables that need to be thread safe, or I'm misreading it somehow and I don't nee to worry about it.

Can someone help clarify the level of thread safety I need to build into my resource classes/methods?

Thanks.


Shan Syed

unread,
Sep 17, 2013, 4:35:47 PM9/17/13
to dropwiz...@googlegroups.com
it's basically saying: as long as you don't maintain state in shared classes, you don't have to worry about anything
it's the same deal as servlets, or any J2EE server stuff.. don't do stuff like this:
- take a request, assign a value from that request to a member variable of the resource itself

instead, pass it through down through your layers (business logic, persistence, etc..) and don't hold onto anything




--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Shan Syed

unread,
Sep 17, 2013, 4:41:24 PM9/17/13
to dropwiz...@googlegroups.com
sorry, let me clarify; don't do this unless it's exactly what you want to do :)

basically, just be aware of the fact that all requests will use the same instance of the resource; so if you mutate the resource somehow, be aware that other calls to the resource will experience this changed state


Jesse Lancaster

unread,
Sep 17, 2013, 5:24:01 PM9/17/13
to dropwiz...@googlegroups.com
But does this imply that I need to have a lock around each method body in my resources? If it uses the same instance of the class it would imply that the following is not safe to use:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> getStuff() throws Exception {

        List<String> stuff = new ArrayList<String>();

        stuff.add("SomeStuff");
        stuff.add("SomeStuff2");
        stuff.add("SomeStuff3");

        return stuff;
    }

Is that the case? If not how is the instance of the class shared without sharing the instances' methods?

Coda Hale

unread,
Sep 17, 2013, 6:43:12 PM9/17/13
to dropwiz...@googlegroups.com
I highly recommend reading Java Concurrency In Practice.
--
Coda Hale
http://codahale.com

Jerry Carter

unread,
Sep 17, 2013, 5:53:44 PM9/17/13
to dropwiz...@googlegroups.com
On Sep 17, 2013, at 5:24 PM, Jesse Lancaster <jessela...@gmail.com> wrote:
But does this imply that I need to have a lock around each method body in my resources? If it uses the same instance of the class it would imply that the following is not safe to use:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> getStuff() throws Exception {

        List<String> stuff = new ArrayList<String>();

        stuff.add("SomeStuff");
        stuff.add("SomeStuff2");
        stuff.add("SomeStuff3");

        return stuff;
    }

Is that the case? If not how is the instance of the class shared without sharing the instances' methods?

This example would be fine.  The local instance is being returned.

More problematic is code which modifies a shared instance.  As in

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<String> modifyList(String newElement) throws Exception {
this.listOfElements.add(newElement);
return this.listOfElements;
}

Here you would want to protect access to the listOfElements.

-=- Jerry

Jesse Lancaster

unread,
Sep 17, 2013, 7:51:31 PM9/17/13
to dropwiz...@googlegroups.com
Thanks Jerry. I appreciate you actually answering my question as opposed to just telling me to read a book.

Tatu Saloranta

unread,
Sep 18, 2013, 11:36:42 AM9/18/13
to dropwiz...@googlegroups.com
Your question was rather indicative of lack of understanding of what thread-safety means. I couldn't for life of me understand why you would think that the example would need any form of locking ("If not how is the instance of the class shared without sharing the instances' methods?").

So I took the answer as a "teach a man to fish" style answer, as opposed to handing fish & chips.

-+ Tatu +-



Jerry Carter

unread,
Sep 23, 2013, 9:29:50 PM9/23/13
to dropwiz...@googlegroups.com
FYI, I noticed that Java Concurrency in Practice is available as a free PDF download.

-=- Jerry

Reply all
Reply to author
Forward
0 new messages