我正在处理一个设置,其中一个线程设置了许多线程(服务),将它们全部一起运行以模拟系统的运行,然后在最后合并它们并处理终止等操作。我的测试作为其中一个服务运行,并通过JMS与其他服务进行通信。对于我的某些测试,我需要访问另一个线程中包含的私有变量。我不能更改在另一个线程中运行的代码,例如添加访问器方法或通过JMS发送变量。由于框架设置的方式,我也无法将要访问的服务的引用传递给我的测试服务。
我知道包含我需要访问的类的线程的名称,并且可以通过枚举正在运行的线程来获取对该线程的引用,但是我不知道如何在获取到它之后从线程中提取任何内容。
是否有一种使用反射或其他技术从另一个线程获取类引用的方法?
编辑:以下是我所处情况的示例:
我知道包含我需要访问的类的线程的名称,并且可以通过枚举正在运行的线程来获取对该线程的引用,但是我不知道如何在获取到它之后从线程中提取任何内容。
是否有一种使用反射或其他技术从另一个线程获取类引用的方法?
编辑:以下是我所处情况的示例:
import java.lang.reflect.Field;
public class Runner
{
/**
* Pretend this is my test class.
*/
public static void main( String[] args )
{
// this is how my test starts up the system and runs the test
runTest( TestService.class );
}
/**
* Instantiate the test service and start up all of the threads in the
* system. Doesn't return until test has completed.
*
* @param testServiceClass
* the class that will run the test
*/
static void runTest( Class<? extends Service> testServiceClass )
{
try
{
// setup the services
Service testService =
testServiceClass.getConstructor( new Class<?>[] { String.class } )
.newInstance( "test service" );
FixedService fixedService = new FixedService( "fixed service" );
// start the services
testService.start();
fixedService.start();
// wait for testService to signal that it is done
System.out.println( "Started threads" );
while ( !testService.isDone() )
{
try
{
Thread.sleep( 1000 );
}
catch ( InterruptedException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// stop the fixed service
fixedService.stop();
System.out.println( "TestService done, fixed service told to shutdown" );
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* I cannot modify this class. Handling of thread start is similar to real
* system.
*/
abstract static class Service implements Runnable
{
protected boolean isDone = false;
protected boolean stop = false;
private Thread thisServiceThread;
public Service( String name )
{
thisServiceThread = new Thread( this, name );
}
public boolean isDone()
{
return isDone;
}
public void start()
{
thisServiceThread.start();
}
public void stop()
{
this.stop = true;
}
}
/**
* I can modify this class. This is the class that actually runs my test.
*/
static class TestService extends Service
{
public TestService( String name )
{
super( name );
}
@Override
public void run()
{
System.out.println( "TestService: started" );
// TODO: How can I access FixedService.getMe from where without
// modifying FixedService?
try
{
Field field = FixedService.class.getDeclaredField( "getMe" );
field.setAccessible( true );
System.out.println( field.get( null ) );
}
catch ( SecurityException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( NoSuchFieldException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( IllegalArgumentException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( IllegalAccessException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "TestService: done" );
isDone = true;
}
}
/**
* I cannot modify this class. This is part of the system being tested.
*/
static class FixedService extends Service
{
private boolean getMe = false;
public FixedService( String name )
{
super( name );
}
@Override
public void run()
{
System.out.println( "FixedService: started" );
// don't stop until signaled to do so
while ( !stop )
{
try
{
Thread.sleep( 1000 );
}
catch ( InterruptedException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println( "FixedService: gotMe? " + getMe );
System.out.println( "FixedService: done" );
isDone = true;
}
}
}