ΠΏΠΎΡΠ΅ΠΌΡ Π½Π΅ Π·Π°ΠΏΡΡΠΊΠ°Π΅ΡΡΡ ΠΌΠ΅ΡΠΎΠ΄ run() Ρ class Violin. Π IDEA Π²ΡΠ΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ
package com.javarush.task.task16.task1605;
import java.util.Date;
/*
ΠΠΎΠ³ΠΎΠ²ΠΎΡΠΈΠΌ ΠΎ ΠΌΡΠ·ΡΠΊΠ΅
*/
public class Solution {
public static int delay = 1000;
public static void main(String[] args) {
Violin violin = new Violin("Player");
Thread t1 = new Thread(violin);
t1.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;
public Violin(String owner) {
this.owner = owner;
}
public Date startPlaying() {
System.out.println(this.owner + " starts playing");
return new Date();
}
public Date stopPlaying() {
System.out.println(this.owner + " stops playing");
return new Date();
}
public void run() {
long start = this.startPlaying().getTime();
sleepNSeconds(1);
long stop = this.stopPlaying().getTime();
System.out.println("Playing " + (stop - start) + "ms");
}
}
}