[英]How do I Set Environment variable for a multithread program?
I would like to run a parallel Java threaded program and take advantage of multiprocessor execution. 我想运行并行Java线程程序并利用多处理器执行的优势。 However I need to set the environment variable, to enable a multi-threaded environment.
但是,我需要设置环境变量,以启用多线程环境。 I understand that you can set the environment by issuing setenv PARALLEL 4 OR setenv OMP_NUM_THREADS 4 (for an OpenMP program).
我了解您可以通过发出setenv PARALLEL 4或setenv OMP_NUM_THREADS 4(对于OpenMP程序)来设置环境。
This should enable 4 processors to run concurrently if you have 4 processor. 如果您有4个处理器,这将使4个处理器能够同时运行。
My Question is: 我的问题是:
- where do you issue the above command (SETENV) and how do you do it?
您在哪里发出上述命令(SETENV),该怎么做?
In java you can call System.getenv("NUM_THREADS")
to get the NUM_THREADS
. 在Java中,您可以调用
System.getenv("NUM_THREADS")
以获取NUM_THREADS
。 However there is no clear way of setting the environment. 但是,没有明确的环境设置方法。 I am running AMD-x64 machine: OS: Windows 8, Processor: AMD E-300 APU Dual-Core processor, Ram: 4.00GB, System Type: 64-bit OS.
我正在运行AMD-x64机器:操作系统:Windows 8,处理器:AMD E-300 APU双核处理器,内存:4.00GB,系统类型:64位操作系统。
Below is the link which explains on how to set environment variable in a Windows machine manually : 以下是说明如何在Windows机器中手动设置环境变量的链接:
http://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html http://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html
A piece taken from above link (must read the link completely, its very rich in knowledge): 从以上链接中摘录的一部分(必须完全阅读该链接,其知识非常丰富):
Display Variables and their Values
显示变量及其值
To list all the variables and their values, start a CMD shell (Click "Start" button ⇒ Run ⇒ Enter "cmd") and issue the command "set". 要列出所有变量及其值,请启动CMD Shell(单击“开始”按钮⇒运行⇒输入“ cmd”)并发出命令“ set”。 To display a particular variable, use command "set varname".
要显示特定变量,请使用命令“ set varname”。 For examples,
举些例子,
// Display all the variables (in NAME=VALUE pairs)
prompt> set
COMPUTERNAME=xxxxxxx
OS=xxxxxxx
PATH=xxxxxxx
.......
// Display a particular variable
prompt> set COMPUTERNAME
COMPUTERNAME=xxxxxx
// OR use echo command with variable enclosed within a pair of '%'s
prompt> echo %COMPUTERNAME%
COMPUTERNAME=xxxxxx
Try issuing a set command on your system, and study the environment variables listed. 尝试在系统上发出set命令,并研究列出的环境变量。 Pay particular attention to the variable called PATH.
请特别注意名为PATH的变量。
Set/Change/Unset a Variable
设置/更改/取消设置变量
To set (or change) a variable, use command "set varname=value". 要设置(或更改)变量,请使用命令“ set varname = value”。 There shall be no spaces before and after the '=' sign.
“ =”符号前后不得有空格。 To unset an environment variable, use "set varname=", ie, set it to an empty string.
要取消设置环境变量,请使用“ set varname =“,即,将其设置为空字符串。
prompt> set varname
prompt> set varname=value
prompt> set varname=
prompt> set
Display the value of the variable
显示变量的值
Set or change the value of the variable (Note: no space before and after '=') Delete the variable by setting to empty string (Note: nothing after '=') Display ALL the environment variables. 设置或更改变量的值(注意:“ =”前后没有空格)通过设置为空字符串来删除变量(注意:“ =”之后无内容)显示所有环境变量。 For examples,
举些例子,
// Set an environment variable
prompt> set MY_VAR=hello
// Display
prompt> set MY_VAR
MY_VAR=hello
// Unset an environment variable
prompt> set MY_VAR=
// Display
prompt> set MY_VAR
Environment variable MY_VAR not defined
A variable set via the "set" command under CMD is a local variable, available to the current CMD session only. 通过CMD下的“ set”命令设置的变量是局部变量,仅可用于当前CMD会话。
If you want to set the same using Java code, below is one example: 如果要使用Java代码设置相同的代码,请参见以下示例:
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
Map<String, String> env = pb.environment();
env.put("MYVAR", "myValue");
Process p = pb.start();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
char[] buf = new char[1024];
while (!isr.ready()) {
;
}
while (isr.read(buf) != -1) {
System.out.println(buf);
}
}
If you want to pass some value to your program, you could also do that in command line: 如果要向程序传递一些值,也可以在命令行中执行以下操作:
java -DMyVar=varValue <main program>
This value could be read as: 该值可以读作:
String myVar= System.getProperty("MyVar");
I believe setenv is a command for linux/unix. 我相信setenv是linux / unix的命令。
In windows 7, you can use the setx command in command prompt to set a User Environment Variable. 在Windows 7中,可以在命令提示符下使用setx命令设置用户环境变量。 eg:
例如:
setx myvariablename myvariablevalue
Or you can do it through the GUI: 或者,您可以通过GUI进行操作:
Right click My Computer -> Properties -> Advanced -> Environment Variables 右键单击我的电脑->属性->高级->环境变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.