Dan
Dan

Reputation: 57881

JavaScript: asynchronous but not multithread?

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...)


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>

Upvotes: 3

Views: 4149

Answers (3)

Van Coding
Van Coding

Reputation: 24534

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!

Upvotes: 0

Eli
Eli

Reputation: 17825

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);

Upvotes: 0

Eric
Eric

Reputation: 97571

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.

Upvotes: 4

Related Questions