使用Scala实现一个并发库(NonBlocking版本)

M24C04-R EEPROM与R7FA4M3AF3CFB144 MCU的嵌入式存储方案 在嵌入式系统开发中,非易失性存储技术是确保数据持久化的关键。EEPROM作为一种经典的存储介质,以其百万次擦写周期和长达40年的数据保持能力,成为工业级应用的理想选择。通过I2C总线协议,EEPROM可以与微控制器高效通信,实现数据的可靠存储与读取。本文以M24C04-R EEPROM与瑞萨R7FA4M3AF3CFB144 MCU的组合为例,详细解析了硬件连接、软件驱动及高级应用技巧。特别是在工业环境中,通过CRC8校验和磨损均衡策略,显著提升了数据可靠性和存储寿命。这些实践不仅适用于温控器、智能电表等场景 阅读详情

这个例子来源于scala圣经级教程《Functional Programming in Scala》,由于本人跟着书中的代码敲了一遍,然后写了点测试代码验证了一下正确性,所以就放在这做个备忘吧。贴出来只是为了方便自己,如果看不懂,但是又感兴趣的就去看原书吧……

> 注:本文是上一篇文章《使用Scala实现一个并发库(阻塞版本, 下一篇文章提供NonBlocking版本)》的延续

package parallelism

import java.util.concurrent._
import java.util.concurrent.atomic.AtomicReference

import parallelism.Nonblocking.Future

import language.implicitConversions

object Nonblocking {

  trait Future[+A] {
    private[parallelism] def apply(k: A => Unit): Unit
  }

  type Par[+A] = ExecutorService => Future[A]

  object Par {

    def run[A](es: ExecutorService)(p: Par[A]): A = {
      // A mutable, threadsafe reference, to use for storing the result
      val ref = new java.util.concurrent.atomic.AtomicReference[A]
      // A latch which, when decremented, implies that `ref` has the result
      val latch = new CountDownLatch(1)
      // Asynchronously set the result, and decrement the latch
      p(es) { a => ref.set(a); latch.countDown }
      // Block until the `latch.countDown` is invoked asynchronously
      latch.await
      // Once we've passed the latch, we know `ref` has been set, and return its value
      ref.get
    }

    def unit[A](a: A): Par[A] =
      es => new Future[A] {
        def apply(cb: A => Unit): Unit =
          cb(a)
      }

    /** A non-strict version of `unit` */
    def delay[A](a: => A): Par[A] =
      es => new Future[A] {
        def apply(cb: A => Unit): Unit =
          cb(a)
      }

    def fork[A](a: => Par[A]): Par[A] =
      es => new Future[A] {
        def apply(cb: A => Unit): Unit =
          eval(es)(a(es)(cb))
      }

    /**
      * Helper function for constructing `Par` values out of calls to non-blocking continuation-passing-style APIs.
      * This will come in handy in Chapter 13.
      */
    def async[A](f: (A => Unit) => Unit): Par[A] = es => new Future[A] {
      def apply(k: A => Unit) = f(k)
    }

    /**
      * Helper function, for evaluating an action
      * asynchronously, using the given `ExecutorService`.
      */
    def eval(es: ExecutorService)(r: => Unit): Unit =
      es.submit(new Callable[Unit] {
        def call = r
      })

    def map2[A, B, C](p: Par[A], p2: Par[B])(f: (A, B) => C): Par[C] =
      es => new Future[C] {
        def apply(cb: C => Unit): Unit = {
          var ar: Option[A] = None
          var br: Option[B] = None
          // this implementation is a little too liberal in forking of threads -
          // it forks a new logical thread for the actor and for stack-safety,
          // forks evaluation of the callback `cb`
          val combiner = Actor[Either[A, B]](es) {
            case Left(a) =>
              if (br.isDefined) eval(es)(cb(f(a, br.get)))
              else ar = Some(a)
            case Right(b) =>
              if (ar.isDefined) eval(es)(cb(f(ar.get, b)))
              else br = Some(b)
          }
          p(es)(a => combiner ! Left(a))
          p2(es)(b => combiner ! Right(b))
        }
      }

