Java Multi-thread Application

Java Multi-thread Application

Sudoku Solution Validator

image-20201113092616307

image-20201113092704268

For this question, we create the 11 threads to solve. Actually, to further disassembly tasks, we could use 27 threads to finish the work(Also thread MyThread1 , MyThread2, MyThread3).

In MyThread1 and MyThread2, I create a hashMap to find the number is in 1~9 and once, if more than once the flag would be false and it meas bad Sudoku.

In MyThread3, I let it sum over the array and use hashSet to check to see if it only happens once.

this is the code:

package com.company;

import java.util.*;

//任务拆解:
//1、生成正确的测试用例 9*9
//2、创建对应的线程来判断
public class SudokuCheck {
    public static int[][] correct_array;
    public static int[][] error_array;

    public static void main(String[] args) throws InterruptedException {
        correct_array = new int[][]{
                {1, 2, 3, 4, 5, 6, 7, 8, 9},
                {4, 5, 6, 7, 8, 9, 1, 2, 3},
                {7, 8, 9, 1, 2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6, 7, 8, 9, 1},
                {5, 6, 7, 8, 9, 1, 2, 3, 4},
                {8, 9, 1, 2, 3, 4, 5, 6, 7},
                {3, 4, 5, 6, 7, 8, 9, 1, 2},
                {6, 7, 8, 9, 1, 2, 3, 4, 5},
                {9, 1, 2, 3, 4, 5, 6, 7, 8}
        };
        error_array = new int[][]{
                {1, 2, 3, 4, 5, 6, 7, 8, 9},
                {4, 5, 6, 7, 8, 9, 1, 2, 3},
                {7, 8, 9, 1, 2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6, 7, 8, 9, 1},
                {5, 6, 7, 8, 9, 1, 2, 3, 4},
                {8, 9, 1, 2, 3, 4, 5, 6, 7},
                {3, 4, 5, 6, 7, 8, 9, 1, 2},
                {6, 7, 8, 9, 1, 1, 3, 4, 5},
                {9, 7, 2, 3, 4, 5, 6, 7, 8}
        };
       // testArray(correct_array);
        testArray(error_array);

        //MyThread a = new MyThread("Thread A",a)
    }

    static void  testArray(int[][] test_array) throws InterruptedException {
        MyThread1 thread1 = new MyThread1("Thread A",test_array);
        MyThread2 thread2 = new MyThread2("Thread B",test_array);
        MyThread3[] thread3s = new MyThread3[9];
        int order = 0;
        for(int i = 0;i < test_array.length;i+=3){
            for(int j = 0;j < test_array[0].length;j+=3){
                int m = i;
                int n = j;
                int[] my_array = new int[]{
                        test_array[m][n],test_array[m+1][n],test_array[m+2][n],
                        test_array[m][n+1],test_array[m+1][n+1],test_array[m+2][n+1],
                        test_array[m][n+2],test_array[m+1][n+2],test_array[m+2][n+2]
                };
                thread3s[order]=new MyThread3("Thread C"+order,my_array);
                order++;
            }
        }
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        for(int i = 0;i < 9; i++){
            thread3s[i].start();
        }
        for(int i = 0;i < 9; i++){
            thread3s[i].join();
            System.out.println(thread3s[i].flag+"  "+thread3s[i].name);
        }
        System.out.println(thread1.flag+"  "+thread1.name);
        System.out.println(thread2.flag+"  "+thread2.name);


    }

    static class MyThread1 extends Thread {
        private HashMap<Integer, Boolean> hashMap = new HashMap<>();
        private String name;
        public int[][] array;
        public boolean flag = true;

        public MyThread1(String name, int[][] array) {
            this.name = name;
            this.array = array;
            for(int i = 1; i <=9;i++)
                hashMap.put(i,false);
        }

        public void run() {
            //Arrays.sort(array);
            for(int[] j: array) {
                for(int i = 1; i <=9;i++)
                    hashMap.put(i,false);
                for (int i : j) {
                    //System.out.print(i + "  ");
                    if (hashMap.containsKey(i)) {
                        if (hashMap.get(i))
                            flag = false;
                        //if the value is true,
                        // it means it is more than one time in the array.
                        hashMap.put(i, true);
                    } else
                        flag = false;
                }
                //System.out.println(name);
            }
        }
    }
    static class MyThread2 extends Thread{
        private HashMap<Integer, Boolean> hashMap = new HashMap<>();
        private String name;
        public int[][] array;
        public boolean flag = true;

        public MyThread2(String name, int[][] array) {
            this.name = name;
            this.array = array;
            for(int i = 1; i <=9;i++)
                hashMap.put(i,false);
        }

        public void run() {
            //Arrays.sort(array);
            for(int i = 0;i <array[0].length; i++) {
                for(int t = 1; t <=9;t++)
                    hashMap.put(t,false);
                for (int j = 0; j < array.length; j++) {
                    if (hashMap.containsKey(array[j][i])) {
                        if (hashMap.get(array[j][i]))
                            flag = false;
                        //if the value is true,
                        // it means it is more than one time in the array.
                        hashMap.put(array[j][i], true);
                    } else
                        flag = false;
                }
            }
        }
    }
    static class MyThread3 extends Thread{
        private String name;
        private int[] array;
        private HashSet<Integer> hashSet;
        public boolean flag = true;
        public void run(){
            int sum = 0;
            for(int i = 0;i < array.length;i++) {
                if (!hashSet.contains(array[i])){
                    sum+=array[i];
                    hashSet.add(array[i]);
                }
                else {
                    flag = false;
                }
            }
            if(sum != 45)
                flag = false;
        }
        public MyThread3(String name,int[] array){
            this.name = name;
            this.array = array;
            hashSet = new HashSet<>();
        }
    }

}
test case 1:

image-20201113091742923

test case 2:

image-20201113091653302

Multithreaded Sorting Application

image-20201113092735355

Our group use Java to finish this exiperment by design two thread which name A, B to indenpently sort the array, then the sorted array would sort again.

This is the code:

package com.company;

import java.util.Arrays;

class Thread1 extends Thread{
    private String name;
    public int[] array;
    public Thread1(String name,int[] array) {
        this.name=name;
        this.array = array;
    }
    public void run() {
        Arrays.sort(array);
    for(int i:array)
        System.out.println(i+"  Thread: "+name);
    }
}
public class MultiSort  {
    static int[] test = new int[10];
    public static void main(String[] args) {
        for(int i = 0;i <test.length;i++){
            test[i] = (int) (Math.random()*10);
        }
//        for(int i :test)
//            System.out.println(i);
        Thread1 mTh1=new Thread1("A",Arrays.copyOfRange(test,0,5));
        // create Thread A and B
        Thread1 mTh2=new Thread1("B",Arrays.copyOfRange(test,5,10));
        mTh1.start();
        mTh2.start();
        try
        {
            mTh1.join();
            mTh2.join();
            //wait the thread
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        int[] test1 = new int[10];
        System.arraycopy(mTh1.array, 0, test1, 0, 5);
        //let the test1 be the finish array
        merge(test1,5,mTh2.array,5);
    }
    static void merge(int[] nums1, int m, int[] nums2, int n) {
        // Make a copy of nums1.
        int [] nums1_copy = new int[m];
        System.arraycopy(nums1, 0, nums1_copy, 0, m);

        // Two get pointers for nums1_copy and nums2.
        int p1 = 0;
        int p2 = 0;

        // Set pointer for nums1
        int p = 0;

        // Compare elements from nums1_copy and nums2
        // and add the smallest one into nums1.
        while ((p1 < m) && (p2 < n))
            nums1[p++] = (nums1_copy[p1] < nums2[p2]) ? nums1_copy[p1++] : nums2[p2++];

        // if there are still elements to add
        if (p1 < m)
            System.arraycopy(nums1_copy, p1, nums1, p1 + p2, m + n - p1 - p2);
        if (p2 < n)
            System.arraycopy(nums2, p2, nums1, p1 + p2, m + n - p1 - p2);
        System.out.print("I finish the merge sort :");
        for(int i :nums1)
            System.out.print(i+"   ");
    }
}

test case 1:

image-20201112121229360

test case 2:

image-20201112121256596

Conclusion

From the two exiperiment , we learn the java multi-thread applycation, and review how to life of thread and the API,such as start(), run(), join() and so on.

Also for the sorting application, it almost like a Merge sort and use two thread to finish.

The code still have some places to improve, like pass the array to the thread, we write it too simple and crude.

原文地址:https://www.cnblogs.com/buzhouke/p/13967590.html