www.webdeveloper.com
Results 1 to 7 of 7

Thread: threading in javascript

  1. #1
    Join Date
    Jan 2008
    Posts
    273

    threading in javascript

    Hi All

    I always thought that javascript was threaded, but the following experiment doesn't confirm this:
    Code:
    function test(x) {
      for (var i = 0; i < 3; i++) {
        alert(x); //console.log(x) ;
      }
    }
    
    setTimeout(function(){
      test(1) 
    }, 1);
    setTimeout(function(){
      test(2)
    }, 1);
    It always prints first the 1's and then the 2's (not 1, 2, 2, 1, 1, 2 for example)
    So, can javascript execute two or more thread at the same time ?

    For example, if you're loading 10 files using async AJAX, which all call the same callback function, AND you only want to call ready when all 10 files are loaded:
    Code:
    var glob_count = 0 ;
    function callback() {
      glob_count++ ;
      if ( glob_count == 10 )
        ready() ; // should only be called once
    }
    As far as I know (from java) this code is not thread save, but considering the first experiment, I guess that it is ok in javascript, right ?

    cheers
    LuCa

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jeanluca View Post
    Code:
    function test(x) {
      for (var i = 0; i < 3; i++) {
        alert(x); //console.log(x) ;
      }
    }
    
    setTimeout(function(){
      test(1) 
    }, 1);
    setTimeout(function(){
      test(2)
    }, 1);
    It always prints first the 1's and then the 2's (not 1, 2, 2, 1, 1, 2 for example)
    So, can javascript execute two or more thread at the same time ?
    a few hitches:
    you can't "sleep" in javascript. i've found a hack for firefox, but it's not pretty.

    the number you pass to test is
    - passed byRef, so the orig won't be clobered
    - private as soon as "x" is substituted for it as the argument name in test(x)

    since window.alert is modal, it halts all script execution until the alert is closed.
    once a for loop is locked-in, it going to be next-to-impossible for something else to execute until the loop completes.

    since x is private if can never change, and the loop can't be interrupted.

    i suppose it's also possible that if you set a lager, yet identical timeout for both batches, you might see the 2s before the 1s.
    some brosers have finer granualrity than others,
    all throw away setTimeout under 10.
    ie rounded from 30 to 0.

    ie lives in ~16ms heartbeats, chrome's is ~11ms.
    it seems like firefox's is about 13ms, but that's anecdotal.

    if you defer each alert inside a the loop by a few hundred ms, and alert a global instead of a local, you might find different results.





    HTML5 defines asynchronous threads that run in a limited javascript environment. It doesn't have the full dom, but it's real javascript.
    you can set several (unlimited?) background worker threads, much like setTimeout.
    these threads can terminate themselves and raise an event, passing the fruits of their labors back to the main JavaScript environment.

    most of the examples ive seen were things like progressively transforming a canvas image, but I'm sure that we will find other uses once they take off.

  4. #4
    Join Date
    Jan 2008
    Posts
    273
    Its not important for me if the 2's come first, because of whatever reason. I was wondering if two threads can run simultaneously. From your answer and the other one I have to conclude that this is not happening.

    Furthermore, I don't thin x is passed by reference, consider the following

    Code:
    function test(x) {
      x = 20 ;
    }
    
    var y = 10 ;
    test(y) ;
    alert(y) ; // alerts 10
    cheers

  5. #5
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    The newest browsers have web workers: https://developer.mozilla.org/En/Using_web_workers

    Eric

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jeanluca View Post
    Its not important for me if the 2's come first, because of whatever reason. I was wondering if two threads can run simultaneously. From your answer and the other one I have to conclude that this is not happening.

    Furthermore, I don't thin x is passed by reference, consider the following

    Code:
    function test(x) {
      x = 20 ;
    }
    
    var y = 10 ;
    test(y) ;
    alert(y) ; // alerts 10
    cheers
    whoops, yes, you are correct.
    i meant to say that prmitive arguments are passed byVal.
    i was thinking byVal, and explaining byVal, but i typed byRef.
    that's a heck of a mistake on my part, i apologize.

    do keep in mind the following byRef behavior:
    Code:
    function test(x) {
      x[0] = 20 ;
    }
    
    var y = [10] ;
    alert(y) ; // alerts 10 
    test(y) ;
    alert(y) ; // alerts 20

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jeanluca View Post
    Its not important for me if the 2's come first, because of whatever reason. I was wondering if two threads can run simultaneously. From your answer and the other one I have to conclude that this is not happening.
    each stack has the conch while it executes.
    some browsers may provide other tabs/popups/iframes to run sepereatly.
    i know chrome has different threads for each tab.
    using dom messages, or even just the dom, you could run different chunks of your code on different tabs/popups/iframes...

    i've demonstrated distributed computing using javascript, and if that's possible, than multithreading has gotta be possible...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles