2

In my GWT client-side code, I have a class as follow

public class SomeClass {

    private Map<Integer, Serializable> mId2ContentMap;
    private Map<Integer, Timestamp> mId2Timestamp;

    public SomeClass() {
        mId2ContentMap = Collections.synchronizedMap(new HashMap<Integer, Serializable>());
        mId2Timestamp = Collections.synchronizedMap(new HashMap<Integer, Timestamp>());
    }
}

When I try to run my GWT web application, I got two errors saying that

The method synchronizedMap(HashMap<Integer,Serializable>) is undefined for the type Collections
The method synchronizedMap(HashMap<Integer,Timestamp>) is undefined for the type Collections

After googled for a while, I only found one post which is remotely relevant to the errors I encountered. That post mentioned that GWT doesn't support reflective calls. But I don't think that Collections.synchronizedMap() is a reflective call. Correct me if I'm wrong here.

So any suggestion?

Community
  • 1
  • 1
dydigging
  • 167
  • 9
  • Why are you trying to use synchronized collection in GWT client code? GWT application is converted to Javascript and JavaScript doesn't support multithreading. Otherwise GWT has limited JDK [compatibility](http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsCompatibility.html) – xRomZak Feb 06 '15 at 20:14
  • @xRomZak thanks, your comment and reference help. – dydigging Feb 06 '15 at 20:53

1 Answers1

1

synchronizedMap is not one of the methods within java.util.Collections that is emulated in the GWT JRE, as you can see here.

That said, would it be possible/convenient for you to just remove the call to synchronizedMap from your client code (and just use the HashMap that you're passing in directly)? I am not very knowledgeable about GWT, but considering that client code is translated into javascript, I can't see what the benefit of using Java synchronization would be, anyway.

wdf
  • 171
  • 5
  • You're right about synchronizedMap is not emulated in GWT JRE. Moreover, I found this ["while GWT silently accepts the synchronized keyword, it has no real effect."](http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsCompatibility.html) – dydigging Feb 06 '15 at 21:10
  • I need to admit that I have little knowledge about Javascript. But after a little bit research I'm under the impression that [it is not completely safe to assume Javascript is always single-threaded](http://stackoverflow.com/a/2734311/4535217). Will you enlighten me on this? – dydigging Feb 06 '15 at 21:17
  • Which makes sense -- synchronization constructs aren't particularly useful in a single-threaded execution environment. So is there anything keeping you from just dropping the usage of synchronizedMap? – wdf Feb 06 '15 at 21:17
  • Well, the answer in the question you linked really only applies "if you're writing complex applications, especially cross-window/frame-scripting ones" (as the author of that post wrote). However, AFAIK, you really don't need to worry about the kinds of synchronization issues that Java's synchronized collections are designed to address, i.e. as concurrently modifying an object's properties. – wdf Feb 06 '15 at 21:26