如何使用线程计算功能或请求所用的时间?更详细地说,在要测量的函数之前调用 start 函数并在之后调用 end 函数。还有一件事,如果我想测量多功能所花费的时间,我该怎么做?如何将结束函数与其对应的启动函数相关联。任何帮助将不胜感激。
我能写这样的东西吗?
public void start() {
long start = SystemClock.uptimeMillis();
thrd = new Thread( new Task(start) );
thrd.start();
}
public void end() {
thrd.interrupt();
}
class Task implements Runnable{
long start;
Task(long start)
{
this.start = start;
}
@Override
public void run() {
// TODO Auto-generated method stub
if( Thread.currentThread().isInterrupted() )
{
myHandler.post(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
long end = SystemClock.uptimeMillis();
long elapsed = end - start;
}
});//post
}//if
}//run
}//Runnable
答案 0 :(得分:0)
:
@Component
public class RootPane extends StackPane {
@Inject
protected Scene scene;
@Inject
protected PerspectiveCamera camera;
@Inject
protected LightBase lightBase;
@PostConstruct
public void init() {
setBackground(new Background(new BackgroundFill(
Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
setAlignment(Pos.CENTER);
setPadding(new Insets(20));
Sphere sunSphere = new Sphere(80);
sunSphere.setEffect(new Glow(5));
Sphere mercurySphere = new Sphere(40);
mercurySphere.translateZProperty().bind(sunSphere.translateZProperty()
.add(200));
Sphere venusSphere = new Sphere(40);
venusSphere.translateXProperty().bind(sunSphere.translateXProperty()
.subtract(300));
venusSphere.translateZProperty().bind(sunSphere.translateZProperty()
.subtract(300));
Sphere earthSphere = new Sphere(40);
earthSphere.translateXProperty().bind(sunSphere.translateXProperty()
.add(300));
earthSphere.translateZProperty().bind(sunSphere.translateZProperty()
.subtract(400));
Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
Translate translate = new Translate(0, 0, -3000);
camera.getTransforms().addAll(rotateX, rotateY, rotateZ, translate);
scene.addEventHandler(
KeyEvent.KEY_PRESSED,
event -> {
if (Objects.equals(event.getCode(), KeyCode.D)) {
rotateY.setAngle(rotateY.getAngle() -
(event.isShiftDown() ? 5 : 0.1));
} else if (Objects.equals(event.getCode(), KeyCode.A)) {
rotateY.setAngle(rotateY.getAngle() +
(event.isShiftDown() ? 5 : 0.1));
}
});
scene.addEventHandler(
KeyEvent.KEY_PRESSED,
event -> {
if (Objects.equals(event.getCode(), KeyCode.W)) {
rotateX.setAngle(rotateX.getAngle() -
(event.isShiftDown() ? 5 : 0.1));
} else if (Objects.equals(event.getCode(), KeyCode.S)) {
rotateX.setAngle(rotateX.getAngle() +
(event.isShiftDown() ? 5 : 0.1));
}
});
lightBase.translateXProperty().bind(sunSphere.translateXProperty());
lightBase.translateYProperty().bind(sunSphere.translateYProperty());
lightBase.translateZProperty().bind(sunSphere.translateZProperty());
getChildren().addAll(sunSphere, mercurySphere, venusSphere,
earthSphere, camera, lightBase);
}
}
功能结束后:
long startTime = System.currentTimeMillis();
会在几秒钟内给你带来差异。希望这会有所帮助。