2

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
  • 5
    You could use [an ExecutorService](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) - that way the threads will (generally) already be started, saving you those 2 milliseconds. – assylias Oct 12 '15 at 10:52
  • As far as thread priority goes, generally Linux Kernels don't support thread priority completely. You might have to set ThreadPriorityPolicy flag to make the JVM even consider your overridden priority value. So, I wouldn't be bothered too much about it – TheLostMind Oct 12 '15 at 11:03
  • @assylias Thank you for the quick response.. When we use an Executorservice , is it possible to interrupt it execution.. because while restarting im interuppting the alive thread and creating new one. – Shana Kalyan Oct 12 '15 at 11:29
  • @TheLostMind , i m working on windows platform only. i checked the priority after setting, its higher only.. but the delay has not reduced even after that. – Shana Kalyan Oct 12 '15 at 11:33
  • 1
    @ShanaKalyan yes you can interrupt a task (but the underlying thread will be reused for the next task you submit). – assylias Oct 12 '15 at 11:35
  • 1
    Re priorities: Desktop/server/smartphone operating systems are designed to host multiple applications that _compete_ with one another for resources such as CPU time. The OS tries to give each thread a _fair share_. Java thread "priorities" on such platforms usually do not map to the thread's true scheduling priority, but rather, to a tuning parameter (e.g., Unix _nice_ value) that adjusts the size of the thread's share. I don't know Android, so I don't know how you can set the true priority of a thread that needs to deliver audio data in real-time. – Solomon Slow Oct 12 '15 at 12:19
  • @assylias Thank you. I was looking for something like this. – Shana Kalyan Oct 13 '15 at 04:00
  • @assylias I tried using ExecutorService . but still the delay is same at the beginning . But one thing i noticed was, while restarting sometimes that delay is very much reduced. Can You please tell me why it is so??? – Shana Kalyan Oct 15 '15 at 03:56
  • I suggest you create a new question and show your code... – assylias Oct 15 '15 at 06:13
  • @assylias I have added my new code with new question (http://stackoverflow.com/questions/33162664/how-to-reduce-the-time-delay-to-reach-run-method-of-runnable-class-using-executo) Please have a look at this. – Shana Kalyan Oct 16 '15 at 04:47

0 Answers0