为什么node.js需要python

2024-05-16 00:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我从诺德开始 来自node.js README.md

先决条件(仅限Unix):

* GCC 4.2 or newer
* Python 2.6 or 2.7
* GNU Make 3.81 or newer
* libexecinfo (FreeBSD and OpenBSD only)

想知道node.js为什么需要Python吗? 它在API下面使用Python吗


Tags: orandgnunodemake先决条件jsunix
2条回答

实际上原因是多线程使用了不同的cpu内核,所以同一个变量在不同的cpu缓存中保持不同的值,您可以将计数设置为volatile来解决这个问题。如果您对volatile感兴趣,可以查看posthttp://tutorials.jenkov.com/java-concurrency/volatile.html。代码是

package com.test;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 *
 */
public class TestList {

    public static class TestScheduler {
        private static ScheduledExecutorService ses;
        private static volatile int count;

        public TestScheduler() {
            ses = Executors.newScheduledThreadPool(2);
            count = 0;
        }

        public void startScheduler() throws InterruptedException {
            System.out.println("startScheduler() thread: " + Thread.currentThread().getName());

            Runnable testTask = () -> {
                System.out.println(Thread.currentThread().getName() + ": count " + count++);
            };

            System.out.println("Starting test scheduler for 10s");
            ScheduledFuture<?> scheduledFuture = ses.scheduleWithFixedDelay(testTask, 5, 1, TimeUnit.SECONDS);
            System.out.println("ScheduledFuture started...");

            while (true) {
                // if any of the 2 lines below are uncommented, it works as I'd expect it to...
                // Thread.sleep(1000);
                // System.out.println(Thread.currentThread().getName() + ": count " + count);
                if (count > 5) {

                    System.out.println(Thread.currentThread().getName() + ": Cancelling scheduled task.");
                    scheduledFuture.cancel(true);
                    break;
                }
            }
            System.out.println("Ending test scheduler");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestScheduler test = new TestScheduler();
        test.startScheduler();
    }

}

它的发生是由于访问countrace conditions引起的
2个线程同时访问此变量,没有任何锁
您可以使用AtomicInteger来克服这一问题:

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Driver {
  public static void main(String[] args) throws InterruptedException {
    TestScheduler test = new TestScheduler();
    test.startScheduler();
  }
}


class TestScheduler {
  private ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);
  private AtomicInteger count = new AtomicInteger(0);

  public void startScheduler() throws InterruptedException {
    System.out.println("startScheduler() thread: " + Thread.currentThread().getName());

    Runnable testTask = () -> {
      System.out.println(Thread.currentThread().getName() + ": count " + count.getAndIncrement());
    };

    System.out.println("Starting test scheduler for 10s");
    ScheduledFuture<?> scheduledFuture = ses.scheduleAtFixedRate(testTask, 5, 1, TimeUnit.SECONDS);
    System.out.println("ScheduledFuture started...");

    while(true){
      if (count.get() > 5){
        System.out.println(Thread.currentThread().getName() + ": Cancelling scheduled task.");
        scheduledFuture.cancel(true);
        break;
      }
    }
    System.out.println("Ending test scheduler");
  }
}

相关问题 更多 >