    // specialized version of `map`
    def map[A, B](p: Par[A])(f: A => B): Par[B] =
      es => new Future[B] {
        def apply(cb: B => Unit): Unit =
          p(es)(a => eval(es) {
            cb(f(a))
          })
      }

    def lazyUnit[A](a: => A): Par[A] =
      fork(unit(a))

    def asyncF[A, B](f: A => B): A => Par[B] =
      a => lazyUnit(f(a))

    def sequenceRight[A](as: List[Par[A]]): Par[List[A]] =
      as match {
        case Nil => unit(Nil)
        case h :: t => map2(h, fork(sequence(t)))(_ :: _)
      }

    def sequenceBalanced[A](as: IndexedSeq[Par[A]]): Par[IndexedSeq[A]] = fork {
      if (as.isEmpty) unit(Vector())
      else if (as.length == 1) map(as.head)(a => Vector(a))
      else {
        val (l, r) = as.splitAt(as.length / 2)
        map2(sequenceBalanced(l), sequenceBalanced(r))(_ ++ _)
      }
    }

    def sequence[A](as: List[Par[A]]): Par[List[A]] =
      map(sequenceBalanced(as.toIndexedSeq))(_.toList)

    def parMap[A, B](as: List[A])(f: A => B): Par[List[B]] =
      sequence(as.map(asyncF(f)))

    def parMap[A, B](as: IndexedSeq[A])(f: A => B): Par[IndexedSeq[B]] =
      sequenceBalanced(as.map(asyncF(f)))

    // exercise answers

    /*
     * We can implement `choice` as a new primitive.
     *
     * `p(es)(result => ...)` for some `ExecutorService`, `es`, and
     * some `Par`, `p`, is the idiom for running `p`, and registering
     * a callback to be invoked when its result is available. The
     * result will be bound to `result` in the function passed to
     * `p(es)`.
     *
     * If you find this code difficult to follow, you may want to
     * write down the type of each subexpression and follow the types
     * through the implementation. What is the type of `p(es)`? What
     * about `t(es)`? What about `t(es)(cb)`?
     */
    def choice[A](p: Par[Boolean])(t: Par[A], f: Par[A]): Par[A] =
      es => new Future[A] {
        def apply(cb: A => Unit): Unit =
          p(es) { b =>
            if (b) eval(es) {
              t(es)(cb)
            }
            else eval(es) {
              f(es)(cb)
            }
          }
      }

    /* The code here is very similar. */
    def choiceN[A](p: Par[Int])(ps: List[Par[A]]): Par[A] =
      es => new Future[A] {
        def apply(cb: A => Unit): Unit =
          p(es) { ind =>
            eval(es) {
              ps(ind)(es)(cb)
            }
          }
      }

    def choiceViaChoiceN[A](a: Par[Boolean])(ifTrue: Par[A], ifFalse: Par[A]): Par[A] =
      choiceN(map(a)(b => if (b) 0 else 1))(List(ifTrue, ifFalse))

    def choiceMap[K, V](p: Par[K])(ps: Map[K, Par[V]]): Par[V] =
      es => new Future[V] {
        def apply(cb: V => Unit): Unit =
          p(es)(k => ps(k)(es)(cb))
      }

    /* `chooser` is usually called `flatMap` or `bind`. */
    def chooser[A, B](p: Par[A])(f: A => Par[B]): Par[B] =
      flatMap(p)(f)

    def flatMap[A, B](p: Par[A])(f: A => Par[B]): Par[B] =
      es => new Future[B] {
        def apply(cb: B => Unit): Unit =
          p(es)(a => f(a)(es)(cb))
      }

    def choiceViaFlatMap[A](p: Par[Boolean])(f: Par[A], t: Par[A]): Par[A] =
      flatMap(p)(b => if (b) t else f)

    def choiceNViaFlatMap[A](p: Par[Int])(choices: List[Par[A]]): Par[A] =
      flatMap(p)(i => choices(i))

