3

I have the following:

function NumberFormatter(){
   ...
   function helper(){
   ...
       var tempvar;
   ...
   }
   function format(num){
   ...
       helper()
   ...
   }
}

//there is a single instance of FT

var FT = new NumberFormatter()
FT.format(123)

The state of the object is not changed.

In a multithreading context, does this code fail if format() is called from two different places almost simultaniously?

Is there a simple way to lock the object or is it better to hold 1000 instances?


Answer: (summarizing all posted here...)

  • No, the code won't fail (because there is no real multithreading in JavaScript)

  • No way to lock an object and you don't need to

  • You can have one instance. No need to create 1000 instances


For those who don't believe, the proofcode:

<script type='text/javascript'>

    function get_random_color() {
        var letters = '0123456789ABCDEF'.split('');
        var color = '#';
        for (var i = 0; i < 6; i++ ) {
            color += letters[Math.round(Math.random() * 15)];
        }
        return color;
    }

    setInterval('document.getElementById("a").style.backgroundColor=get_random_color()', 10)

    //***  setInterval() stops when heavy calculations are done   ***//

    document.getElementById("b").onclick = function(){
        for(var i=0;i<10000000; i++){
            Math.atan2(Math.round(1000))
        }
    }
</script>

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
Dan
  • 55,715
  • 40
  • 116
  • 154

3 Answers3

4

Two things. Firstly, the only time you need to worry about concurrency issues is when you're handling external resources, or changing the state of the object. Since calling formatNum does not change the state of NumberFormatter, there is absolutely nothing to worry about.

Secondly, javascript doesn't do multi threading. So it's a moot point.

Community
  • 1
  • 1
Eric
  • 95,302
  • 53
  • 242
  • 374
  • The state of the object is not changed. The only thing I wanted to know is: can `format()` method be executed again when it has not finished running yet? – Dan Apr 14 '11 at 13:50
  • @Dan: No. Its not possible for that to happen, as *no other code can execute* until the current line of execution stops. Javascript doesn't multithread. – Eric Apr 15 '11 at 07:35
0

I think you should check your code. The var FT is undefined because NumberFormatter does not return a value. format() will throw an exception.

For single instances, why not just make something like this:

var FT = new function (){
   this.helper = function (x){
   }
   this.format = function (num){
   }
}

FT.format(123);

One more thing: JavaScript does not use multiple Threads nor can there be code executed at the same time. All code in JavaScript is queued in a very intelligent way.

A simple example:

setTimeout(function(){
   alert("hi");
},0);
while(true);

You never will get the "hi" message. All code must be executed first before other code can be executed! JavaScript will also not pause the execution of code to run other code and then resume the first code. Sorry for my bad explanation!

Van Coding
  • 24,244
  • 24
  • 88
  • 132
  • Because I wanted to create a single instance of an object that formats integers into some string representation – Dan Apr 14 '11 at 13:43
  • `nor can there be code executed at the same time` - thanks a lot! – Dan Apr 14 '11 at 14:10
0

JavaScript is an asynchronous language and always runs on a single thread. Douglas Crockford has a great slideshow of JavaScript synchronicity:

http://www.slideshare.net/douglascrockford/crockford-on-javascript-scene-6-loopage

Since JavaScript operates on a single thread, it is practically impossible for the same function to be called twice simultaneously. Your code should be fine.

Also, if you want a single function to help you format, how about returning an object which has the public and private methods? This will help you get closer to a Singleton pattern:

var NumberFormatter = (function() {

    // this method is private
    var helper = function(x) {

    };

    // all methods in here are public
    return {
        format: function(num) {
            helper();
        }
    };
})();

var FT = NumberFormatter.format(123);
Eli
  • 17,397
  • 4
  • 36
  • 49