同样一段程序,网页和IEDA运行出不一样的结果!!
package zh.codegym.task.task16.task1605;
import java.util.Date;
/*
谈谈音乐
*/
public class Solution {
public static int delay = 1000;
public static void main(String[] args) {
Thread violin = new Thread(new Violin("演奏者"));
violin.start();
}
public static void sleepNSeconds(int n) {
try {
Thread.sleep(n * delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public interface MusicalInstrument extends Runnable {
Date startPlaying();
Date stopPlaying();
}
public static class Violin implements MusicalInstrument {
private String owner;
Date a = new Date();
Date b = new Date();
public Violin(String owner) {
this.owner = owner;
}
@Override
public Date startPlaying() {
System.out.println(this.owner + " 开始演奏");
a =new Date();
return new Date();
}
public void sleepNSeconds(int n) {
try {
Thread.sleep(n*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public Date stopPlaying() {
System.out.println(this.owner + " 停止演奏");
b = new Date();
return new Date();
}
@Override
public void run(){
startPlaying();
sleepNSeconds(1);
stopPlaying();
System.out.println("已演奏"+(b.getTime()-a.getTime())+"毫秒");
}
}
}