    def join[A](p: Par[Par[A]]): Par[A] =
      es => new Future[A] {
        def apply(cb: A => Unit): Unit =
          p(es)(p2 => eval(es) {
            p2(es)(cb)
          })
      }

    def joinViaFlatMap[A](a: Par[Par[A]]): Par[A] =
      flatMap(a)(x => x)

    def flatMapViaJoin[A, B](p: Par[A])(f: A => Par[B]): Par[B] =
      join(map(p)(f))

    /* Gives us infix syntax for `Par`. */
    implicit def toParOps[A](p: Par[A]): ParOps[A] = new ParOps(p)

    // infix versions of `map`, `map2` and `flatMap`
    class ParOps[A](p: Par[A]) {
      def map[B](f: A => B): Par[B] = Par.map(p)(f)

      def map2[B, C](b: Par[B])(f: (A, B) => C): Par[C] = Par.map2(p, b)(f)

      def flatMap[B](f: A => Par[B]): Par[B] = Par.flatMap(p)(f)

      def zip[B](b: Par[B]): Par[(A, B)] = p.map2(b)((_, _))
    }

  }


  def main(args: Array[String]): Unit = {

    // _print for dubug purpose
    val _print = (flag: Boolean) => if (flag) println(": " + Thread.currentThread()) else println(Thread.currentThread())

    val es = Executors.newFixedThreadPool(3)

    val intDelayPar = Par.delay[Int]({
      _print(false);
      math.pow(999, 67).toInt
    });

    intDelayPar(es).apply((a: Int) => {
      _print(true);
      println(a)
    })

    intDelayPar(es)((a: Int) => {
      _print(true);
      println(a)
    })

    Par.fork(intDelayPar)(es)((a: Int) => {
      _print(true);
      println(a)
    })


    Thread.sleep(6000L)
    println("**********************************************")

    Par.run(es)(intDelayPar)

    Thread.sleep(6000L)
    println("**********************************************")


    val _intDelayPar = Par.delay[Int]({
      val future = es.submit(new Callable[Int] {
        _print(true);
        override def call = math.pow(999, 67).toInt
      })
      val res = future.get
      println(res)
      res
    });
    Par.run(es)(_intDelayPar)

    println("-------------------------------------------")

    import scala.language.implicitConversions
    import Par.toParOps

    // 通过隐式转换将 map 方法变为中缀操作方式
    (Par.delay(1) map {(a: Int) => 111 * a})(es)(println(_))
    (Par.delay(2) map {(a: Int) => 111 * a})(es)(println(_))
    (Par.delay(3) map {(a: Int) => 111 * a})(es)(println(_))

    es.shutdown()
  }

}

上述代码的运行结果是:


Thread[main,5,main]
: Thread[main,5,main]
2147483647
Thread[main,5,main]
: Thread[main,5,main]
2147483647
Thread[pool-1-thread-1,5,main]
: Thread[pool-1-thread-1,5,main]
2147483647
**********************************************
Thread[main,5,main]
**********************************************
: Thread[main,5,main]
2147483647
-------------------------------------------
111
222
333

上述代码用到的Actor源码是:

package parallelism

import java.util.concurrent.atomic.{AtomicInteger, AtomicReference}
import java.util.concurrent.{Callable,ExecutorService}
import annotation.tailrec

/*
 * Implementation is taken from `scalaz` library, with only minor changes. See:
 *
 * https://github.com/scalaz/scalaz/blob/scalaz-seven/concurrent/src/main/scala/scalaz/concurrent/Actor.scala
 *
 * This code is copyright Andriy Plokhotnyuk, Runar Bjarnason, and other contributors,
 * and is licensed using 3-clause BSD, see LICENSE file at:
 *
 * https://github.com/scalaz/scalaz/blob/scalaz-seven/etc/LICENCE
 */

