小编yor*_*eta的帖子

拆分java.util.stream.Stream

我有一个包含URL和电子邮件的文本文件.我需要从文件中提取所有这些内容.每个URL和电子邮件可以找到一次以上,但结果不应包含重复项.我可以使用以下代码提取所有URL:

Files.lines(filePath).
    .map(urlPattern::matcher)
    .filter(Matcher::find)
    .map(Matcher::group)
    .distinct();
Run Code Online (Sandbox Code Playgroud)

我可以使用以下代码提取所有电子邮件:

Files.lines(filePath).
    .map(emailPattern::matcher)
    .filter(Matcher::find)
    .map(Matcher::group)
    .distinct();
Run Code Online (Sandbox Code Playgroud)

我是否可以提取所有Files.lines(filePath)只读取一次返回的流的URL和电子邮件?类似于将行流分割为URL流和电子邮件流的东西.

java java-8 java-stream

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

在此代码中使用memory_order_relaxed是否正确?

std::memory_order_relaxed在以下代码中使用是否正确?

#include <atomic>
#include <array>
#include <iostream>
#include <thread>

using namespace std;

int main() {
    std::atomic<int> counter(0);

    constexpr size_t thread_count = 10;
    std::array<std::thread, thread_count> threads;
    for (auto& th : threads) {
        th = std::thread([&counter]() {
            for (int j = 0; j < 100; ++j) {
/*1*/           counter.fetch_add(1, std::memory_order_relaxed);
            }
        });
    }

    for (auto& th : threads) th.join();

/*2*/ std::cout << counter.load(std::memory_order_relaxed) << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有一个方法调用计数器.计数器何时实际递增(/*1*/)无关紧要,如果它会增加一段时间就足够了.但是当我调用atomic::load(/*2*/)时,所有已经进行的计数器更改都应该是可见的.

它是正确使用std::memory_order_relaxed的线路/*1*//*2*/ …

c++ concurrency atomic c++11

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

标签 统计

atomic ×1

c++ ×1

c++11 ×1

concurrency ×1

java ×1

java-8 ×1

java-stream ×1