是否可以为Java 8并行流指定一个自定义线程池?我到处都找不到。

假设我有一个服务器应用程序,我想使用并行流。但是这个应用程序很大,而且是多线程的,所以我想对它进行划分。我不希望在来自另一个模块的applicationblock任务的一个模块中运行缓慢的任务。

如果我不能为不同的模块使用不同的线程池,这意味着我不能在大多数实际情况下安全地使用并行流。

试试下面的例子。有一些CPU密集型任务在单独的线程中执行。 任务利用并行流。第一个任务中断,因此每一步花费1秒(通过线程睡眠模拟)。问题是其他线程卡住,等待中断的任务完成。这是一个虚构的例子,但是想象一下servlet应用程序和某人向共享fork连接池提交了一个长时间运行的任务。

public class ParallelTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService es = Executors.newCachedThreadPool();

        es.execute(() -> runTask(1000)); //incorrect task
        es.execute(() -> runTask(0));
        es.execute(() -> runTask(0));
        es.execute(() -> runTask(0));
        es.execute(() -> runTask(0));
        es.execute(() -> runTask(0));


        es.shutdown();
        es.awaitTermination(60, TimeUnit.SECONDS);
    }

    private static void runTask(int delay) {
        range(1, 1_000_000).parallel().filter(ParallelTest::isPrime).peek(i -> Utils.sleep(delay)).max()
                .ifPresent(max -> System.out.println(Thread.currentThread() + " " + max));
    }

    public static boolean isPrime(long n) {
        return n > 1 && rangeClosed(2, (long) sqrt(n)).noneMatch(divisor -> n % divisor == 0);
    }
}

我正在学习iOS的并发编程。到目前为止,我已经了解了NSOperation/NSOperationQueue和GCD。为什么在GCD上使用NSOperationQueue,反之亦然?

听起来好像GCD和NSOperationQueue都从用户那里抽象出了nsthread的显式创建。然而,这两种方法之间的关系对我来说并不清楚,所以任何反馈都很感激!

有几次,我因为建议使用以下方法而受到批评:

setPreferredSize setMinimumSize setMaximumSize

Swing组件。当我想要定义显示的组件之间的比例时,我没有看到任何替代它们的使用。有人告诉我:

对于布局,答案总是一样的:使用合适的 LayoutManager

我在网上搜索了一下,但我还没有找到任何关于这个主题的全面分析。我有以下几个问题:

Should I completely avoid the use of those methods? The methods have been defined for a reason. So when should I use them? In which context? For what purposes? What exactly are the negative consequences of using those methods? (I can only think adding portability between systems with different screen resolution). I don't think any LayoutManager can exactly satisfy all desired layout needs. Do I really need to implement a new LayoutManager for every little variation on my layout ? If the answer to 4 is "yes", won't this lead to a proliferation of LayoutManager classes which will become difficult to maintain? In a situation where I need to define proportions between children of a Component (eg, child1 should use 10% of space, child2 40% ,child3 50%), is it possible to achieve that without implementing a custom LayoutManager?

我听说过这些与并发编程有关的词,但是锁、互斥量和信号量之间有什么区别呢?

我有一个映射,这是由几个线程并发修改。

在Java API中似乎有三种不同的同步Map实现:

哈希表 collections . synchronizedmap(地图) ConcurrentHashMap

根据我的理解,Hashtable是一个旧的实现(扩展了过时的Dictionary类),后来为了适应Map接口而进行了调整。虽然它是同步的,但它似乎有严重的可伸缩性问题,不推荐用于新项目。

那另外两个呢?Collections.synchronizedMap(Map)和ConcurrentHashMaps返回的Map之间有什么区别?哪一种适合哪种情况?

HashSet基于HashMap。

如果我们查看HashSet<E>实现,所有内容都在HashMap<E,Object>下管理。

<E>用作HashMap的键。

我们知道HashMap不是线程安全的。这就是为什么我们在Java中有ConcurrentHashMap。

基于此,我很困惑,为什么我们没有一个应该基于ConcurrentHashMap的ConcurrentHashSet ?

我还遗漏了什么吗?我需要在多线程环境中使用Set。

此外,如果我想创建自己的ConcurrentHashSet,我可以通过将HashMap替换为ConcurrentHashMap并保留其余部分来实现它吗?

众所周知,JavaScript在所有现代浏览器实现中都是单线程的,但这是在任何标准中指定的还是只是传统?假设JavaScript总是单线程的,这是完全安全的吗?

这些技术之间的核心架构差异是什么?

另外,哪些用例通常更适合每种用例?

互斥是一种编程概念,经常用于解决多线程问题。我对社区的问题是:

什么是互斥锁,如何使用它?