是否有像Java中的Intel TBB这样的库支持Parallelism.
你能发现这个bug吗?这将抛出一个java.lang.OutOfMemoryError.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestTheads {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(1);
while(true) {
executorService.submit(new Runnable() {
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是我调用executorService.submit()而不是executorService.execute(),因为submit()返回一个Future我忽略的对象.有了execute(),这个程序实际上将永远运行.
然而,人们并不总是拥有一种execute()方法,例如使用时ScheduledExecutorService:
public static void main(String[] args) {
// this will FAIL because I ignore the ScheduledFuture object
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); …Run Code Online (Sandbox Code Playgroud) 我正在寻找具有以下功能的java.util.Set的实现:
据我所知,唯一正确的impl是CopyOnWriteArraySet,但它在文档中指出:
变异操作(添加,设置,删除等)很昂贵,因为它们通常需要复制整个底层阵列.
在我的情况下,我有很多插入到队列末尾(set)和批量从队列的头部删除(和读取).那么,有什么建议吗?
我们有一个stipes(java)web-app,需要从一个方法进行大约15个不同的webserivce调用.例如: ...
public Resolution userProfile()
{
serviceOneCall();
serviceTwoCall();
serviceThreeCall();
serviceFourCall();
....
serviceTenCall();
return new RedirectResolution("profiel.jsp");
}
Run Code Online (Sandbox Code Playgroud)
所有这些都可以并行调用,而不是相互依赖.大多数这些调用所做的一件事就是将数据放入会话中,并且一两个可能将数据放入会话中的同一对象中,因此线程安全可能是一个问题.
有人能建议同时调用所有这些的好方法吗?
java stripes web-services web-applications java.util.concurrent
我想了解线程池的逻辑,下面有一个简单的不正确且不完整的实现:
class ThreadPool {
private BlockingQueue<Runnable> taskQueue;
public ThreadPool(int numberOfThreads) {
taskQueue = new LinkedBlockingQueue<Runnable>(10);
for (int i = 0; i < numberOfThreads; i++) {
new PoolThread(taskQueue).start();
}
}
public void execute(Runnable task) throws InterruptedException {
taskQueue.put(task);
}
}
class PoolThread extends Thread {
private BlockingQueue<Runnable> taskQueue;
public PoolThread(BlockingQueue<Runnable> queue) {
taskQueue = queue;
}
public void run() {
while (true) {
try {
taskQueue.take().run();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果要执行的线程数超过taskQueue大小,将会阻塞调用线程吗?ThreadPoolExecutor-在这里我们可以看到在这种情况下这是拒绝执行处理程序的工作,但是我仍然不明白它是如何工作的。在此先感谢您的帮助。
编辑:
将阻止队列的最大大小设置为10
我写了一个类,CountDownLatchContainer 它有一个await()方法,等待所有CountDownLatches.一切都很好.
public final class CountDownLatchContainer
{
private final Set<CountDownLatch> countDowns;
public CountDownLatchContainer(List<CountDownLatch> countDowns)
{
this.countDowns = new HashSet<CountDownLatch>(countDowns);
}
public void await() throws InterruptedException
{
for (CountDownLatch count : countDowns)
count.await();
}
}
Run Code Online (Sandbox Code Playgroud)
为了科学和艺术(:D),我想扩展功能并public boolean await(long timeout, TimeUnit unit)从CountDownLatch类中添加.
我希望每个倒计时锁存器具有相同的超时和整个方法只是为了阻止timeoutTimeUnits 的数量.我很难实现这一目标.我有的是:
public boolean await(long timeout, TimeUnit unit) throws InterruptedException
{
boolean success = true;
for (CountDownLatch count : countDowns)
success &= count.await(timeout / countDowns.size(), unit);
return success;
}
Run Code Online (Sandbox Code Playgroud)
因此,总体超时时间会得到照顾,但每次倒计时只占整个时间的百分比.
那么如何在不超过总时间的情况下为单个锁存器提供与方法参数中指定的时间相同的时间? …
在最新一期的《德国Java杂志》中,有一个代码示例ReentrantReadWriteLock(我想ReadWriteLock通常是这样)经常被错误地使用。不幸的是,作者并不愿意解释原因。
private static final ReadWriteLock lock = new ReentrantReadWriteLock();
// #1: correct use
lock.writeLock().lock();
try {
// do stuff
} finally {
lock.writeLock().unlock();
}
// #2: incorrect use
try {
lock.writeLock().lock();
// do stuff
} finally {
lock.writeLock().unlock();
}
// #3: incorrect use
lock.writeLock().lock();
// do stuff
lock.writeLock().unlock();
Run Code Online (Sandbox Code Playgroud)
我明白了为什么#3是错的。但是#1和#2之间有什么区别?假设lock.writeLock().lock();没有抛出异常(编辑:错误的假设,请参见接受的答案),这些版本与我相同。
具有ReentrantLock和lock()/ unlock()的普通模式如下所示:
lck.lock();
try {
// ...
}
finally {
lck.unlock();
}
Run Code Online (Sandbox Code Playgroud)
可以重构为
synchronized(lck) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
?
又为什么呢
在下面的代码中scheduleAtFixedRate无休止地运行.
所以问题是: -
为什么java提供无限的线程执行场景?
Runnable task1 = () -> System.out.println("Hello Zoo");
Future<?> result = service1.scheduleAtFixedRate(task1, 8, 2, TimeUnit.SECONDS);
System.out.println(result.get());
System.out.println(result.isDone());
Run Code Online (Sandbox Code Playgroud)
程序从不打印输出result.get()应为null或System.out.println(result.isDone());应为0.
所以我在调用scheduleAtFixedRate代码之后的观点应该是无法访问的.
java concurrency scheduled-tasks java.util.concurrent java-8
在检查Kotlin协同程序的来源时,我注意到JDK 8 CompletableFuture**之间存在差异(标记为)
public fun <T> future(
context: CoroutineContext = DefaultDispatcher,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
): CompletableFuture<T> {
require(!start.isLazy) { "$start start is not supported" }
val newContext = newCoroutineContext(context)
val job = Job(newContext[Job])
val future = CompletableFutureCoroutine<T>(newContext + job)
job.cancelFutureOnCompletion(future)
** future.whenComplete { _, exception -> job.cancel(exception) } **
start(block, receiver=future, completion=future) // use the specified start strategy
return future
}
private class CompletableFutureCoroutine<T>(
override val context: CoroutineContext
) : CompletableFuture<T>(), …Run Code Online (Sandbox Code Playgroud) coroutine java.util.concurrent guava kotlin kotlin-coroutines
java ×9
concurrency ×7
coroutine ×1
guava ×1
java-8 ×1
kotlin ×1
locking ×1
set ×1
stripes ×1
threadpool ×1
web-services ×1