生活随笔
收集整理的這篇文章主要介紹了
java-多线程知识
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
幾個排序算法比較
- 采用多線程實現幾個排序算法,比較各個排序算法的優劣;
- java實現,一個主類,多個內部排序算法進程的接口,涉及到進程間的通信,因為每個進程包含自己的儲存空間,無法直接訪問其他線程的數據,可以在創建線程時,傳入數據;
package app;import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Thread;class AppFrame extends JFrame
{//主類數據變量int[] Array = new int[20000]; //待排序數組Thread[] thread = new Thread[6];//進程數組,用于存儲進程//構造函數public AppFrame() {thread[0] = new BubbleSortThread(0);thread[1] = new QuickSortThread(1);thread[2] = new SelectSortThread(2);thread[3] = new InsertSortThread(3);thread[4] = new HeapSortThread(4);thread[5] = new MergSortThread(5); }public void ThreadStart{for(int i=0;i<thread.length;i++){thread[i].start();//啟動線程}}//內部類,各類排序算法線程class BubbleSortThread extends Thread implements Runnable{private int key; //指定類線程在thread數組中的存儲位置public BubbleSortThread(int k){key = k;}public void run() //為每個進程重寫run功能{//冒泡算法實現int[] bubble= new int[20000];System.arraycopy(Array, 0, bubble, 0, 20000);boolean exchange = true;for (int i = 1; i < bubble.length && exchange; i++) {exchange = false;for (int j = 0; j < bubble.length - i; j++) {if (bubble[j] > bubble[j + 1]) {int temp = bubble[j];bubble[j] = bubble[j + 1];bubble[j + 1] = temp;exchange = true;}}}}}//快速排序算法進程實現class QuickSortThread extends Thread implements Runnable {private int key;public QuickSortThread(int k) {key = k;}private void quickSort(int[] table, int low, int hight) {if (low < hight) {int i = low;int j = hight;int vot = table[i];while (i != j) {while (i < j && vot <= table[j]) {j--;}if (i < j) {table[i] = table[j];i++;}while (i < j && table[i] < vot) {i++;}if (i < j) {table[j] = table[i];j--;}}table[i] = vot;quickSort(table, low, j - 1);quickSort(table, i + 1, hight);}}public void run() {int[] table = new int[20000];System.arraycopy(Array, 0, table, 0, 20000);quickSort(table, 0, table.length - 1);}}//選擇排序class SelectSortThread extends Thread implements Runnable {private int key;public SelectSortThread(int k) {key = k;}public void run() {int[] table = new int[20000];System.arraycopy(Array, 0, table, 0, 20000);for (int i = 0; i < table.length - 1; i++) {int min = i;for (int j = i + 1; j < table.length; j++) {if (table[j] < table[min])min = j;if (min != i) {int temp = table[i];table[i] = table[min];table[min] = temp;}}}}}//插入排序class InsertSortThread extends Thread implements Runnable {private int key;public InsertSortThread(int k) {key = k;}public void run() {int[] table = new int[20000];System.arraycopy(Array, 0, table, 0, 20000);for (int i = 1; i < table.length; i++) {int temp = table[i];int j;for (j = i - 1; j > -1 && temp < table[j]; j--) {table[j + 1] = table[j];}table[j + 1] = temp;}}}//小堆排序class HeapSortThread extends Thread implements Runnable{private int key;public HeapSortThread(int k) {key = k;}private void sift(int[] table, int low, int hight) {int i = low;int j = 2 * i + 1;int temp = table[i];while (j <= hight) {if (j < hight && table[j] > table[j + 1])j++;if (temp > table[j]) {table[i] = table[j];i = j;j = 2 * i + 1;} elsej = hight + 1;}table[i] = temp;}public void run() {int[] table = new int[20000];System.arraycopy(Array, 0, table, 0, 20000);int n = table.length;for (int j = n / 2 - 1; j >= 0; j--) {sift(table, j, n - 1);}for (int j = n - 1; j > 0; j--) {int temp = table[0];table[0] = table[j];table[j] = temp;sift(table, 0, j - 1);}}}//歸并排序class MergSortThread extends Thread implements Runnable {private int key;public MergSortThread(int k) {key = k;}private void merg(int[] x, int[] y, int m, int r, int n) {int i = m, j = r, k = m;while (i < r && j < r + n && j < x.length) {if (x[i] < x[j]) {y[k++] = x[i++];}else {y[k++] = x[j++];}while (i < r) {y[k++] = x[i++];}while (j < r + n && j < x.length) {y[k++] = x[j++];}}}private void mergepass(int[] x, int[] y, int n) {int i = 0;while (i < x.length - 2 * n + 1) {merg(x, y, i, i + n, n);i += 2 * n;}if (i + n < x.length) {merg(x, y, i, i + n, n);} else {for (int j = i; j < x.length; j++) {y[j] = x[j];}}}public void run() {int[] table = new int[20000];System.arraycopy(Array, 0, table, 0, 20000);int n = 1;int[] y = new int[table.length];do {mergepass(table, y, n);n *= 2;if (n < table.length) {mergepass(y, table, n);n *= 2;}} while (n < table.length);}}
}public class App {public static void main(String[] args) throws Exception {AppFrame appframe=new AppFrame();appframe.setVisible(true);appframe.ThreadStart();//同時啟動線程運行}
}
- 這指示一個基本框架思路,涉及到一些知識,旨在學習。
總結
以上是生活随笔為你收集整理的java-多线程知识的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。