| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Java 並行処理を行う例

提供: MyMemoWiki
2020年2月16日 (日) 04:27時点におけるPiroto (トーク | 投稿記録)による版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動


Java 並行処理を行う例

Java |

並行処理コード抜粋

  1. package info.typea.checker;
  2.  
  3. import info.typea.checker.SourceCodeAnalyzer;
  4. import info.typea.checker.SourceCodeParser;
  5. import info.typea.checker.JavaProgram;
  6. import info.typea.checker.checkcase.CheckCase;
  7. import info.typea.checker.checkcase.FormatCheckCase;
  8. import info.typea.checker.checkcase.InvalidSyntaxCheckCase;
  9.  
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.FilenameFilter;
  13. import java.io.OutputStream;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Date;
  18. import java.util.List;
  19. import java.util.concurrent.Callable;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.Executors;
  22. import java.util.concurrent.Future;
  23.  
  24. /**
  25. * 並行処理でJavaソースコードをチェックする
  26. */
  27. public class SourceCodeChecker {
  28.  
  29. private static final int THREAD_SIZE = 8; // スレッドプールのサイズ
  30. private ExecutorService threadPool; // スレッドプール
  31.  
  32. /**
  33. * @param args
  34. * @throws Exception
  35. */
  36. public static void main(String[] args) throws Exception {
  37. if (args.length < 1) {
  38. System.out.println(usage());
  39. System.exit(-1);
  40. }
  41. String dirName = args[0];
  42. SourceCodeChecker me = new SourceCodeChecker();
  43. me.check(new File(dirName));
  44. System.out.println("終了しました");
  45. }
  46. /**
  47. * @param dir 対象ディレクトリ
  48. * @throws Exception
  49. */
  50. public void check(File dir) throws Exception {
  51. if (!dir.exists() || !dir.isDirectory()) {
  52. throw new IllegalArgumentException("対象ディレクトリが不正です");
  53. }
  54.  
  55. // 出力先ディレクトリ作成
  56. SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getInstance();
  57. sdf.applyPattern("yyyyMMdd_hhmmss");
  58. String outdirName = dir.getAbsolutePath() + File.separator + "out_" + sdf.format(new Date());
  59. File outdir = new File(outdirName);
  60. if (!outdir.exists()) {
  61. if (!outdir.mkdirs()) {
  62. throw new IllegalStateException("出力先ディレクトリが作成できません " + outdirName);
  63. }
  64. }
  65. // ソースコードに対してチェックするタスクを追加する
  66. List<CheckCase> cases = new ArrayList<CheckCase>();
  67. cases.add(new FormatCheckCase());
  68. cases.add(new InvalidSyntaxCheckCase());
  69. // 対象ファイルを拡張子で判断
  70. File[] files = dir.listFiles(new FilenameFilter(){
  71. public boolean accept(File dir, String name) {
  72. return name.toLowerCase().endsWith(".py") || name.toLowerCase().endsWith(".java");
  73. }});
  74.  
  75. // ************* ここから並行処理 *******************
  76. threadPool = Executors.newFixedThreadPool(THREAD_SIZE);
  77. List<CheckTask> tasks = new ArrayList<CheckTask>();
  78. for (File file : files) {
  79. tasks.add(new CheckTask(file, outdir, cases));
  80. }
  81.  
  82. // すべてのタスクの完了を待つ
  83. List<Future<Long>> result = threadPool.invokeAll(tasks);
  84. for (Future<Long>: result) {
  85. System.out.printf("終了しました... %d\n" , f.get());
  86. }
  87. // ************* ここまで並行処理 *******************
  88. }
  89. public static class CheckTask implements Callable<Long> {
  90. private File file;
  91. private File outdir;
  92. List<CheckCase> cases;
  93. /**
  94. *
  95. */
  96. public CheckTask(File file, File outdir, List<CheckCase> cases) {
  97. this.file = file;
  98. this.outdir = outdir;
  99. this.cases = cases;
  100. }
  101. /* (non-Javadoc)
  102. * @see java.util.concurrent.Callable#call()
  103. */
  104. public Long call() throws Exception {
  105. System.out.printf("処理開始... %s\n", file.getName());
  106. File outfile = new File(outdir.getAbsolutePath() + File.separator + file.getName() + "_report.txt");
  107. OutputStream out = new FileOutputStream(outfile);
  108. SourceCodeParser parser = new SourceCodeParser();
  109. JavaProgram pgm = parser.parse(file);
  110. SourceCodeAnalyzer analyzer = new SourceCodeAnalyzer(pgm);
  111. long ret = analyzer.analyze(cases);
  112. pgm.printSource(out);
  113. out.close();
  114. return new Long(ret);
  115. }
  116. }
  117. public static String usage() {
  118. return "java SourceCodeChecker target directory";
  119. }
  120. }