我在这个程序中使用多线程。我是这个概念的新手,我已经使用了我从互联网上学到的东西,但它运作不正常。能否帮助我利用所有CPU内核,以便尽可能快地计算出来。
package percolate;
public class Multithread implements Runnable
{
static int col =10000;
int m=8;
int n=3;
static int row=10000;
int count=0;
double p=Math.PI,t=Math.sqrt(3),pi=(p/(col+1));
double mul,a,b,c,d,e,f,xx,yy;
static int[][] matrix = new int [row][col];
static int[][] label = new int [row][col];
public Multithread(Object a,Object b)
{
// TODO Auto-generated constructor stub
sin1((int) a,(int) b);
}
public Multithread()
{
// TODO Auto-generated constructor stub
eqltri();
operation();
outputt();
}
private void sin1(int start,int row)
{
// TODO Auto-generated method
for (int i =start; i < row; i++)
{
for (int j = 0; j <col; j++)
{
xx=((i+1)*pi);
yy=((j+1)*pi);
a=Math.cos(((2*m)-n)*((2*(xx))/3));
b=Math.sin(((2*(n*(yy)))/t));
c=Math.cos(((((2*n)-m)*(2*(xx)))/3));
d=Math.sin(((2*m)*(yy)/t));
e=Math.cos((((m+n)*(2*(xx)))/3));
f=Math.sin((((m-n)*(2*(yy)))/t));
mul=(a*b)-(c*d)+(e*f);
if(mul<0 && (xx*t)>yy&& yy<(t*(p-xx)))
{
matrix[i][j]=0;
}
else
{
matrix[i][j]=1;
}
//System.out.print(matrix[i][j]);
}
//System.out.println();
}
}
private void eqltri()
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++)
{
for (int j = 0; j <col; j++)
{
xx=(i+1)*pi;
yy=(j+1)*pi;
if((xx*t)>yy&& yy<(t*(p-xx)))
{
matrix[i][j]=matrix[i][j];
}
else
{
matrix[i][j]=4;
}
//System.out.print(matrix[i][j]);
}
//System.out.println();
}
}
private void operation()
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++)
{
for (int j = 0; j <col; j++)
{
if(i==0 && j==0)
{
count=count+1;
label[i][j]=count;
}
else if (((i-1)>=0) && j==0)
{
left(i,j);
}
else if (((j-1)>=0)&& i==0)
{
above(i,j);
}
else
{
aboveleft(i,j);
}
}
}
}
private void left(int a,int b)
{
// TODO Auto-generated method stub
if(matrix[a-1][b]==matrix[a][b])
{
label[a][b]=label[a-1][b];
}
else
{
count=count+1;
label[a][b]=count;
}
}
private void above(int a,int b)
{
// TODO Auto-generated method stub
if (matrix[a][b-1]==matrix[a][b])
{
label[a][b]=label[a][b-1];
}
else
{
count=count+1;
label[a][b]=count;
}
}
private void aboveleft(int a,int b)
{
// TODO Auto-generated method stub
if (matrix[a][b-1]==matrix[a][b] && matrix[a-1][b]!=matrix[a][b])
{
label[a][b]=label[a][b-1];
}
else if (matrix[a-1][b]==matrix[a][b] && matrix[a][b-1]!=matrix[a][b])
{
label[a][b]=label[a-1][b];
}
else if (matrix[a][b-1]!=matrix[a][b] && matrix[a-1][b]!=matrix[a][b])
{
count=count+1;
label[a][b]=count;
}
else
{
checklabel(a, b);
}
}
private void checklabel(int a, int b)
{
// TODO Auto-generated method stub
if(label[a-1][b]>label[a][b-1])
{
label[a][b]=label[a][b-1];
int neww=label[a][b-1];
int old=label[a-1][b];
nonzero(old,neww);
backtrace(old);
count=count-1;
}
else if (label[a-1][b]==label[a][b-1])
{
label[a][b]=label[a-1][b];
}
else
{
label[a][b]=label[a-1][b];
int neww=label[a-1][b];
int old=label[a][b-1];
nonzero(old,neww);
backtrace(old);
count=count-1;
}
}
private void nonzero(int ol,int nw)
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (label[i][j]==ol)
{
label[i][j]=nw;
}
}
}
}
private void backtrace(int a)
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if(label[i][j]>a )
{
label[i][j]=(label[i][j])-1;
}
}
}
}
private void outputt()
{
// TODO Auto-generated method stub
System.out.println("hello its error");
/*for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
System.out.print(matrix[i][j]);
}
System.out.println();
}*/
System.out.println(count-1);
}
public static void main(String[] args)
{
Multithread n1=new Multithread(0,5000);
Multithread n3=new Multithread(5000,10000);
Thread t1 = new Thread(n1);
Thread t3 = new Thread(n3);
t1.start();
t3.start();
Multithread n=new Multithread();
System.out.println("Number of active threads from the given thread: " + Thread.activeCount());
}
@Override
public void run()
{
// TODO Auto-generated method stub
}
}
主要目标是在执行一些三角计算之后填充大约1000万个元素的数组,对于那个东西,我只想要多线程。请帮忙!!