# Practical application of Java thread pools: Probability of a wooden stick randomly splitting into three segments to form a triangle multithreaded solution

The introduction of the java thread pool is Web-wide and can be queried, so I won't dwell on it (it may not be as good as the data)

Let's look at a real problem:

We have a wooden stick of length 1. Now we randomly divide it into three parts. What is the probability that these three sticks will form a triangle?

I believe that some math-savvy little buddies can give analytical answers through trilateral relationships, but as a computer man, we can learn from the great Monte Carlo method.

(Monte Carlo method was used in the United States in the 1940s. The Second World War Development in Atomic Bomb S.M. Uram and J. von Neumann, members of the Manhattan Initiative, first proposed this method. Mathematician von Neumann gave it a mystery by naming it M onte Carlo, Monaco, the world-famous gambling city.)

We use java's random method to generate a random number within [0,1], a=Math.random(), and then b=(1-a)*Math.random(), c=1-a-b;So we have three random numbers a, b, C and sum(a,b,c)=1.

As the characteristics of triangles tell us, triangles can easily add up to more than the third side, so we can set three test conditions:

a+b>c&&a+c>b&&c+b>a

This gives us the result of determining whether the generated random numbers make up a triangle. Next, repeat the process and let it calculate n times to get an answer. We solve this problem with a for loop.

Using java programming, we encapsulate this triangle problem into a class. If you want to concurrently compute n such triangle problems at the same time, one solution is to use a thread pool. If you want to do this, you need to implement the Runnable interface in the triangle problem, then overload the run method. Then declare a thread pool in the Solution class, and each new oneThe triangle problem can be solved by using the execute method. (The principle is more difficult than using the series).

Say nothing more. Code it:

```import java.util.ArrayList;
import java.util.List;

public class TriangleProbability implements Runnable{
TriangleProbability(int size,int num){
inf=size;
this.num=num;
}
TriangleProbability(){
inf=(int)1e8;
}
private int inf;
private int num;
boolean judge(double a,double b,double c){
if (a+b>c&&a+c>b&&b+c>a)return true;
return false;
}
public double probability(){
int cnt=0;
for (int i = 0; i < inf; i++) {
double a=Math.random();
double b=(1-a)*Math.random();
double c=1.0-a-b;
if (judge(a,b,c)){
cnt++;
}
}
double result=((double) cnt)/((double) inf);
return result;
}

@Override
public void run() {
System.out.println("run test "+num);
System.out.println(probability());
}
}```

The above code, which implements a triangle problem alone, will solve the problem at this point, but let's take a closer look at the results of parallel computing:

```import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Solution {
public static void main(String[] args) {
int cnt= (int) 1e7;
for (int i = 0; i < 25; i++) {
TriangleProbability triangleProbability=new TriangleProbability(cnt,i);
executor.execute(triangleProbability);
}
executor.shutdown();
//      System.out.println("all the test is finished");
}
}
```

In this simple code, just put the new triangle Probability into the execute method of the thread pool, qaq.

However, trying to figure out if the thread pool is still a strenuous endeavor will allow me to delve deeper when I have time.

Attached results:

```run test 0
Number of threads in thread pool: 1, number of tasks waiting to be executed in queue: 0, number of other tasks performed: 0
Number of threads in thread pool: 2, number of tasks waiting to be executed in queue: 0, number of other tasks executed: 0
run test 1
Number of threads in thread pool: 3, number of tasks waiting to be executed in queue: 0, number of other tasks executed: 0
Number of threads in thread pool: 4, number of tasks waiting to be executed in queue: 0, number of other tasks executed: 0
run test 2
Number of threads in thread pool: 5, number of tasks waiting to be executed in queue: 0, number of other tasks performed: 0
Number of threads in thread pool: 6, number of tasks waiting to be executed in queue: 0, number of other tasks performed: 0
Number of threads in thread pool: 7, number of tasks waiting to be executed in queue: 0, number of other tasks performed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 0, number of other tasks performed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 1, number of other tasks performed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 2, number of other tasks performed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 3, number of other tasks performed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 4, number of other tasks executed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 5, number of other tasks executed: 0
Number of threads in thread pool: 8, number of tasks waiting to be executed in queue: 6, number of other tasks performed: 0
Number of threads in thread pool: 9, number of tasks waiting to be executed in queue: 6, number of other tasks performed: 0
Number of threads in thread pool: 10, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 11, number of tasks waiting to be executed in queue: 6, number of other tasks performed: 0
Number of threads in thread pool: 12, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 13, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 14, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 15, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 16, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 17, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 18, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
Number of threads in thread pool: 19, number of tasks waiting to be executed in queue: 6, number of other tasks executed: 0
all the test is finished
run test 3
run test 4
run test 14
run test 6
run test 7
run test 16
run test 17
run test 20
run test 21
run test 24
run test 15
run test 5
run test 18
run test 19
run test 22
run test 23
0.1930215
run test 8
0.1930061
run test 9
0.1929787
run test 10
0.1930489
run test 11
0.1930994
run test 12
0.1932215
run test 13
0.1929723
0.19309
0.1933499
0.1931939
0.1931726
0.19289
0.1930908
0.1930913
0.1933291
0.1931412
0.1930616
0.1930444
0.1932149
0.1931634
0.1934008
0.1932558
0.1933119
0.1930957
0.1931464

Process finished with exit code 0
```

You can see that the results of each run are stable around 0.193. If calculated mathematically, the mathematical expectation of probability is 0.2.

Let's give a rough estimate of the maximum error: (0.2-0.193)/0.2=0.035=3.5% I think this error range is acceptable.

Posted on Sun, 10 Oct 2021 12:12:33 -0400 by geek_girl_2020