I was trying to implement a real time executing application in which a thread is the initiator, which will call java midi
to play some music (i.e. execution statements implemented in the overriden run()
method of Runnable interface
).
But thread starting (thread.start()
method) itself is taking more than 2 milliseconds which is making a significant delay in the execution of application. I used thread because sometimes the player application has to be restarted in between.
Is there any method to reduce the execution time of start()
method. I tried by giving priority to the thread also, but still it didnt work. I want that delay to be few micro seconds only. Any helps will be appreciative. I am using a java fxml application
in which a button click event is handling these all.
Here is my code:
@FXML
private static NotesPlayer playerObj=new NotesPlayer();
@FXML
private void handlePlayButtonAction(ActionEvent event) {
System.out.println("BUTTON CLICKED ");
NotesPlayer.onclick=System.nanoTime();
playerObj.play();
}
NotesPlayer Class
`public class NotesPlayer implements Runnable
{
protected Thread t=null;
protected static long onclick;
private static long b4start;
private static long afterstart;
public void run()
{
afterstart=System.nanoTime();
System.out.println("THREAD STARTING duration: "+((double)(afterstart-b4start))/1000000+" Milli sec");
System.out.println("Total time : "+((double)(afterstart-onclick))/1000000 +" milli sec");
}
public void play()
{
if(t==null)
{
t = new Thread (this);
t.setPriority(1);
}
else
{
try {
t.interrupt();
t = new Thread (this);
t.setPriority(1);
} catch (Exception ex) {
Logger.getLogger(NotesPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
b4start=System.nanoTime();
t.start();
}}
Output:
BUTTON CLICKED
THREAD STARTING duration: 3.89165 Milli sec
Total time : 4.4235 Milli sec