当我想在创建Observable后添加元素时,如何实现场景,是否可以完成?在Observer模式中,我只会触发事件左右.你有什么想法吗?
import rx.lang.scala._
val target = Observable(1,2,3,4)
val subscription1 = target subscribe(println(_))
val subscription2 = target subscribe(println(_))
def addToObservable(toAdd: Int, target: Observable[Int]): Observable[Int] = {
target/*.addElementAndNotifyObservers(toAdd)*/
}
addToObservable(4, target) //should print 4 on all subscriptions
addToObservable(6, target) //should print 6 on all subscriptions
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用specs2,我有隐式转换的问题,这些转换与我从scala.concurrent.duration._中导入的转换混合,是否有任何方法可以从范围中排除隐含的转换?
import org.specs2.mutable.Specification
import scala.concurrent.duration._
class StatisticsSampleCacheSpec extends Specification {
val map: Map[Long, Duration] = Map(
1L -> 5.minute,
3L -> 3.day,
5L -> 5.day,
7L -> 30.day)
}
Run Code Online (Sandbox Code Playgroud) 为什么没有线程等待notify()?线程启动然后进入等待池,但是在那之后它继续执行.
public class JavaApplication2 {
public static void main(String [] args) {
ThreadB b = new ThreadB();
synchronized(b) {
b.start();
try {
System.out.println("1");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
@Override
public void run() {
synchronized(this) {
total += 1;
//notify();
}
}
}
Run Code Online (Sandbox Code Playgroud) 在Scala中你可以做这样的事情:
trait A[T]
trait B[C[_] <: A[_]] {
def apply[T](entity: C[T]): T
}
Run Code Online (Sandbox Code Playgroud)
Java模拟看起来像这样:
interface A<T>
interface B<C<?> extends A> {
<T> T apply(entity: C<T>): T
}
Run Code Online (Sandbox Code Playgroud)
您可以将B与任何扩展A的类型一起使用,并使用此子类型的类型参数作为方法的输出类型.
但它不编译,你会用Java做什么?
public static void main(String argv[]){
String a="0700";
Scanner s = new Scanner(a);
while(s.hasNextLong()){
System.out.print(s.nextLong()+",");
}
Run Code Online (Sandbox Code Playgroud)
结果将是"700",而不是"448".