小编dsa*_*ish的帖子

为什么Phantom References在排队时没有被清除?

我们可以看到"幻影可达"与"无法访问"一样无法访问:§

如果一个物体既不是强烈的,柔和的,也不是微弱的可触及的,那么该物体是幻影可达的,它已被最终确定,并且一些幻象参考指的是它.

最后,如果无法通过上述任何方式访问某个对象,则该对象无法访问,因此有资格进行回收.

现在,来自:http://download.oracle.com/javase/6/docs/api/java/lang/ref/PhantomReference.html

与软引用和弱引用不同,垃圾收集器在排队时不会自动清除幻像引用.通过幻像引用可访问的对象将保持不变,直到所有此类引用都被清除或自身无法访问.

基本原理是什么?还有一个吗?

这是Java API怪癖的另一个典型案例吗?

java garbage-collection

16
推荐指数
2
解决办法
997
查看次数

何时对同步块的更改对其他线程可见

假设我在synchronized方法中更新了两个变量的值.退出synchronized块之前,同步方法中设置的新值是否可能对其他线程可见?

public synchronized void setValues(){
    a=5;
    // assume thread is preempted after this assignment
    // would the value 5 be visible to other threads?
    // my understanding is that the values will not be flushed to
    // main memory until the lock is released- i.e., until the synchronized
    // method is complete. So the changes will not be visible to other 
    // threads even when not using synchronization
    b=10;
}
Run Code Online (Sandbox Code Playgroud)

下面的方法同步,所以我理解该线程可能会 看到陈旧的值.我的问题是,如果线程在分配给a之后被抢占,那么变量a的新值"5"是否可能在printValues方法中可见?

public …
Run Code Online (Sandbox Code Playgroud)

java synchronization

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

使用findbugs或其他分析工具检测竞争条件

下面的bean不是线程安全的:方法addIfNotExist不同步,因此由于竞争条件,相同的术语可能会被添加两次.我使用JCIP注释@ThreadSafe注释该类,希望FindBugs发现该实现不是线程安全的,并将其标记为错误,但事实并非如此.是否有任何工具可以识别代码库中的这些类型的错误?

方法addIfNotExist和isExist应该同步,以使这个bean线程安全.isExist方法也应同步吗?

package com.test;

import java.util.ArrayList;

import java.util.Collection;

import net.jcip.annotations.GuardedBy;

import net.jcip.annotations.ThreadSafe;

@ThreadSafe

public class Dictionary {

    @GuardedBy("this")
    public Collection<String> terms = new ArrayList<String>();

    public void addIfNotExist(final String input) {
        if (!this.terms.contains(input)) {
            this.terms.add(input);
        }
    }

    public boolean isExist(final String input){
        return this.terms.contains(input);
    }

    public void remove(final String input){
        this.terms.remove(input);
    }
}
Run Code Online (Sandbox Code Playgroud)

java findbugs thread-safety

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

mongodb - 未使用日期索引

集合事件具有userId和一系列事件 - 数组中的每个元素都是嵌入式文档.例:

{
    "_id" : ObjectId("4f8f48cf5f0d23945a4068ca"),
    "events" : [
            {
                    "eventType" : "profile-updated",
                    "eventId" : "247266",
                    "eventDate" : ISODate("1938-04-27T23:05:51.451Z"),
            },
           {
                   "eventType" : "login",
                   "eventId" : "64531",
                   "eventDate" : ISODate("1948-05-15T23:11:37.413Z"),
           }
    ],
    "userId" : "junit-19568842",
Run Code Online (Sandbox Code Playgroud)

}

使用如下所示的查询来查找过去30天内生成的事件:

db.events.find( { events : { $elemMatch: { "eventId" : 201, 
"eventDate" : {$gt : new Date(1231657163876) } } } }  ).explain()
Run Code Online (Sandbox Code Playgroud)

查询计划显示当测试数据包含较少的事件(大约20)时,使用"events.eventDate"的索引:

{
    "cursor" : "BtreeCursor events.eventDate_1",
    "nscanned" : 0,
    "nscannedObjects" : 0,
    "n" : 0,
    "millis" : 0,
    "nYields" : 0, …
Run Code Online (Sandbox Code Playgroud)

mongodb

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

Spring - JPA - 插入 - 不传递具有默认值的字段的值会引发错误

表中的一些不可为空的字段具有默认值。通过 JPA 将新行插入表时,我不想为这些字段传递任何值,以便它们获得默认值。但是,当通过 Spring JPA 存储库类插入新行时,出现无法插入空值的错误。我注意到 JPA 发送到数据库的插入语句列出了所有字段:

insert into table (field1, field2, field3) values ('abc',null,null);
Run Code Online (Sandbox Code Playgroud)

由于field2和field3指定了null,因此未分配默认值,数据库会抛出无法插入null值的错误。有解决方法吗?

java spring jpa spring-data-jpa

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

否定的前瞻性正则表达式无效

input1="caused/VBN by/IN thyroid disorder"
Run Code Online (Sandbox Code Playgroud)

要求:找到"caused"后跟斜线后跟任意数量的大写字母的单词- 后面跟不上空格+ "by/IN.

在上面的示例中,"caused/VBN"后面跟着" by/IN",因此'cause'不匹配.

input2="caused/VBN thyroid disorder" 
Run Code Online (Sandbox Code Playgroud)

"by/IN" 不遵循造成的,所以它应该匹配

regex="caused/[A-Z]+(?![\\s]+by/IN)"
Run Code Online (Sandbox Code Playgroud)

caused/[A-Z]+- 单词'引起'+/+一个或多个大写字母
(?![\\s]+by)- 负向前瞻 - 不匹配空格和

以下是我用来测试的简单方法

public static void main(String[] args){
    String input = "caused/VBN by/IN thyroid disorder";

    String regex = "caused/[A-Z]+(?![\\s]+by/IN)";

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);

    while(matcher.find()){
        System.out.println(matcher.group());
    }
Run Code Online (Sandbox Code Playgroud)

输出: caused/VB

我不明白为什么我的负面前瞻正则表达式不起作用.

java regex regex-negation regex-lookarounds

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