/**
 * Processes messages of type `A`, one at a time. Messages are submitted to
 * the actor with the method `!`. Processing is typically performed asynchronously,
 * this is controlled by the provided `strategy`.
 *
 * Memory consistency guarantee: when each message is processed by the `handler`, any memory that it
 * mutates is guaranteed to be visible by the `handler` when it processes the next message, even if
 * the `strategy` runs the invocations of `handler` on separate threads. This is achieved because
 * the `Actor` reads a volatile memory location before entering its event loop, and writes to the same
 * location before suspending.
 *
 * Implementation based on non-intrusive MPSC node-based queue, described by Dmitriy Vyukov:
 * [[http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue]]
 *
 * @see scalaz.concurrent.Promise for a use case.
 *
 * @param handler  The message handler
 * @param onError  Exception handler, called if the message handler throws any `Throwable`.
 * @param strategy Execution strategy, for example, a strategy that is backed by an `ExecutorService`
 * @tparam A       The type of messages accepted by this actor.
 */
final class Actor[A](strategy: Strategy)(handler: A => Unit, onError: Throwable => Unit = throw(_)) {
  self =>

  private val tail = new AtomicReference(new Node[A]())
  private val suspended = new AtomicInteger(1)
  private val head = new AtomicReference(tail.get)

  /** Alias for `apply` */
  def !(a: A) {
    val n = new Node(a)
    head.getAndSet(n).lazySet(n)
    trySchedule()
  }

  /** Pass the message `a` to the mailbox of this actor */
  def apply(a: A) {
    this ! a
  }

  def contramap[B](f: B => A): Actor[B] =
    new Actor[B](strategy)((b: B) => (this ! f(b)), onError)

  private def trySchedule() {
    if (suspended.compareAndSet(1, 0)) schedule()
  }

  private def schedule() {
    strategy(act())
  }

  private def act() {
    val t = tail.get
    val n = batchHandle(t, 1024)
    if (n ne t) {
      n.a = null.asInstanceOf[A]
      tail.lazySet(n)
      schedule()
    } else {
      suspended.set(1)
      if (n.get ne null) trySchedule()
    }
  }

  @tailrec
  private def batchHandle(t: Node[A], i: Int): Node[A] = {
    val n = t.get
    if (n ne null) {
      try {
        handler(n.a)
      } catch {
        case ex: Throwable => onError(ex)
      }
      if (i > 0) batchHandle(n, i - 1) else n
    } else t
  }
}

private class Node[A](var a: A = null.asInstanceOf[A]) extends AtomicReference[Node[A]]

object Actor {

  /** Create an `Actor` backed by the given `ExecutorService`. */
  def apply[A](es: ExecutorService)(handler: A => Unit, onError: Throwable => Unit = throw(_)): Actor[A] =
    new Actor(Strategy.fromExecutorService(es))(handler, onError)
}

/**
 * Provides a function for evaluating expressions, possibly asynchronously.
 * The `apply` function should typically begin evaluating its argument
 * immediately. The returned thunk can be used to block until the resulting `A`
 * is available.
 */
trait Strategy {
  def apply[A](a: => A): () => A
}

object Strategy {

  /**
   * We can create a `Strategy` from any `ExecutorService`. It's a little more
   * convenient than submitting `Callable` objects directly.
   */
  def fromExecutorService(es: ExecutorService): Strategy = new Strategy {
    def apply[A](a: => A): () => A = {
      val f = es.submit { new Callable[A] { def call = a} }
      () => f.get
    }
  }

  /**
   * A `Strategy` which begins executing its argument immediately in the calling thread.
   */
  def sequential: Strategy = new Strategy {
    def apply[A](a: => A): () => A = {
      val r = a
      () => r
    }
  }
}
879675643@qq.com  lhever

