Home > Back-end >  Java reflection mechanism sophomore for help
Java reflection mechanism sophomore for help

Time:03-30

? Experimental requirements: the use of reflection mechanism, periodically from the configuration file conf. TXT read the content, implementation once every 5 seconds to read operation,
? Step 1: given a conf. TXT, the content inside the following
#==============
A, f1, 5
#==============
? Step 2: given a a. ava, can take the package name,
Class A
{
Public void f1 ()
{
System. The out. Println (" hello Java ");
//the following increase to read and write the conf. TXT file code, using the input stream
.
}
}
? Step 3: please use of reflection mechanism. Write a program that reads the conf. TXT, automatically according to once every 5 seconds to perform A.f (), 1
1) you can use the TimerTask to implement periodic tasks, the code is as follows:
Public class MyTask extends TimerTask {
@ Override
Public void the run () {
//use of reflection mechanism to create A instance, and call the A.f () 1.
.
}
}
2) trigger MyTask execution, the reference code is as follows:
Public static void main (String [] args) {
//TODO Auto - generated method stub
The Timer Timer=new Timer ();
The Calendar now=Calendar. GetInstance ();
The Date runDate=now. GetTime ();
MyTask task2=new MyTask ();
The timer. The scheduleAtFixedRate (task2, runDate, 5000);
}
?
  • Related