我发现的大多数归并排序示例都在单个线程中运行。这首先就抵消了使用合并排序算法的一些优势。有人可以展示使用多线程在 java 中编写合并排序算法的正确方法吗?
该解决方案应使用最新版本的 java 的适用功能。Stackoverflow 上已有的许多解决方案都使用普通线程。我正在寻找一个演示 ForkJoin 与 RecursiveTask 的解决方案,这似乎是 RecursiveTask 类的主要用例。
重点应该是展示一种具有卓越性能特征的算法,包括尽可能的时间和空间复杂度。
注意:所提议的重复问题都不适用,因为两者都没有提供使用递归任务的解决方案,而这正是该问题所要求的。
我正在创建自己的自定义身份验证提供程序。现在我只是在检查静态用户名和密码,但稍后这将被替换为更高级的东西,所以虽然我不需要在这种情况下使用自定义提供程序,但它对我没有多大帮助,因为它只是基础工作我还没有添加的附加代码。
话虽如此,这是我的代码处于损坏状态。
我的自定义身份验证提供程序:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class SatAuthenticationProvider implements AuthenticationProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class);
public SatAuthenticationProvider() {
LOGGER.info("*** CustomAuthenticationProvider created");
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
LOGGER.info("*** Creating authentication");
if (authentication.getName().equals("test") && authentication.getCredentials().equals("test")) {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("USER")); …
Run Code Online (Sandbox Code Playgroud) 所以我正在尝试学习haskell,在玩Maybe类型时,我想出了这个简单的代码片段
import Data.Maybe
betterDouble :: Maybe Int -> Maybe Int
betterDouble x =
case x of
Just y -> Just (y * 2)
Nothing -> Nothing
Run Code Online (Sandbox Code Playgroud)
这看起来很笨拙而且冗长.我不能帮助,但觉得有更简洁的方式在haskell写这个.什么是惯用或简洁的方法,我可以重写该代码块?
我正在尝试编写一个使用Akka Streams的简单介绍示例.我试图基本上创建一个流,它将一系列整数作为源并过滤掉所有非素数的整数,产生一个素数整数流作为其输出.
构造流的类相当简单; 为此,我有以下.
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.javadsl.Flow;
import com.aparapi.Kernel;
import com.aparapi.Range;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class PrimeStream {
private final AverageRepository averageRepository = new AverageRepository();
private final ActorSystem actorSystem;
public PrimeStream(ActorSystem actorSystem) {
this.actorSystem = actorSystem;
}
public Flow<Integer, Integer, NotUsed> filterPrimes() {
return Flow.of(Integer.class).grouped(10000).mapConcat(PrimeKernel::filterPrimes).filter( v -> v != 0);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行以下测试时,它工作正常.
private final ActorSystem actorSystem = ActorSystem.create("Sys");
@Test
public void testStreams() {
Flow<Integer, Integer, NotUsed> filterStream = new PrimeStream(actorSystem).filterPrimes();
Source<Integer, NotUsed> flow …
Run Code Online (Sandbox Code Playgroud)