.---.                                                                         
|   |   .              __.....__   .----.     .----.   __.....__              
|   | .'|          .-''         '.  \    \   /    /.-''         '.            
|   |<  |         /     .-''"'-.  `. '   '. /'   //     .-''"'-.  `. .-,.--.  
|   | | |        /     /________\   \|    |'    //     /________\   \|  .-. | 
|   | | | .'''-. |                  ||    ||    ||                  || |  | | 
|   | | |/.'''. \\    .-------------''.   `'   .'\    .-------------'| |  | | 
|   | |  /    | | \    '-.____...---. \        /  \    '-.____...---.| |  '-  
|   | | |     | |  `.             .'   \      /    `.             .' | |      
'---' | |     | |    `''-...... -'      '----'       `''-...... -'   | |      
      | '.    | '.                                                   |_|      
      '---'   '---'  
scala并发编程开发教程 Spark 的RPC是通过Akka类库实现的,Akka用Scala语言开发,基于Actor并发模型实现 立即下载

相关推荐

立创电赛C题:基于STM32的三端口DC-DC变换器设计与MPPT算法实现

本文详细解析了2021年立创电赛C题——基于STM32的三端口DC-DC变换器设计。文章从题目要求出发,系统阐述了采用双DC-DC电路(分别负责MPPT追踪与稳压)的硬件架构,并重点讲解了以STM32为核心、结合PID控制与扰动观察法(MPPT算法)的软件实现方案,为备赛者提供了完整的软硬件设计思路与调试经验。

weixin_42588877的博客 233

scala并发_探索Scala并发

2003年,Herb Sutter在他的“免费午餐结束了”一文中揭露了业界最大的“肮脏小秘密”,清楚地表明,越来越快的处理器时代已经结束,将由一个新的并行化时代取代。单个芯片上的“核心”(虚拟CPU)。 这个消息在编程界引起了震惊,因为正确地保持线程安全的代码一直都存在,从理论上讲,即使不是在实践中,高能力的软件开发人员也对您的公司而言过于昂贵。 似乎很少有特权的人对Java的线程模型和并发A...

cusi77914的博客 758

半导体制造技术导论(第二版)萧宏 第五章集成电路加热工艺答案

第二次退火是在较高温度下进行,并将电阻系数较高的c49碳化硅转变为电阻系数较低的c54碳。水平式高温炉加热、垂直式高温炉加热(即传统的管式炉加热)、快速加热工艺RTP、激光镭射。8. 集成电路芯片制造过程中,会使用垫底氧化层、阻挡氧化层、栅氧氧化层、屏蔽氧化层和全。氧化是最重要的加热过程之一,是一种添加工艺,将氧气加入到硅晶圆后,在晶圆表面形成二氧。为了满足元器件性能的要求,必须利用退火工艺将晶格的损伤修复,使其恢复单晶结构并激活掺。工艺氮气气流下将晶舟推入炉管,升高温度,稳定温度;

goal_achieving的博客 1891

Scala中如何基于ZIO构建可组合的程序(命令行)

设计一个命令行程序 大多数命令行程序都是无状态的,这是理所当然的,因为它们可以很容易地集成到脚本中并通过shell管道链接。然而,对于本文,我们需要一个稍微复杂一点的程序。 让我们写一个SQL命令行程序。用户将通过文本命令与之交互,根据不同的SQL命令创建不同的程序命令以输出不同的字符,同时我们还希望可以循环输入。 对于这些问题中的每一个,我们将创建一个独立的模块,该模块依赖于其他模块,如下所示: 基本程序 ZIO 应用程序的基本构建块是ZIO[R, E, A]类型,它描述了有效的计算,其中: R 是运

梦境迷离 1077

2.实体机安装TrueNAS 方法

访问不了可以使用以下连接:提取码:0927。

Unofficial_user的博客 7470

【开源教程25】疯壳·开源编队无人机-定高、定点、悬停

【开源教程25】疯壳·开源编队无人机-定高、定点、悬停

efans_Mike的博客 1658

Scala学习笔记20: Future 和Promise

在 Scala 中, `Future` 和 `Promise` 是用于处理异步操作的强大工具 ; 它们就像一对搭档, 协同工作, 优雅地管理着那些需要花费时间的任务 .

Taerge0110的博客 1208

交换机背板带宽、交换容量与包转发率到底是啥?

