解答例 - 実習課題2 - 4.スレッドの制御
(実習課題2)
2章の実習課題2のプログラムに以下の機能を追加しなさい。
- 一定時間内に終了しないソーティングスレッドを強制終了させる機能。
- 処理時間の管理はソーティングスレッド以外で行う事。
- また強制終了までの時間はプログラムの実行時の引数で指定できるようにする事。
解答例
/**
* BubbleSortImp.java
* TECHSCORE Java マルチスレッドプログラミング4章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.thread.chapter4.exercise2;
public class BubbleSortImp implements Runnable {
private int[] array = new int[ThreadSpeadExample.LENGTH];
private boolean stop = false;
public void stopRunning() {
stop = true;
}
public void run() {
System.out.println("バブルソート開始");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
//制限時間チェック
if (stop) {
System.out.println("bubblesort:Time Over");
//強制終了
return;
}
}
}
System.out.println("バブルソート終了");
//正常終了
ThreadSpeadExample.finish();
}
public void setArray(int[] array) {
this.array = array;
}
}
/**
* ChoiceSortImp.java
* TECHSCORE Java マルチスレッドプログラミング4章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.thread.chapter4.exercise2;
public class ChoiceSortImp implements Runnable {
private int[] array = new int[ThreadSpeadExample.LENGTH];
private boolean stop = false;
public void stopRunning() {
stop = true;
}
public void run() {
System.out.println("選択ソート開始");
for (int i = 0; i < array.length; i++) {
int min = i;
for (int j = i + 1; j < array.length; j++) {
if (array[min] > array[j]) {
min = j;
}
//制限時間チェック
if (stop) {
System.out.println("choicesort:Time Over");
//強制終了
return;
}
}
if (min != i) {
int tmp = array[i];
array[i] = array[min];
array[min] = tmp;
}
}
System.out.println("選択ソート終了");
//正常終了
ThreadSpeadExample.finish();
}
public void setArray(int[] array) {
this.array = array;
}
}
/**
* ThreadSpeadExample.java
* TECHSCORE Java マルチスレッドプログラミング4章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.thread.chapter4.exercise2;
import java.util.Random;
public class ThreadSpeadExample {
public static final int LENGTH = 20000;
private static boolean running = true;
public static void finish() {
running = false;
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("強制終了までの時間をミリ秒で指定してください");
} else {
long limit = Integer.parseInt(args[0]);
ChoiceSortImp choice = new ChoiceSortImp();
BubbleSortImp bubble = new BubbleSortImp();
//選択ソートスレッド
Thread choiceThread = new Thread(choice);
//バブルソートスレッド
Thread bubbleThread = new Thread(bubble);
//同じ値を持つ配列を作る
int[] array1 = new int[LENGTH];
int[] array2 = new int[LENGTH];
for (int i = 0; i < LENGTH; i++) {
array1[i] = new Random().nextInt(1000); //1000以下のランダムな値を代入
array2[i] = array1[i]; //同じ値をarray2にも代入
}
//配列をセットする
choice.setArray(array1);
bubble.setArray(array2);
//ソート開始時間
long start = System.currentTimeMillis();
//よーい、どん!
bubbleThread.start();
choiceThread.start();
//現在時間
long now = 0;
while (running) {
now = System.currentTimeMillis();
//現在時間が制限時間を超えると強制終了する
if (now > limit + start) {
choice.stopRunning();
bubble.stopRunning();
break;
}
}
}
}
}

