KB Article #163630

Scripting and global variables

The Gateway is a a multi threaded environment, thus at any one time multiple threads can be executing code within a script. When you're writing javascript always declare variables with "var" otherwise the variables are global. Global variables can be updated by multiple threads.
 
Always do this:
 
var myString = new java.lang.String("hello word");
for (var i = 100; i < 100; i++) {
    java.lang.System.out.println(myString + java.lang.Integer.toString(i));
}
 
 
DO NOT do this:
 
myString = new java.lang.String("hello word");
for (i = 100; i < 100; i++) {
    java.lang.System.out.println(myString + java.lang.Integer.toString(i));
}
 
With the later under load it is not guaranteed what value will be outputted as both the variables myString and i are global.