Skip to content

Instantly share code, notes, and snippets.

@lovelock
Created February 8, 2021 14:19
Show Gist options
  • Save lovelock/9286565cab4e168645f3d25cd5086b45 to your computer and use it in GitHub Desktop.
Save lovelock/9286565cab4e168645f3d25cd5086b45 to your computer and use it in GitHub Desktop.
一个将要改写成并发程序的demo
package fun.happyhacker;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author Frost Wong <[email protected]>
* @version 2021/2/8
**/
public class CompletableFutureTest {
public static void main(String[] args) throws InterruptedException {
CompletableFutureTest completableFutureTest = new CompletableFutureTest();
completableFutureTest.compose();
}
private List<Target> compose() throws InterruptedException {
List<Long> ids = genIds();
Map<Long, String> contentMap = fun1(ids);
Map<Long, List<String>> aMap = fun2(ids);
Map<Long, String> bMap = hash(contentMap);
List<Target> list = new ArrayList<>();
for (Long id : ids) {
Target target = new Target();
target.setId(id);
target.setContent(contentMap.get(id));
target.setList(aMap.get(id));
target.setMd5(bMap.get(id));
}
return list;
}
private List<Long> genIds() {
return Collections.emptyList();
}
private Map<Long, String> fun1(List<Long> ids) throws InterruptedException {
Map<Long, String> result = new HashMap<>();
for (Long id : ids) {
TimeUnit.MILLISECONDS.sleep(100);
result.put(id, id.toString());
}
return result;
}
private Map<Long, List<String>> fun2(List<Long> ids) throws InterruptedException {
Map<Long, List<String>> result = new HashMap<>();
for (Long id : ids) {
TimeUnit.MILLISECONDS.sleep(100);
result.put(id, List.of(id.toString(), id.toString()));
}
return result;
}
private Map<Long, String> hash(Map<Long, String> map) throws InterruptedException {
Map<Long, String> result = new HashMap<>();
for (Map.Entry<Long, String> entry : map.entrySet()) {
TimeUnit.MILLISECONDS.sleep(100);
result.put(entry.getKey(), DigestUtils.md5Hex(entry.getValue()));
}
return result;
}
}
@Data
class Target {
private Long id;
private String content;
private String md5;
private List<String> list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment