Computing the sum of an array of 50 integers (primary multithreaded applications) - - Learning Notes

First, we define an integer array. For convenience, the code is as follows:

int[] a = new int[50];
		for (int i = 0; i < a.length; i++) {
			a[i] = i;
		}

2. Design requires several threads to complete, the average number of threads completed per thread:

int num = 5;
		int avg = a.length/num;

3. Declare thread classes that define calculations and sums:
To compute sum, you pass in the array and the subscript range of the part to compute, and define a static variable for the output sum.
The code is as follows:

class MyThread extends Thread{
	private int[] a;  //Array to be computed
	private int start;  //The starting point of each thread calculation
	private int end;  //The end of each thread is calculated.
	private int sum;  //Every sum
	public  static int allsum;  //The sum

public MyThread(int[] a, int start, int end) {
	this.a = a;
	this.start = start;
	this.end = end;
}


public int getSum() {
	return sum;
}


public void setSum(int sum) {
	this.sum = sum;
}
@Override
public void run() {
	for (int i = start; i < end; i++) {
		sum +=a[i];
	}
	System.out.println(getName()+"---->"+sum);
   allsum +=sum;
}

}

4.for loops create threads and start:
Error demonstration:

for(int i=0;i<num;i++ ) {
            MyThread thread = new MyThread(a,i*avg,(i+1)*avg);
            thread.start();
            try {
				thread.join();  //Serial
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
        }
        System.out.println(MyThread.allsum);

Calling join() method ----------------------------- Parallel variable serial loses the meaning of multithreading every time a thread is created.
Demonstrate correctly:

MyThread[] mt = new MyThread[num];
		for(int i = 0 ; i < num ; i++) {
			mt[i] = new MyThread(a,i*avg,(i+1)*avg);
			mt[i].start();
		}
		//Insertion that failed to run   
		for(int i = 0 ;i < num ; i++) {
			try {
				System.out.println("--->"+i);
				mt[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(MyThread.allsum);

Personal Idea: Threads are opened and concurrent operations are performed. To output the sum, each thread must be guaranteed to run. The purpose of this traversal join is to ensure that each thread can complete, and then print: The results are as follows:
Thread-related running results are generally not fixed:

Thread-0---->45
Thread-3---->345
Thread-2---->245
Thread-1---->145
--->0
--->1
--->2
Thread-4---->445
--->3
--->4
1225

You can see that if the thread has finished running, that is, dead, the join() method is useless. It completes in parallel before entering the traversal join() loop, and then becomes the main thread waiting for the sub-thread to complete.
Several more operation results are attached:

Thread-1---->145
--->0
Thread-0---->45
Thread-2---->245
--->1
Thread-3---->345
--->2
--->3
--->4
Thread-4---->445
1225

This is because the insert is the main thread, only the main thread will wait, and the sub-threads will still be concurrent.
Specific description: Thread-1, completed before the main thread traverses the join
While the main thread waits for the Thread-0 thread to complete, Thread-2 just finishes running.
Thread-3, on the other hand, happens to complete the first traversal.
Thread-4 is waiting for completion by the main thread

Thread-0---->45
Thread-2---->245
--->0
--->1
Thread-3---->345
Thread-1---->145
Thread-4---->445
--->2
--->3
--->4
1225

This is before the main thread traverses join(), concurrently completes a part, and then completes a part in the traversal process.

If there is a mistake, the bothersome men will correct it.

Posted by jeephp on Sat, 05 Oct 2019 21:24:06 -0700