Created
October 5, 2015 00:41
-
-
Save marsyang1/89f3f4376f2aaf6b08b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mars; | |
import akka.actor.UntypedActor; | |
import com.google.common.base.Stopwatch; | |
import com.google.common.collect.Lists; | |
import lombok.extern.slf4j.Slf4j; | |
import org.apache.commons.lang3.RandomStringUtils; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.ExecutorCompletionService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.atomic.AtomicReference; | |
import java.util.stream.Collectors; | |
/** | |
* Hello world! | |
*/ | |
@Slf4j | |
public class App { | |
public static void main(String[] args) { | |
log.info("Hello World!"); | |
String question = "Test123"; | |
List<String> engines = Lists.newArrayList(); | |
for (int i = 0; i < 100; i++) { | |
engines.add(RandomStringUtils.randomAlphanumeric(10)); | |
} | |
engines.add(question); | |
Stopwatch stopwatch = Stopwatch.createStarted(); | |
String actual = getFirstResultByThread(question, engines); | |
stopwatch.stop(); | |
log.info("actual = " + actual); | |
} | |
private static String getFirstResultByThread(String question, List<String> engines) { | |
AtomicReference<String> result = new AtomicReference<>(); | |
for (String base : engines) { | |
new Thread(() -> { | |
// String url = base + question; | |
// result.compareAndSet(null, WS.url(url).get()); | |
if (base.equals(question)) { | |
result.compareAndSet(question, "Hello " + question); | |
} | |
}).start(); | |
} | |
while (result.get() == null) ; | |
return result.get(); | |
} | |
// private static String getFirstResultExecutors(String question, List<String> engines) { | |
// ExecutorCompletionService<String> service = new ExecutorCompletionService<>(Executors.newFixedThreadPool(4)); | |
// for (String base : engines) { | |
// String url = base + question; | |
// service.submit(() -> { | |
//// return WS.url(url).get(); | |
// if (base.equals(question)) { | |
// return question; | |
// } | |
// }); | |
// } | |
// try { | |
// return service.take().get(); | |
// } catch (InterruptedException | ExecutionException e) { | |
// return null; | |
// } | |
// } | |
// | |
// private static String getFirstResult(String question, List<String> engines) { | |
// Optional<String> result = engines.stream().parallel().map((base) -> { | |
// String url = base + question; | |
//// return WS.url(url).get(); | |
// }).findAny(); | |
// return result.get(); | |
// } | |
// | |
// private static String getFirstResultCompletableFuture(String question, List<String> engines) { | |
// CompletableFuture result = CompletableFuture.anyOf(engines.stream().map((base) -> { | |
// return CompletableFuture.supplyAsync((() -> { | |
// String url = base + question; | |
//// return WS.url(url).get(); | |
// })); | |
// } | |
// ).collect(Collectors.toList()).toArray(new CompletableFuture[0])); | |
// | |
// try { | |
// return (String) result.get(); | |
// } catch (InterruptedException | ExecutionException e) { | |
// return null; | |
// } | |
// } | |
static class UrlFetcher extends UntypedActor { | |
@Override | |
public void onReceive(Object message) throws Exception { | |
if (message instanceof Message) { | |
Message work = (Message) message; | |
// String result = WS.url(work.url).get(); | |
String result = ""; | |
getSender().tell(new Result(result), getSelf()); | |
} else { | |
unhandled(message); | |
} | |
} | |
} | |
static class Message { | |
String url; | |
Message(String url) { | |
this.url = url; | |
} | |
} | |
static class Result { | |
String html; | |
Result(String html) { | |
this.html = html; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.mars</groupId> | |
<artifactId>test-concurrency</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>test-concurrency</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.1.3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.16.6</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>18.0</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.typesafe.akka</groupId> | |
<artifactId>akka-actor_2.10</artifactId> | |
<version>2.3.14</version> | |
</dependency> | |
<dependency> | |
<groupId>com.typesafe.play</groupId> | |
<artifactId>play-ws_2.10</artifactId> | |
<version>2.4.3</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>${project.artifactId}</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment