小编use*_*842的帖子

最终关键字对并发的保证究竟是什么?

我想我已经读过,字段上的final关键字保证如果线程1实例化包含该字段的对象,那么如果线程2具有对该对象的引用,则线程2将始终看到该字段的初始化值(假设它是正确建造).它还在JLS中说过

[Thread 2]还将看到那些最终字段引用的任何对象或数组的版本,这些字段至少与最终字段一样是最新的.(JLS第17.5节)

这意味着如果我有A级

class A {
  private final B b = new B();
  private int aNotFinal = 2;
  ...
Run Code Online (Sandbox Code Playgroud)

和B级

class B {
  private final int bFinal = 1;
  private int bNotFinal = 2;
  ...
Run Code Online (Sandbox Code Playgroud)

然后aNotFinal不保证在线程2获得对类A的引用时被初始化,但字段bNotFinal是,因为B是由JLS中指定的最终字段引用的对象.

我有这个权利吗?

编辑:

如果我们有两个线程同时在类C的同一个实例上执行getA(),那么可能发生这种情况的情况就是这样

class C {
  private A a;

  public A getA(){
    if (a == null){
      // Thread 1 comes in here because a is null. Thread B doesn't come in 
      // here because by the time it gets here, object c …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading

40
推荐指数
2
解决办法
5067
查看次数

在scala ide中打开scala play项目时,"对象索引不是包views.html的成员"

我用play 2.3.7创建了一个游戏项目

在项目目录中,我运行了activator并运行eclipse命令来生成eclipse项目文件.

当我去eclipse时(我使用的是类型安全的构建ID:4.0.0-vfinal-20150119-1023-Typesafe中的Scala IDE,我的Application.scala文件中有一个错误:

object index is not a member of package views.html
Run Code Online (Sandbox Code Playgroud)

我的设置有什么不对劲吗?当我run在激活控制台提示符下执行时,应用程序运行正常.

谢谢!

编辑:添加代码

package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

} 
Run Code Online (Sandbox Code Playgroud)

错误在'Ok ..'行.

视图中有一个名为index.scala.html的文件,当我从激活器运行它时,应用程序会运行文件.

eclipse scala playframework-2.0

33
推荐指数
4
解决办法
1万
查看次数

Java lambda如果为空列表则返回null,否则为值的总和?

如果我想要列出账户的当前余额列表,我可以这样做:

accountOverview.setCurrentBalance(account.stream().
                filter(a -> a.getCurrentBalance() != null).
                mapToLong(a -> a.getCurrentBalance()).
                sum());
Run Code Online (Sandbox Code Playgroud)

但是这个表达式将返回0,即使所有余额都为空.如果所有余额都为空,我希望它返回null,如果有非空0余额则返回0,否则返回余额之和.

我怎么能用lambda表达式做到这一点?

非常感谢

java lambda

9
推荐指数
2
解决办法
1万
查看次数

如何在mac上使用adb和genymotion?

我在Mac OS X 10.9.4上使用cordova为Android构建应用程序.

我正在使用genymotion来创建虚拟设备.

昨天我能够获得'cordova run android'来让我的应用程序在genymotion虚拟设备上运行.

今天,我想我重复了昨天采取的步骤,但我无法让它发挥作用.

我启动了adb服务器(genymotion没有运行)

adb start-server
Run Code Online (Sandbox Code Playgroud)

adb服务器启动正常:

* daemon not running. starting it now on port 5037 *
* daemon started successfully *
Run Code Online (Sandbox Code Playgroud)

我可以看到它在那里

lsof -i tcp:5037
adb     19131 bw    7u  IPv4 0x3c0bc54e449e35f5      0t0  TCP localhost:5037 (LISTEN)
Run Code Online (Sandbox Code Playgroud)

没有附加设备

adb devices
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好

现在,当我启动genymotion并启动虚拟设备时,我的adb似乎已经被杀死并且无法启动,因为虚拟设备显然已将其杀死并启动了自己的adb实例

unknown-6c-40-08-9b-25-2c:todo bw$ adb devices
adb server is out of date.  killing...
cannot bind 'tcp:5037'
ADB server didn't ACK
* failed to start daemon *
error: 
Run Code Online (Sandbox Code Playgroud)

现在,这使我无法将设备添加到adb,以便我可以通过执行来访问它

cordova run android
Run Code Online (Sandbox Code Playgroud)

此命令现在失败,因为它找不到正在运行的设备.

我尝试以root身份运行adb start-server,因此genymotion无法杀死它,但是genymotion虚拟设备无法启动. …

macos android cordova genymotion

4
推荐指数
1
解决办法
8922
查看次数

在EJB 3.1中,容器代理在技术上如何有效地欺骗我的无接口对象?

在EJB 3.1中,我可以创建一个无接口会话bean。当将其注入其他类时,他们会收到一个与我的pojo类型相同的对象,而实际上他们得到的是一个存根,它通过一连串的类与我的pojo进行交互。这个技巧如何实现?我可以理解存根是否具有与我的pojo相同的接口类型,但是容器如何创建相同类型的对象?反射?编织?非常感谢!

java jboss ejb-3.1 glassfish-4

4
推荐指数
1
解决办法
143
查看次数

What are the use cases for IntStream?

I've written some code that creates a List<Integer> from a Stream<List<Integer>> like so:

List<Integer> abc = stream.flatMap(e -> e.stream()).
    collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

But I'm interested how to use IntStream. I thought I could do

List<Integer> abc = stream.flatMapToInt(e -> e.stream()).
    collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

using flatMapToInt to give me an IntStream that the collector would collect, but I get a compilation error:

Type mismatch: cannot convert from Collector<Object,?,List<Object>> to Supplier<R>
Run Code Online (Sandbox Code Playgroud)

What is an IntStream, how is it different to a regular stream, and when would …

java lambda

1
推荐指数
1
解决办法
401
查看次数

这个陈述背后的语法是什么?

我发现这句话:

var o = None: Option[String]
Run Code Online (Sandbox Code Playgroud)

您可以使用将o设置为None,以后可能将其设置为Some [String]

但是这个实际的陈述如何在语法上分解?None是一个扩展Option [Nothing]的对象 - 但是该语句的其余部分是如何工作的?例如,冒号做什么?

非常感谢!

scala

0
推荐指数
1
解决办法
52
查看次数