所以我正在编写一个简单的线程来将两个向量相加,它需要 2 个命令行参数:向量长度和线程数。据我了解,该程序应该接受这两个参数并根据它们将向量相加,显示取决于线程数和向量长度的性能。这就是我卡住的地方。到目前为止,我已经编写了使用数组将两个向量相加并创建线程的基本代码,显示了时间,但我在使用命令行参数实现它时遇到了麻烦。这是我到目前为止所做的。
public class Addition
{
public static void main(String args[])
{
int NoOfThreads = Integer.parseInt(args[0]);
VectorLength = Integer.parseInt(args[1]);
System.out.println("Start time: " + System.nanoTime());//print start time
Thread v1 = new Vector();
Thread v2 = new Vector();
Thread vsum = new Vector();
//start all threads
v1.start();
v2.start();
vsum.start();
//vsum2.start();
System.out.println("End time: " + System.nanoTime());//print end time
}
}
public class Vector extends Thread
{
//create vectors and assign them arbitrary values
int v1[] = {12,13,14,15,16,17,18};
int v2[] = {15,19,20,22,24,26,28};
//initialise the vector sums to zero
int vsum = 0;
public void run()
{
//loop to add up the elements of the first vector
if(Integer.parseInt(args[0])> 0 )
{
for(int i = 0; i < v1.length; i++)
{
for(int j=0; j<v2.length; j++)
{
vsum = v1[i]+ v2[j];
System.out.println("Current total of vector 1: " + vsum);
try
{
System.out.println(System.nanoTime());
Thread.sleep(100);
}
catch (InterruptedException e)
{}
}//for
}
}
}
}