阻塞队列取代同步(Blocking queue to replace synchronized)
我用下面的阻塞队列替换了下面的代码。
为什么我会得到一个队列完整的异常 - 是不是阻塞队列可以预防这种情况?class Offer implements Runnable{ Random r=new Random(); public void run(){ while(System.currentTimeMillis() < end){ synchronized(b){ try{ while(b.size()>10){ b.wait(); } b.add(r.nextInt()); b.notify(); }catch(InterruptedException x){} } } } } class Take implements Runnable{ public void run(){ while(System.currentTimeMillis() < end){ synchronized(b){ try{ while(b.size()<1){ b.wait(); } b.remove(0); b.notify(); }catch(InterruptedException x){} } } } }
阻塞队列相当于 -
BlockingQueue<Integer> b = new ArrayBlockingQueue<Integer>(10); class Offer implements Runnable{ Random r=new Random(); public void run(){ while(System.currentTimeMillis() < end){ b.add(r.nextInt()); } } } class Take implements Runnable{ public void run(){ while(System.currentTimeMillis() < end){ b.remove(0); } } }
I replaced the following code with the blocking Queue below.
Why do I get a queue full exception - isn't the blocking queue expected to prevent just that?class Offer implements Runnable{ Random r=new Random(); public void run(){ while(System.currentTimeMillis() < end){ synchronized(b){ try{ while(b.size()>10){ b.wait(); } b.add(r.nextInt()); b.notify(); }catch(InterruptedException x){} } } } } class Take implements Runnable{ public void run(){ while(System.currentTimeMillis() < end){ synchronized(b){ try{ while(b.size()<1){ b.wait(); } b.remove(0); b.notify(); }catch(InterruptedException x){} } } } }
THe blockingqueue equivalent -
BlockingQueue<Integer> b = new ArrayBlockingQueue<Integer>(10); class Offer implements Runnable{ Random r=new Random(); public void run(){ while(System.currentTimeMillis() < end){ b.add(r.nextInt()); } } } class Take implements Runnable{ public void run(){ while(System.currentTimeMillis() < end){ b.remove(0); } } }
原文:https://stackoverflow.com/questions/43956250
更新时间:2023-05-30 12:05
最满意答案
你可以使用
List.collect
方法和模式匹配:list.collect{ case Example(x, "3", null) => x } // res1: List[String] = List(Five)
You could use
List.collect
method with pattern matching:list.collect{ case Example(x, "3", null) => x } // res1: List[String] = List(Five)
相关问答
更多-
将元素添加到列表在Scala中(Add element to a list In Scala)[2022-11-09]
您正在使用不可变列表。 列表上的操作返回一个新的列表。 旧名单保持不变。 如果另一个类/方法持有对原始集合的引用并且依赖于其保持不变,这可能非常有用。 您可以使用不同的命名val val myList1 = 1.0 :: 5.5 :: Nil val myList2 = 2.2 :: 3.7 :: mylist1 或者使用var作为 var myList = 1.0 :: 5.5 :: Nil myList :::= List(2.2, 3.7) 这是等同的语法: myList = myList.: ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
将Scala列表的每个元素与列表中的其余元素进行比较(Compare each element of Scala list against rest of elements in the list)[2022-05-14]
你尝试过使用for -loop吗? for( a <- lis; b <- lis) { // do stuff with a and b } 为了进一步方便,这里有一个简单的例子来了解幕后的迭代: scala> val list = 1 to 3 list: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3) scala> for(a <- list; b <- list) println(a+" - "+b) 1 - 1 1 - ... -
首先,您希望将其添加到Maps ,因此查找起来更容易,然后您可以只map所有键: val L1Map = L1.map{case (x,y,z) => ((x,y) -> z)}.toMap val L2Map = L2.map{case (x,y,z) => ((x,y) -> z)}.toMap val allKeys = L1Map.keySet ++ L2Map.keySet val result = allKeys map {case (x,y) => (x, y, L1Map.getOrE ...
-
如何在Scala中的数组列表中获取两个元素的总和(How to get the sum of two elements in a list of Arrays in Scala)[2023-10-09]
它肯定不会“归还一个单位”。 res是一系列双打。 你忘记了这个名字,但这很容易解决: val res = datArray.map(x => x(1) -> x(4).toInt * x(5).toDouble) 现在,你有一系列元组, Seq[(String, Double)] ,其中第一个元素是名称,第二个元素是你所追求的产品。 您可以使用此列表执行各种操作: 将其转换为地图name - > score : res.toMap (注意:如果您有相同名称的重复条目,则只保留每组中的最后一个) 找到v ... -
Scala List中的最后一个元素?(Last element in Scala List?)[2023-05-13]
是和不。 事实上,结束标记总是单独的零,同一个对象,最后一个元素是前一个元素。 scala> val a = 1 :: 2 :: Nil a: List[Int] = List(1, 2) scala> a.last res10: Int = 2 您可能会争论这个术语,但编码人员往往是这方面的实证主义者,事实是,代码所说的。 Yes and No. While in fact the end marker is always the singleton Nil, the same object, the ... -
另一种类似的方法是: l match{ case Nil => Nil case xs :+ y => xs :+ change(y) } Another similar way is: l match{ case Nil => Nil case xs :+ y => xs :+ change(y) }
-
你可以使用List.collect方法和模式匹配: list.collect{ case Example(x, "3", null) => x } // res1: List[String] = List(Five) You could use List.collect method with pattern matching: list.collect{ case Example(x, "3", null) => x } // res1: List[String] = List(Five)
-
Scala列表匹配最后一个元素(Scala List match last element)[2022-03-13]
您需要切换订单。 xs符号将热切地匹配该位置中的任何内容。 试图首先匹配Nil将使该声明不再无法访问。 此外,您仍然需要自己匹配Nil以计算空列表。 def stringify(list: List[String]): String = list match { case x :: Nil => x case x :: xs => x + ":" + stringify(xs) case Nil => "" } 虽然mkString已经完成了你mkString事情。 You need ... -
Scala是一种静态语言 。 您的代码不起作用,因为您为元素混合了不同的类型。 这就是编译器为List[Any]推断类型List[Any]的原因。 如果列表具有类型List[(Int, Char)]则代码可以正常工作。 虽然有一种方法可以使用强制转换来解决您的问题,但我希望大胆地概述它是单一的: l(1).asInstanceOf[(Int, Char)]._1 惯用方法是具有单形列表,即包含相同类型元素的列表。 Scala is a static language. Your code does not ...