[英]Threads in Java and command line arguments
So I am writing a simple thread to add two vectors together and it takes 2 command line arguments: vector length and number of threads. 因此,我正在编写一个简单的线程来将两个向量相加,它需要2个命令行参数:向量长度和线程数。 From what I understand, the program is supposed to take in these two arguments and add up the vectors according to them showing the performance depending on the number of threads and vector length.
据我了解,该程序应该接受这两个参数,并根据它们相加向量,从而根据线程数和向量长度显示性能。 This is where I am stuck.
这就是我卡住的地方。 So far I have written basic code that adds two vectors together using arrays and creates the threads, showing the times but I am having trouble implementing it with the command line arguments.
到目前为止,我已经编写了基本的代码,该代码使用数组将两个向量相加并创建线程,显示了时间,但是我在使用命令行参数来实现它时遇到了麻烦。 Here is what I've done so far.
到目前为止,这是我所做的。
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
}
}
}
} }
You cannot use args
outside the main
class. 您不能在
main
类之外使用args
。
Please don't use Vector
as it is an implementation of ArrayList
. 请不要使用
Vector
因为它是ArrayList
的实现。
What you can do is add member variable to your thread
class as int NoOfThreads
and set it while constructing the thread class
and use this in your run()
. 您可以做的是将成员变量
int NoOfThreads
添加到thread
类中,并在构造thread class
时int NoOfThreads
进行设置,并在run()
使用它。
A few notes: 一些注意事项:
main(..)
, you are not waiting properly for the threads to terminate. main(..)
,您没有正确等待线程终止。 "End time:..." will be printed immediately after you start the threads. Thread.join()
. Thread.join()
。 vectorLength
not declared, args
unknown in the scope where it's used, etc.) vectorLength
,在使用范围内args
未知,等等) I see two compilation problems here. 我在这里看到两个编译问题。
Firstly, the type of VectorLength
isn't defined. 首先,没有定义
VectorLength
的类型。 I think it should be int VectorLength
. 我认为应该是
int VectorLength
。
Secondly, you are trying to access args
reference in the Vector
class which does not seem available. 其次,您尝试访问似乎不可用的
Vector
类中的args
引用。
If you want to use args
in the Vector
class, you can pass this as an argument to Vector
constructor and store it as a field. 如果要在
Vector
类中使用args
,则可以将其作为参数传递给Vector
构造函数并将其存储为字段。 Something like this: 像这样:
public Vector(String[] args) {
this.args = args;
}
And from your Addition
class you should then be using this overloaded constructor: 然后从您的
Addition
类中,应该使用此重载的构造函数:
Thread v1 = new Vector(args);
Thread v2 = new Vector(args);
Creating a class as Vector will not be a problem since Vector is not a reserved keyword in Java and since you are not using the Vector Class of <>-><> it will not be a problem. 因为Vector不是Java中的保留关键字,所以将类创建为Vector将不会有问题,并且由于您没有使用<>-> <>的Vector类,因此不会有问题。
The only problem i can see is in this code 我唯一看到的问题是这段代码
int NoOfThreads = Integer.parseInt(args[0]);
VectorLength = Integer.parseInt(args[1]);
You are trying to initialize two variable NoOfThreads and VectorLength at the same time but have ended the statement in between with a semicolon. 您正在尝试同时初始化两个变量NoOfThreads和VectorLength,但是已经用分号结束了两者之间的语句。
int NoOfThreads = Integer.parseInt(args[0])**,**
VectorLength = Integer.parseInt(args[1]);
notice the comma inplace of semicolon. 注意用逗号代替分号。
Secondly, you are trying to access args[0] in the run method in Vector class but the scope of args array is in the main method of the Addition. 其次,您尝试在Vector类的run方法中访问args [0],但args数组的范围在Addition的main方法中。
if(Integer.parseInt(args[0])> 0 )
Try solving both problems on your own. 尝试自己解决两个问题。 All the best.
祝一切顺利。
There are too many things broken in your code. 您的代码中有太多坏处。 Here are few.
这里有几个。
I would suggest to begin on more humble programming exercises before going for this task. 我建议在执行此任务之前先进行一些谦虚的编程练习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.