根据Java Concurrency in Practice,第11.4.3章说:
锁分裂有时可以扩展到一组变量独立对象的分区锁定,在这种情况下,它被称为锁定条带.例如,ConcurrentHashMap的实现使用一个包含16个锁的数组,每个锁保护1/16的散列桶; 铲斗N由锁定N mod 16保护.
我仍然有理解和可视化锁条纹和铲斗机制的问题.有人可以用很好理解的话来解释这个:)
提前致谢.
我注意到我的应用程序中的一些命令失败了
Caused by: ! com.netflix.hystrix.exception.HystrixRuntimeException: GetAPICommand timed-out and no fallback available.
out: ! at com.netflix.hystrix.HystrixCommand.getFallbackOrThrowException(HystrixCommand.java:1631)
out: ! at com.netflix.hystrix.HystrixCommand.access$2000(HystrixCommand.java:97)
out: ! at com.netflix.hystrix.HystrixCommand$TimeoutObservable$1$1.tick(HystrixCommand.java:1025)
out: ! at com.netflix.hystrix.HystrixCommand$1.performBlockingGetWithTimeout(HystrixCommand.java:621)
out: ! at com.netflix.hystrix.HystrixCommand$1.get(HystrixCommand.java:516)
out: ! at com.netflix.hystrix.HystrixCommand.execute(HystrixCommand.java:425)
out: Caused by: ! java.util.concurrent.TimeoutException: null
out: !... 11 common frames omitted
Run Code Online (Sandbox Code Playgroud)
这是我的Hystrix配置覆盖:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=210000
hystrix.threadpool.default.coreSize=50
hystrix.threadpool.default.maxQueueSize=100
hystrix.threadpool.default.queueSizeRejectionThreshold=50
Run Code Online (Sandbox Code Playgroud)
这是什么样的超时?它是外部应用程序的读/连接超时吗?我该如何调试呢?
一个执行器对象是否意味着在一个shutdown?之后重用?我的意思是,如果我调用shutdown或shutdownNow在执行程序终止后,我应该new创建一个新的线程池,还是可以以某种方式"重置"/重用以前终止的执行程序并重用它?
更新:
如果我需要创建新的线程池,我怎么能"理解"前一个已停止?
例如以下内容:
public void startPool(){
if(threadPool != null && !threadPool.isShutdown()){
return;
}
threadPool = Executors.newCachedThreadPool();
//other stuff
}
public void stopPool(){
if(threadPool != null){
threadPool.shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
不管用.如果我调用stop然后start由于条件将不会创建新的线程池.编码的正确方法是什么?
我用一个固定大小的线程池Executors.newFixedThreadPool(2),我执行了10个Runnable对象.我设置断点并追踪执行.但是,fixedSizeThreadPool.awaitTermination()即使完成了所有任务,也不允许我继续.
基本上:
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; ++i) {
fixedSizeThreadPool.execute(myRunables[i]);
}
try {
fixedSizeThreadPool.awaitTermination(timeout, timeoutUnits);
} catch (Exception e) { }
System.out.println("done!");
Run Code Online (Sandbox Code Playgroud)
但这总是被困住awaitTermination.怎么了?
AtomicBoolean将其值存储在:
private volatile int value;
Run Code Online (Sandbox Code Playgroud)
然后,例如,提取其值的方式如下:
public final boolean get() {
return value != 0;
}
Run Code Online (Sandbox Code Playgroud)
它背后的原因是什么?为什么没用boolean?
我试图理解CompletableFutureJava 8 如何与Java内存模型交互.在我看来,对于程序员的理智,理想情况下应该成立:
CompletableFuture - 在执行任何java.util.concurrent文档中有一条说明:
在提交
Runnable到执行之前的线程中的操作Executor- 在执行开始之前.同样的Callables提交给ExecutorService.
这表明第一个属性为true,只要完成未来的线程执行完成依赖阶段或将其提交给Executor.另一方面,在阅读CompletableFuture文档后,我不太确定:
为非异步方法的依赖完成提供的动作可以由完成当前的线程执行
CompletableFuture,或者由完成方法的任何其他调用者执行.
这让我想到了我的问题:
CompletableFuture?附录:
在具体示例中,请考虑以下代码:
List<String> list1 = new ArrayList<>();
list1.add("foo");
CompletableFuture<List<String>> future =
CompletableFuture.supplyAsync(() -> {
List<String> list2 = new ArrayList<>();
list2.addAll(list1);
return list2;
});
Run Code Online (Sandbox Code Playgroud)
能够保证所有的加入"foo"到list1是可见的lambda函数?是否保证添加 …
java java.util.concurrent java-memory-model java-8 completable-future
我对此表示怀疑,在Java语言中,我们需要保持锁定,然后再等待满足某些条件.
例如,int java monitor lock:
synchronized(lock){
System.out.println("before lock ...");
lock.wait();
System.out.println("after lock ...");
}
Run Code Online (Sandbox Code Playgroud)
或者是黄色的工具.
Lock lock = new ReentrantLock();
Condition cond = lock.newCondition();
lock.lock();
try{
System.out.println("before condition ...");
cond.await();
System.out.println("after condition ...");
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
Run Code Online (Sandbox Code Playgroud)
所以,为什么我们不能等待,而不是锁定?
如果只是因为Java,其他语言的工作方式不同?
我希望你能在设计之后解释原因,但不仅仅是JAVA-SPEC的定义.
我需要一个long具有以下要求/事实的类型的计数器:
根据这些要求,您将如何选择实施计数器?作为一个简单的long,作为一个volatile long或使用AtomicLong?为什么?
目前我有一个volatile long但是想知道另一种方法是否会更好.我也是通过做++counter而不是增加我的长期counter++.这真的更有效率(因为我已被引导相信其他地方),因为没有完成任务?
当我通过JDK 7时,我发现它java.util.concurrent.RunnableFuture<V>有一个run方法.我想知道在接口中重复相同的run方法签名的重要性是什么时候它已经扩展了Runnable.
package java.util.concurrent;
public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}
Run Code Online (Sandbox Code Playgroud) 我在我的Android应用程序中有以下代码:
/**
* callback executed after fetching the data.
*/
public void OnPointsFetch(ArrayList<Shop> result) {
toggleLoader(false);
this.shops = result;
if(activeFilter == Constants.POINTS_FILTER_AVAILABLE){
for(Shop s : result){
if(s.getClientPoints().getPointsAvailable() == 0){
this.shops.remove(s);
}
}
}
else{
for(Shop s : result){
if(s.getClientPoints().getPointsSpent() == 0){
this.shops.remove(s);
}
}
}
ptsListAdapter.setCollection(this.shops);
ptsListAdapter.setFilter(this.activeFilter);
}
Run Code Online (Sandbox Code Playgroud)
在异步任务的结果上调用此方法.我需要在传递给列表适配器之前删除集合的一些元素.
11-23 17:39:59.760: E/AndroidRuntime(19777): java.util.ConcurrentModificationException
11-23 17:39:59.760: E/AndroidRuntime(19777): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
Run Code Online (Sandbox Code Playgroud) java ×9
concurrency ×4
android ×1
arraylist ×1
atomic ×1
exception ×1
hystrix ×1
java-8 ×1
performance ×1
wait ×1