天天看點

1. 将插入、冒泡排序算法設計成線程,啟動兩個以上不同的線程同時運作,計算不同排序 的運作時間。

實驗十 多線程
實驗目的
1.線程的概念、線程的生命周期。
2.多線程的程式設計:繼承 Thread 類與使用 Runnable 接口。
主要儀器裝置及耗材
安裝了 JDK1.8 的 PC 一台
實驗内容
1. 将插入、冒泡排序算法設計成線程,啟動兩個以上不同的線程同時運作,計算不同排序
的運作時間。      
package com.temp;

import java.util.Random;

/**
 * @Author lanxiaofang
 * @email [email protected]
 * @date 2020/11/27 19:57
 * 1. 将插入、冒泡排序算法設計成線程,啟動兩個以上不同的線程同時運作,計算不同排序的運作時間。
 */
public class RimingTimeOfAlgorithm extends Thread {
    
    private static final int Length = 10000;
    private static int array[] = new int[Length];
    private long startTime, endTime;

    public RimingTimeOfAlgorithm(String name) {
        super(name);
    }

    public static void main(String[] args) {
        Random random = new Random(System.nanoTime());
        for (int i = 0; i < Length; i++) {
            array[i] = random.nextInt(Length);
            System.out.print(" " + array[i]);
        }
        System.out.println("\n------------随機數輸出完畢---------------");

        RimingTimeOfAlgorithm rtoa1 = new RimingTimeOfAlgorithm("Thread 1");
        RimingTimeOfAlgorithm rtoa2 = new RimingTimeOfAlgorithm("Thread 2");
        RimingTimeOfAlgorithm rtoa3 = new RimingTimeOfAlgorithm("Thread 3");
        RimingTimeOfAlgorithm rtoa4 = new RimingTimeOfAlgorithm("Thread 4");
        RimingTimeOfAlgorithm rtoa5 = new RimingTimeOfAlgorithm("Thread 5");
        RimingTimeOfAlgorithm rtoa6 = new RimingTimeOfAlgorithm("Thread 6");

        rtoa1.bubble();
        rtoa2.insert();
        rtoa3.bubble();
        rtoa4.insert();
        rtoa5.bubble();
        rtoa6.insert();

    }

    void bubble() {
        BubbleSort bubbleSort = new BubbleSort();
        bubbleSort.start();
    }

    void insert() {
        InsertSort insertSort = new InsertSort();
        insertSort.start();
    }

    class BubbleSort extends Thread {
        @Override
        public void run() {
            startTime = System.currentTimeMillis();
            for (int i = 2; i < Length; i++) {
                for (int j = 1; j < Length - i; j++) {
                    if (array[j] > array[j + 1]) {
                        int c = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = c;
                    }
                }
            }
            endTime = System.currentTimeMillis();
            System.out.println("#### BubbleSort takes " + (endTime - startTime) + "ms");
        }
    }

    class InsertSort extends Thread {
        @Override
        public void run() {
            startTime = System.currentTimeMillis();
            for (int i = 1; i < Length; i++) {
                int c = array[i];
                int j;
                for (j = i - 1; j >= 0; j--) {
                    if (array[j] > c)
                        array[j + 1] = array[j];
                    else
                        break;
                }
                array[j + 1] = c;
            }
            endTime = System.currentTimeMillis();
            System.out.println("#### InsertSort takes " + (endTime - startTime) + "ms");
        }
    }


}