在网络工程中,交换机是核心设备之一,而关于它的性能指标,背板带宽、交换容量、以及包转发率往往成为人们衡量其性能的重要参数。这些术语的定义、关系、以及在实际场景中的应用,都对交换机的选型与部署有着重要意义。本文将为您全面解读这些概念,帮助您深入了解交换机的核心性能指标。

网络技术联盟站 1561

Java并发包源码分析

  并发是一种能并行运行多个程序或并行运行一个程序中多个部分的能力。如果程序中一个耗时的任务能以异步或并行的方式运行,那么整个程序的吞吐量和可交互性将大大改善。现代的PC都有多个CPU或一个CPU中有多个核,是否能合理运用多核的能力将成为一个大规模应用程序的关键。   Java基础部分知识总结点击Java并发基础总结。Java多线程相关类的实现都在Java的并发包concurrent,concur...

weixin_33989780的博客 398

Claude Code本地Windows安装教程

本文介绍了在Windows系统上安装和配置Claude Code本地调用的详细步骤。首先需要确保网络畅通和账号余额充足,然后安装Node.js v22版本作为基础环境。接着通过npm全局安装Claude Code包,并进行版本验证。安装完成后,使用命令行登录并可以指定模型启动。最后还提供了切换API秘钥的方法,通过setup-token命令重新授权账号。整个过程配有详细的命令行示例和操作截图,方便用户按步骤完成安装配置。

猿小白的博客 2万+

嵌入式系统开发笔记96:将STM32CubeIDE设置为中文

本文介绍将STM32CubeIDE设置为中文的方法。

snmplink的博客 8563

Scala Actor并发编程

Scala 并发编程介绍,Scala Actor实例及匿名Actor创建方法。最后介绍Scala Actor和case class 以及接收回传消息的方法。

残缺的孤独 4878

YOLOv11【第二章:核心模块进阶篇·第28节】DynamicHead动态检测头设计!

🏆 本文收录于专栏 《YOLOv11实战:从入门到深度优化》。本专栏围绕 YOLOv11 的改进、训练、部署与工程优化 展开,系统梳理并复现当前主流的 YOLOv11 实战案例与优化方案,内容目前已覆盖 分类、检测、分割、追踪、关键点、OBB 检测 等多个方向。

**My Coding Family** 1万+

使用Scala实现一个并发库(阻塞版本, 下一篇文章提供NonBlocking版本)

这个例子来源于scala圣经级教程《Functional Programming in Scala》,由于本人跟着书中的代码敲了一遍,然后写了点测试代码验证了一下正确性,所以就放在这做个备忘吧。贴出来只是为了方便自己,如果看不懂,但是又感兴趣的就去看原书吧…… &amp;amp;gt; 注:这个并发库使用的线程池如果只有唯一一条工作线程的话,会导致线程阻塞,可以参考main方法中的示例,阻塞原因与代...

lhever_的博客 332

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

【Scala笔记——道】给你的Future一个Promise --最浪漫的并发模型(一)

