How to write this java program using javascript and Node?

Asked

Viewed 67 times

3

public class Principal {

private static int x = 0;
private static int y = 0;

public static void sum() {
    x = y + 1;
    y = x + 1;
}

public static void main(String[] args) {

    for (int i = 1; i <= 10; i++) {

        Thread a = new Thread() {
            public void run() {
                sum();
            }
        };
        a.start();

        Thread b = new Thread() {
            public void run() {
                sum();
            }
        };
        b.start();

        System.out.println(x);
    }
  }
}

How can I write this algorithm using Node and javascript? I just don’t have a clue how to do it! I was analyzing some shapes and I found the Web Worker but I couldn’t do anything. What should I do? I don’t know how to work Node threads, I tried using this example Node-threads-a-Gogo but it seems that it does not run in windows.

  • I think this should help to understand and simulate some situations similar to Thread in Javascript http://answall.com/a/100657/3635, understand that Javascript works only with a single thread (although eventually async), look for the written part Example with web Workers

  • Possible duplicate of How to release frozen/locked thread?

  • @Guilherme , thanks . I had already read this example ! But the problem is that I still have no code in javascript!!!

  • I don’t think I understand, you don’t have or know anything about javascript?

  • Related: http://answall.com/a/45721/3635

1 answer

4


Javascript has no threads. However you can run asynchronous methods. Example below:

var x = 0;
var y = 0;

function sum() {
  x = y + 1;
  y = x + 1;
}

function main() {

  for (i = 1; i <= 10; i++) {
    var a = setTimeout(function run(){ sum() }, 0);
    var b = setTimeout(function run(){ sum() }, 0);
  }

  console.log(x);

  // Complementar: Valor de X depois de 1 segundo.
  setTimeout(function(){ console.log(x); }, 1000);

}

main();

The result will be:

0
39

This is because asynchronous methods are declared and prepared for execution - but Scheduler does not invoke them until after the first console.log(x);. The second console call is there to show the result after a second.

Javascript has some native methods that provide asynchronous mechanisms. Among them:

  • setInterval
  • setTimeout
  • requestAnimationFrame
  • Xmlhttprequest
  • Websocket
  • Worker
  • Thanks man! Now I’ll learn a little about javascript starting in style!! So to run the above algorithm I can do : Node suaostai.js ?

  • I would only remove the anomimous callbacks to call the direct sum :) ... +1

  • @Guilhermenascimento I tried to leave as close as possible to the original code. But - good tip, let me adjust... [Edit] ready. =)

  • @Penapintada Always a pleasure to help! Try and tell me if it worked. =)

  • @Onosendai The way you answered, also ran Ode without problems! Thank you very much

  • I don’t think that question is a duplicate

  • @Penapintada maybe not this one Linkei specifies, but the question of Threads in Js has already been asked yes, is that in the answer I focused the webworkers, which seemed to be its initial need, Anyway the problem was more you do not know the least language what is a basic requirement to do something

  • @I understood and I agree with you Guilherme , I promise that as soon as I finish this semester here in college I will dedicate myself to the fabulous javascript language to help the class around here, as you helped me !!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.