npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

concurrent-thread-js

v1.0.2

Published

为单线程的JavaScript实现多线程并发的功能,语意上参考Java实现,提供getState/sleep/join等API,并提供线程间通信的功能,依赖ES6语法,基于Promise和Async函数实现,故需要Babel编译才能运行

Downloads

5

Readme

concurrent-thread.js

为单线程的JavaScript实现多线程并发的功能,语意上参考Java实现,提供getState/sleep/join等API,并提供线程间通信的功能,依赖ES6语法,基于Promise和Async函数实现,故需要Babel编译才能运行。JavaScrpt本来就是单线程的,所以这只是在API的层面实现了模拟,在下文的介绍中,每条所谓的线程其实就是普通的异步函数,并在此基础上实现不同线程的协调配合

When to use

当工程需要让两个函数在执行上不互相干扰,同时也不希望它们会阻塞主线程,与此同时,这两个函数还需要实现类似并发多线程之间的协调需求的时候,你可以使用这个并发模拟库,~~实际上这种应用场景很少(扎心了呀)~~。

API

  • submit(function,[namespace]): 接收一个函数,普通函数或Async函数均可,并异步执行,线程
  • sleep(ms): 线程休眠,可指定休眠时间ms,以毫秒计算
  • join(threadName): 线程同步,调用此方法的线程函数将在threadName执行结束后继续执行
  • interupt(threadName): 线程中断,影响线程内部调this.isInterrupted()的返回值
  • Lock.lock: 加锁,一个时刻只能有一个线程函数进入临界区,其他线程函数需要等待,锁是非公平的,也就是说后面排队的线程函数没有先后,以随机的方式进行竞争。
  • Lock.unlock:解除非公平锁
  • Condition.wait:不具备执行条件,线程进入waiting状态,等待被唤醒
  • Condition.notify:随机唤醒一个wait的线程
  • Condition.notifyAll: ~~尚未编写~~,唤醒所有wait的线程
  • getState: ~~尚未编写~~ 获取线程状态,包括RUNNALE(运行),WAITING(等待),BLOCKED(阻塞),TERMINATED(终止)

Usage

submit

线程休眠

submit(async function example() {
    await this.sleep(3000);
    console.log(1);
});
// 3秒后输出 1

interupt

线程中断

submit(async function example() {
    console.log(this.isInterrupted());
    this.interrupt();
    console.log(this.isInterrupted());
});
// 输出 false 
// 输出 true

getState

获取线程状态

submit(async function example() {
    console.log(this.getState());
});
// 输出 RUNNABLE

Lock

非公平锁

  • Lock.lock: 加锁
  • Lock.unlock: 解锁
async function commonCode() {
    await Lock.lock();
    await Executor.sleep(3000);
    Lock.unLock();
}

submit(async function example1() {
    console.log('example1 start')
    await commonCode();
    console.log('example1 end')
});
submit(async function example2() {
    console.log('example2 start')
    await commonCode();
    console.log('example2 end')
});
// 立即输出
example1 start
example2 start
// 3秒后输出
example1 end
// 再3秒后输出
example2 end

Condition

条件对象

  • Condition.wait:不具备执行条件,线程进入waiting状态,等待被唤醒
  • Condition.notify: 唤醒线程
async function testCode() {
    console.log('i will be wait');
    if (true) {
        await Condition.wait();
    };
    console.log('i was notified ');
}

submit(async function example() {
    testCode();
    setTimeout(() => {
        Condition.notify();
    }, 3000);
});
i will be wait
// 3秒后输出
i was notified

dataMap/dataList

线程通信状态对象,分别为对象和数组