Future 曾经,她在他的臂弯中,星空下,问他:“会不会一直对我这么好”, “你会不会在五年后来娶我”… Future 和 Promise Future 代表未来。未来总会来到,但这个未来是成功或是失败我们不得而之。抽象为核心三个方法 `onComplete’, ’onSuccess’, ‘onFailure’。其中 onSuccess和 onFailure是 onComplete的简化。"...

Contributor -> Committer 1270

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

C语言冒泡排序法,无序数值实现升序或降序

冒泡排序: 一串无序数值,从第一个数值开始依次与后面的数字比较,交换位置,得到有序的数值(升序排列/降序排列),此过程成为冒泡排序。 冒泡排序法: 用冒泡排序,将无序数值转化位有序数值的方法,称为冒泡排序法。 运行效果: 升序排列 降序排列 实现以上功能: [代码+解释] 升序排列(小→大) int[] arr = { 1, 5, 6, 4, 5 ,0}; Console.WriteLine("冒泡排序后的数值:"); for (int i = 0; i < arr.Len

HuiDieDie的博客 6156

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

原理剖析(第 012 篇)Netty之无锁队列MpscUnboundedArrayQueue原理分析

原理剖析(第 012 篇)Netty之无锁队列MpscUnboundedArrayQueue原理分析-一、大致介绍1、了解过netty原理的童鞋,其实应该知道工作线程组的每个子线程都维护了一个任务队列; 2、细心的童鞋会发现netty的队列是重写了队列的实现方法,覆盖了父类中的LinkedBlockingQueue队列,但是如今却换成了JCTools的一些并发队列,因为JCTools是一款对jdk并

YLIMH_HMILY的专栏 4050

scala进阶:Future的理解及使用

看了下scala future的官方文档,融合了自己的理解整理在这里,例子全部来自官方文档:http://docs.scala-lang.org/overviews/core/futures.html future represents a result of an async method, would could due to long time-consuming operat

cykic的专栏 1万+

scala的Future和Promise

前言 使用scala的Future和Promise就不得不提一下异步编程和多线程编程的区别。 共同点:异步和多线程两者都可以达到避免调用线程阻塞的目的,从而提高软件的可响应性 不同点: 线程不是一个计算机硬件的功能,而是操作系统提供的一种逻辑功能,线程本质上是进程中一段并发运行的代码,所以线程需要操作系统投入CPU资源来运行和调度。 多线程在单个线程中的处理程序依然是顺序执行,符合普通人的思维习...

齐梦星空 971

Netty源码分析---MpscLinkedQueue

 MpscLinkedQueue是netty自己实现的线程安全的队列,与JDK通过

凌霄的专栏 2477

netty5笔记-线程模型4-无锁队列MpscLinkedQueue

NioEventLoop里面使用了MpscLinkedQueue作为taskQueue,替换了父类中默认的LinkedBlockingQueue队列。taskQueue主要用于存放可执行任务,其调用的频率非常高,因此使用一个更高效的队列能带来很大的收益。 为什么在NioEvnetLoop里用MpscLinkedQueue替换了LinkedBlockingQueue,是使用了更好的算法?还是通过舍弃

youaremoon的专栏 6490

scala中一个高阶函数的N种调用方法

// (参数类型) => 结果类型 //定义接受一个函数参数的函数 def valueAtOneQuarter(f: (Double) => Double) = f(0.25) // 函数传参的完整写法 valueAtOneQuarter { (x: Double) => 3 * x } // scala类型推断写法 valueAtOneQua

NON blog 1469

BlockingQueue(阻塞队列)详解

注意:该随笔内容完全引自http://wsmajunfeng.iteye.com/blog/1629354,写的很好,非常感谢,复制过来算是个积累,怕以后找不到。 一. 前言   在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。本文详细介绍了Blo...

weixin_30478923的博客 2993

Intrusive MPSC node-based queue

mpscq :Multiple-producer single-consumer lock free queue Advantages: + Intrusive. No need for additional internal nodes. + Wait-free and fast producers. One XCHG is maximum what one can get with mu...

maimang1001的专栏 383

Scala 异步编程之 Future

同步异步,阻塞非阻塞,在IO模型中几个概念组合在一起不是很容易理解,但是只从代码执行的角度看同步异步是很清晰的: 同步代表这段代码中的逻辑必须执行完毕,而异步代表调用马上返回,但通常情况下是获取不到需要的值。 同步:val  value={ Thread.sleep(2000) 1 } 异步: val value=Future{ Thread.sleep(2000) 1 }

onwingsofsong的博客 8487

同时使用java和scala语言的maven项目pom文件示例

主要就是在pom.xml中添加scala的三个依赖包和maven-scala插件,配置如下:&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...

lhever_的博客 3158
上一篇: 使用Scala实现一个并发库(阻塞版本, 下一篇文章提供NonBlocking版本)
下一篇: 使用Scala实现一个基于性质的测试库
lhever_
博客等级 码龄14年 213粉丝 130原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址