Skip to content

Instantly share code, notes, and snippets.

@xhiroga
Created December 27, 2017 21:24
Show Gist options
  • Save xhiroga/75c49c3bc3bfbcba29874b4f8b6538e4 to your computer and use it in GitHub Desktop.
Save xhiroga/75c49c3bc3bfbcba29874b4f8b6538e4 to your computer and use it in GitHub Desktop.
20171227.初めてのマルチスレッド@jjugナイトセミナー プレゼン用ソースコード
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class IOByExec implements Runnable{
private ExecutorService exec = Executors.newCachedThreadPool();
private String name;
private int count;
public IOByExec(String name, int count){
this.name = name;
this.count = count;
}
@Override
public void run(){
try{
File f = new File(name);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
for (int i=1; i<=count; i++){
pw.print(emojis.get((int)(Math.random()*189)));
if(i%100==0){pw.println();}
}
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void doExecute(){
for (String name: Arrays.asList("temp1","temp2","temp3","temp4","temp5")){
exec.submit(new IOByExec(name, count));
};
exec.shutdown();
while(!exec.isShutdown()){}
return;
}
private List<String> emojis = Arrays.asList(
"😄","😃","😀","😊","☺","😉","😍","😘","😚","😗","😙",
"😜","😝","😛","😳","😁","😔","😌","😒","😞","😣","😢",
"😂","😭","😪","😥","😰","😅","😓","😩","😫","😨","😱",
"😠","😡","😤","😖","😆","😋","😷","😎","😴","😵","😲",
"😟","😦","😧","😈","👿","😮","😬","😐","😕","😯","😶",
"😇","😏","😑","👲","👳","👮","👷","💂","👶","👦","👧",
"👨","👩","👴","👵","👱","👼","👸","😺","😸","😻","😽",
"😼","🙀","😿","😹","😾","👹","👺","🙈","🙉","🙊","💀",
"👽","💩","🔥","✨","🌟","💫","💥","💢","💦","💧","💤",
"💨","👂","👀","👃","👅","👄","👍","👎","👌","👊","✊",
"✌","👋","✋","👐","👆","👇","👉","👈","🙌","🙏","☝",
"👏","💪","🚶","🏃","💃","👫","👪","👬","👭","💏","💑",
"👯","🙆","🙅","💁","🙋","💆","💇","💅","👰","🙎","🙍",
"🙇","🎩","👑","👒","👟","👞","👡","👠","👢","👕","👔",
"👚","👗","🎽","👖","👘","👙","💼","👜","👝","👛","👓",
"🎀","🌂","💄","💛","💙","💜","💚","❤","💔","💗","💓",
"💕","💖","💞","💘","💌","💋","💍","💎","👤","👥","💬",
"👣","💭"
);
public static void main(String[] args){
long start = System.nanoTime();
IOByExec io = new IOByExec("", Integer.valueOf(args[0]));
io.doExecute();
long end = System.nanoTime();
System.out.println(end - start);
}
}
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class IOByFJ extends RecursiveAction{
private final String name;
private int count;
public IOByFJ(String name, int count){
this.name = name;
this.count = count;
}
@Override
public void compute(){
try{
File f = new File(name);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
for (int i=1; i<=count; i++){
pw.print(emojis.get((int)(Math.random()*189)));
if(i%100==0){pw.println();}
}
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
private List<String> emojis = Arrays.asList(
"😄","😃","😀","😊","☺","😉","😍","😘","😚","😗","😙",
"😜","😝","😛","😳","😁","😔","😌","😒","😞","😣","😢",
"😂","😭","😪","😥","😰","😅","😓","😩","😫","😨","😱",
"😠","😡","😤","😖","😆","😋","😷","😎","😴","😵","😲",
"😟","😦","😧","😈","👿","😮","😬","😐","😕","😯","😶",
"😇","😏","😑","👲","👳","👮","👷","💂","👶","👦","👧",
"👨","👩","👴","👵","👱","👼","👸","😺","😸","😻","😽",
"😼","🙀","😿","😹","😾","👹","👺","🙈","🙉","🙊","💀",
"👽","💩","🔥","✨","🌟","💫","💥","💢","💦","💧","💤",
"💨","👂","👀","👃","👅","👄","👍","👎","👌","👊","✊",
"✌","👋","✋","👐","👆","👇","👉","👈","🙌","🙏","☝",
"👏","💪","🚶","🏃","💃","👫","👪","👬","👭","💏","💑",
"👯","🙆","🙅","💁","🙋","💆","💇","💅","👰","🙎","🙍",
"🙇","🎩","👑","👒","👟","👞","👡","👠","👢","👕","👔",
"👚","👗","🎽","👖","👘","👙","💼","👜","👝","👛","👓",
"🎀","🌂","💄","💛","💙","💜","💚","❤","💔","💗","💓",
"💕","💖","💞","💘","💌","💋","💍","💎","👤","👥","💬",
"👣","💭"
);
public static void main(String[] args){
long start = System.nanoTime();
ForkJoinPool pool = new ForkJoinPool();
for (String name: Arrays.asList("temp1","temp2","temp3","temp4","temp5")){
pool.invoke(new IOByFJ(name, Integer.valueOf(args[0])));
};
long end = System.nanoTime();
System.out.println(end-start);
}
}
import java.util.concurrent.*;
public class RecursiveByExec implements Callable<Integer>{
private final int n;
private ExecutorService exec = Executors.newCachedThreadPool();
public RecursiveByExec(int n){
this.n = n;
}
public Integer call() throws Exception{
if(n <= 1){
return n;
}
Future<Integer> f1 = exec.submit(new RecursiveByExec(n-1));
Future<Integer> f2 = exec.submit(new RecursiveByExec(n-2));
return f1.get() + f2.get();
}
public void doExecute(){
try{
Future<Integer> f = exec.submit(this);
System.out.println(f.get());
}catch(Exception e){
e.printStackTrace();
}finally{
exec.shutdown();
}
}
public static void main(String[] args){
long start = System.nanoTime();
RecursiveByExec rec = new RecursiveByExec(Integer.valueOf(args[0]));
rec.doExecute();
long end = System.nanoTime();
System.out.println(end-start);
}
}
import java.util.concurrent.*;
public class RecursiveByFJ extends RecursiveTask<Integer>{
private final int n;
public RecursiveByFJ(int n){
this.n = n;
}
@Override
public Integer compute(){
if(this.n <= 1){
return n;
}
RecursiveByFJ r1 = new RecursiveByFJ(n-1);
r1.fork();
RecursiveByFJ r2 = new RecursiveByFJ(n-2);
return r2.compute() + r1.join();
}
// フィボナッチ数列における任意番目の値を返す。
public static void main(String[] args){
long start = System.nanoTime();
ForkJoinPool pool = new ForkJoinPool();
System.out.println(pool.invoke(new RecursiveByFJ(Integer.valueOf(args[0]))));
long end = System.nanoTime();
System.out.println(end-start);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment