code: public static Object sync(Context cx, Scriptable thisObj, Object[] args,
                                Function funObj)
      {
          if (args.length >= 1 && args.length <= 2 && args[0] instanceof Function) {
              Object syncObject = null;
              if (args.length == 2 && args[1] != Undefined.instance) {
                  syncObject = args[1];
              }
              return new Synchronizer((Function)args[0], syncObject);
          }
          else {
              throw reportRuntimeError("msg.sync.args");
          }
      }
docs: The sync function creates a synchronized function (in the sense
  of a Java synchronized method) from an existing function. The
  new function synchronizes on the the second argument if it is
  defined, or otherwise the <code>this</code> object of
  its invocation.
  js&gt; var o = { f : sync(function(x) {
  print("entry");
  Packages.java.lang.Thread.sleep(x*1000);
  print("exit");
  })};
  js&gt; spawn(function() {o.f(5);});
  Thread[Thread-0,5,main]
  entry
  js&gt; spawn(function() {o.f(5);});
  Thread[Thread-1,5,main]
  js&gt;
  exit
  entry
  exit