Previous Topic: Using Date UtilitiesNext Topic: In-Container Testing (ICT)


Class Loader Sandbox Example

The following Java class is a simple example of a class that cannot be run in multithreaded or multiuser fashion, because it accesses and modifies a static variable.

public class NeedsASandbox \{
 
static \{
 
System.out.println("This is my static initializer. You will see this many times.");
 
 
 
static String s;
 
 
 
public NeedsASandbox() \{\};
 
 
 
public void setS(String s)\{
 
this.s = s;
 
public String getS() \{
 
return s;

This class must run in a class loader sandbox.

Assume, for example, that you were to run this class in DevTest and create a load test of ten users. If you do not use the class loader sandbox, you see the System.out.println phrases only one time, and the value of s would be incorrect. This result is because all users are running in one class loader. This specific class fails in those circumstances. Presuming that this is the proper function of the application, use support for the class loader sandbox to make this work properly.

When you create the Class Loader Sandbox Companion and DevTest stages your ten users, you see the text phrase in the code appear ten times. DevTest constructs ten separate class loaders and instantiates this class ten times; there are ten separate instances of the class variable "static string s." This capability lets your application logic, which is not thread safe, to be run in concurrent user tests.

The Class Loader Sandbox Companion is useful only if the following three conditions are present: