Java 线程间的反射
我正在使用一个设置,其中一个线程设置多个线程(服务),将它们一起运行以模拟系统的运行,然后在最后连接它们并处理终止等。我的测试作为一个服务运行,并通过JMS与其他服务通信。对于我的一个测试,我需要访问另一个线程中包含的私有变量。我不能将另一个线程中运行的代码更改为(比如)添加访问器方法或让它通过JMS发送变量。由于框架的设置方式,我也没有办法将对我想要访问的服务的引用传递到我的测试服务中 我知道包含我需要访问的类的线程的名称,并且我可以通过枚举正在运行的线程来获得对该线程的引用,但我不知道一旦获得该线程,如何从该线程中获取任何内容 我是否有办法使用反射或其他技术在另一个线程中获取对类的引用 编辑:以下是我所处情况的示例:Java 线程间的反射,java,multithreading,reflection,Java,Multithreading,Reflection,我正在使用一个设置,其中一个线程设置多个线程(服务),将它们一起运行以模拟系统的运行,然后在最后连接它们并处理终止等。我的测试作为一个服务运行,并通过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;
}
}
}
import java.lang.reflect.Field;
公开课跑者
{
/**
*假装这是我的测试课。
*/
公共静态void main(字符串[]args)
{
//这就是我的测试如何启动系统并运行测试
runTest(TestService.class);
}
/**
*实例化测试服务并启动测试中的所有线程
*系统。在测试完成之前不会返回。
*
*@param testServiceClass
*将运行测试的类
*/
静态void运行测试(类[]{String.Class})
.newInstance(“测试服务”);
FixedService FixedService=新的FixedService(“固定服务”);
//启动服务
testService.start();
fixedService.start();
//等待testService发出完成的信号
System.out.println(“启动线程”);
而(!testService.isDone())
{
尝试
{
睡眠(1000);
}
捕捉(中断异常e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
//停止固定服务
fixedService.stop();
System.out.println(“TestService完成,修复了告知关闭的服务”);
}
捕获(例外e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
/**
*我不能修改这个类。线程启动的处理类似于real
*系统。
*/
抽象静态类服务实现可运行
{
受保护布尔isDone=false;
受保护布尔停止=false;
私有线程thisServiceThread;
公共服务(字符串名称)
{
thisServiceThread=新线程(此,名称);
}
公共布尔isDone()
{
返回isDone;
}
公开作废开始()
{
thisServiceThread.start();
}
公共停车场()
{
this.stop=true;
}
}
/**
*我可以修改这个类。这是实际运行我的测试的类。
*/
静态类TestService扩展了服务
{
公共测试服务(字符串名称)
{
超级(姓名);
}
@凌驾
公开募捐
{
System.out.println(“TestService:started”);
//TODO:如果没有,我如何从何处访问FixedService.getMe
//修改固定服务?
尝试
{
Field=FixedService.class.getDeclaredField(“getMe”);
字段。setAccessible(true);
System.out.println(field.get(null));
}
捕获(安全异常e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(无此字段例外)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(IllegalArgumentException e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(非法访问例外e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
System.out.println(“TestService:done”);
isDone=true;
}
}
/**
*我不能修改这个类。这是正在测试的系统的一部分。
*/
静态类FixedService扩展服务
{
私有布尔getMe=false;
公共固定服务(字符串名称)
{
超级(姓名);
}
@凌驾
公开募捐
{
System.out.println(“FixedService:started”);
//在接到信号之前不要停车
当(!停止)
{
尝试
{
睡眠(1000);
}
捕捉(中断异常e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
System.out.println(“FixedService:getMe?”+getMe);
System.out.println(“FixedService:done”);
isDone=true;
}
}
}
必须从中取出变量的不是线程,而是对象。试着拿出一个控制线程和被控制线程的小代码示例,它会变得更清晰
访问私有成员
import java.lang.reflect.Field;
public class SSCCE {
static class T extends Thread {
private int i;
public T(int i) {
this.i = i;
}
@Override
public void run() {
while(true) {
System.out.println("T: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
}
}
}
static class R implements Runnable {
private int i;
public R(int i) {
this.i = i;
}
@Override
public void run() {
while(true) {
System.out.println("R: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new T(1);
Thread t2 = new Thread(new R(2));
t1.start();
t2.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// ignore
}
setI(t1,3);
setI(t2,4);
}
static void setI(Thread t, int newVal) {
// Secret sauce here...
try {
Field fTarget = Thread.class.getDeclaredField("target");
fTarget.setAccessible(true);
Runnable r = (Runnable) fTarget.get(t);
// This handles the case that the service overrides the run() method
// in the thread instead of setting the target runnable
if (r == null) r = t;
Field fI = r.getClass().getDeclaredField("i");
fI.setAccessible(true);
fI.setInt(r, newVal);
} catch (Exception e) {
e.printStackTrace();
}
}
}