하나의 스레드가 여러 스레드 (서비스)를 설정하고 시스템 실행을 시뮬레이트하기 위해 모두 실행 한 다음 끝에 모두 조인하고 종료 등을 처리하는 설치 작업 중입니다. 내 테스트 는 서비스 중 하나로 실행되고 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;
}
}
}
전용 변수는 스레드에 속하지 않습니다. 좀 더 설명해 주시겠습니까? Thread의 서브 클래스의 실제 인스턴스의 멤버 변수입니까? –
@SteveMcLeod Runnable 클래스의 인스턴스 멤버 변수를 의미합니다. –