首页 / JAVA / java-线程间反射
java-线程间反射
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-线程间反射,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5183字,纯文字阅读大概需要8分钟。
内容图文
![java-线程间反射](/upload/InfoBanner/zyjiaocheng/654/082a6497612f43e6a30a9450cf72e096.jpg)
我正在使用一种设置,其中一个线程设置了多个线程(服务),将它们全部一起运行以模拟系统的运行,然后在最后将它们全部加入并处理终止,等等.我的测试以通过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;
}
}
}
解决方法:
如Hemal Pandya所述,如果您想实际读取或操作该字段,则将需要服务对象,而不仅仅是类.
假设您需要的对象是线程上的Runnable集,则有可能发生一些非常肮脏的反射黑客.您必须使用私有成员访问技巧从线程中获取目标字段,然后再次使用它来访问可运行对象本身上所需的字段.
这是一些示例代码.请注意,我在这里并没有真正考虑线程同步问题(尽管我不确定是否可以正确同步此类访问)
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();
}
}
}
内容总结
以上是互联网集市为您收集整理的java-线程间反射全部内容,希望文章能够帮你解决java-线程间反射所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。