小编Pus*_*raj的帖子

java monitor是否包含实例变量?

是java中的监视器不限制对实例变量的访问,而只限制对同步语句中声明为synchronized的方法或代码的访问?

我创建了两个线程,thread y调用sync方法,该方法在sync()调用未声明同步的unsync方法时声明为synchronized.两者都在共享对象上调用方法thread r.

unsync()能够修改对象的实例变量,s同时该对象的监视器或锁仍然被持有Thread r.

是Java中的监视器不限制对实例变量的访问,并且只限制声明为synchronized的方法或同步语句中的代码?

public class Stuff {

    private int a = 10;

    public synchronized void sync() {
        long t1 = System.currentTimeMillis();
        System.out.println("Okay, I am in sync() method. "
                        + "I will be waiting for 10 seconds. Current Time = "
                        + System.currentTimeMillis());
        while (System.currentTimeMillis() - t1 < 10000);
        System.out.println("Okay, I have waited for 10 seconds. Current time is "
                        + System.currentTimeMillis()
                        + ". Now I …
Run Code Online (Sandbox Code Playgroud)

java synchronized

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

为什么在CopyOnWriteArrayList中需要setArray()方法调用

在CopyOnWriteArrayList.java中,在方法集(int index,E element)下面

public E set(int index, E element) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] elements = getArray();
        Object oldValue = elements[index];

        if (oldValue != element) {
            int len = elements.length;
            Object[] newElements = Arrays.copyOf(elements, len);
            newElements[index] = element;
            setArray(newElements);
        } else {
            // Not quite a no-op; ensures volatile write semantics
            setArray(elements);----? Why this call required?
        }
        return (E)oldValue;
    } finally {
        lock.unlock();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么需要调用setArray()?我无法理解上面那个方法调用的注释.是因为我们没有使用synchronized块,我们必须手动刷新我们使用的所有变量吗?在上面的方法中,他们使用了重入锁.如果他们使用了synchronized语句,他们仍然需要调用setArray()方法吗?我想不是.

问题2:如果我们最终在else中,这意味着我们没有修改元素数组,那么为什么我们需要刷新变量数组的值?

java collections java-memory-model locks

13
推荐指数
2
解决办法
868
查看次数

同步进入、退出和易失性读写时的缓存刷新

当同步块执行完成时,所有处理器缓存都会被刷新,还是只有同步语句所作用的对象才会被刷新?在下面的例子中,当线程完成method2的执行时,obj2的数据是否也被刷新到主内存?

class cls1 {
    int x=10;
}

class cls2{
    int y=10;
}

class cls3{
    cls1 obj1;
    cls2 obj2;
    public void method2(){
        obj2.y=10;
        synchronized(obj1){
            obj1.x=30;
        }
    }

    public static void main(String[] args) {
        final cls3 obj3 = new cls3();

        Thread thread1 = new Thread(){
            public void run(){
                obj3.method2();
            }
        };
        Thread thread2 = new Thread(){
            public void run(){
                System.out.println(obj3.obj1.x);
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

java synchronized

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

Class类中的getDeclaredMethods()

public Method[] getDeclaredMethods() throws SecurityException
Run Code Online (Sandbox Code Playgroud)

有如下文件.

/**
     *
     * Returns an array containing {@code Method} objects reflecting all the
     * declared methods of the class or interface represented by this {@code
     * Class} object, including public, protected, default (package)
     * access, and private methods, but excluding inherited methods.

<p> If this {@code Class} object represents a type that has multiple
             * declared methods with the same name and parameter types, but different
             * return types, then the returned array has …
Run Code Online (Sandbox Code Playgroud)

java reflection

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

java中的Arrays.asList()实现

在Arrays.java文件中.方法asList定义如下.

 @SafeVarargs
    public static <T> List<T> asList(T... arr) {
        return new ArrayList<>(arr);
    }
Run Code Online (Sandbox Code Playgroud)

这里使用Array.(arr),new ArrayList(arr)调用ArrayList构造函数.但是ArrayList类中没有构造函数接受Array作为参数.Array类在Collections FrameWork中,但没有实现Collection接口.ArrayList中的构造函数是

ArrayList()

ArrayList(Collection<? extends E> c)

ArrayList(int initialCapacity)
Run Code Online (Sandbox Code Playgroud)

谁能解释那里发生了什么?

java arrays arraylist

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

条件运算符?:类型评估

void method(Set<String> whiteListProviders){

        HashSet<String> hashedWhitelistedProviders;

        HashSet<String> fdsfh = (hashedWhitelistedProviders = (HashSet<String>) whitelistedProviders);

        HashSet<String> ghjk = (hashedWhitelistedProviders = new HashSet<String>(whitelistedProviders));

        HashSet<String> gh4jk = true ? fdsfh : ghjk; //compiles

        true?fdsfh:ghjk; //gives error "Type mismatch: cannot convert from HashSet<String> to boolean" 




}
Run Code Online (Sandbox Code Playgroud)

我读了http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25, 但仍然无法理解为什么它在eclipse中给出了编译错误

java conditional

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

在java中初始化类时,请展示以下内容

class Size{
Size(){

}
//public  final Size SMALL = new Size();//infinite loop
  final Size as = new Size();//infinite loop
//static Size aw = new Size();//No infinte loop

}


class b{

static public void main(String ftr[]){

    Size z = new Size();

  }
}
Run Code Online (Sandbox Code Playgroud)

当静态大小aw等于new Size(); 上述程序运行时出错.当程序运行时,请参阅下面的方案:

  public  final Size SMALL = new Size();// infinite loop is happening 
  final Size as = new Size();  
Run Code Online (Sandbox Code Playgroud)

一个无限循环正在发生,程序正在给予

  Exception in thread "main" java.lang.StackOverflowError
  at Size.<init>(Interesting.java:6)  
  at Size.<init>(Interesting.java:6) 
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释,或者指出我可以找到背后原因的地方.

java

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