2024-05-16 00:38:32 发布
网友
我从诺德开始 来自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吗
实际上原因是多线程使用了不同的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(); } }
它的发生是由于访问count时race conditions引起的 2个线程同时访问此变量,没有任何锁 您可以使用AtomicInteger来克服这一问题:
count
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"); } }
实际上原因是多线程使用了不同的cpu内核,所以同一个变量在不同的cpu缓存中保持不同的值,您可以将计数设置为volatile来解决这个问题。如果您对volatile感兴趣,可以查看posthttp://tutorials.jenkov.com/java-concurrency/volatile.html。代码是
它的发生是由于访问
count
时race conditions引起的2个线程同时访问此变量,没有任何锁
您可以使用
AtomicInteger
来克服这一问题:相关问题 更多 >
编程相关推荐