Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: thread/timer/sound interaction

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default thread/timer/sound interaction

    Good morning !
    I m new to Java ,but I believe this question about thread organisation in my program ..Can you please help me to optimize it ?
    Program has to do count time and show it on display and at the same time play appropriate sound (frequency of sound is taken from array freq[]
    .When i m trying to run program it produced appropriate sounds ,but timer slows down and count time with delays up to 10 seconds ...
    What should i do ?
    Thank you very much
    Edward
    Here is code
    import java.util.Locale;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import javax.sound.sampled.*;
    import java.text.*;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.applet.*;
    import java.awt.*;
    import java.util.*;
    import java.text.SimpleDateFormat;


    public class clock extends Applet implements Runnable{
    Thread t,t1;
    public static float SAMPLE_RATE = 8000f;
    public int rem60(int a){
    int k = (a% 60);

    k=60-Math.abs(k);

    return k;
    }
    public static void sound60(double hz, int msecs, double vol)
    throws LineUnavailableException {

    if (hz <= 0)
    throw new IllegalArgumentException("Frequency <= 0 hz");

    if (msecs <= 0)
    throw new IllegalArgumentException("Duration <= 0 msecs");

    if (vol > 1.0 || vol < 0.0)
    throw new IllegalArgumentException("Volume out of range 0.0 - 1.0");

    byte[] buf = new byte[(int)SAMPLE_RATE * msecs / 1000];

    for (int i=0; i<buf.length; i++) {
    double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
    buf[i] = (byte)(Math.sin(angle) * 127.0 * vol);
    }

    // shape the front and back 10ms of the wave form
    for (int i=0; i < SAMPLE_RATE / 100.0 && i < buf.length / 2; i++) {
    buf[i] = (byte)(buf[i] * i / (SAMPLE_RATE / 100.0));
    buf[buf.length-1-i] =
    (byte)(buf[buf.length-1-i] * i / (SAMPLE_RATE / 100.0));
    }

    AudioFormat af = new AudioFormat(SAMPLE_RATE,8,1,true,false);
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
    sdl.open(af);
    sdl.start();
    sdl.write(buf,0,buf.length);
    sdl.drain();
    sdl.close();
    }
    public void start(){
    t = new Thread(this);
    t.start();
    }
    public void run(){
    t1 = Thread.currentThread();
    while(t1 == t){
    repaint();
    try{
    t1.sleep(1000);
    }
    catch(InterruptedException e){}
    }
    }
    public void paint(Graphics g){



    double[] ff={400,500,600};

    Calendar cal = new GregorianCalendar();
    Calendar cal0 = new GregorianCalendar();
    cal0.set(2013, 1, 13, 23, 56,04);



    int difDay =cal.get(Calendar.DAY_OF_YEAR)-cal0.get(Calendar.DAY_OF_YEAR);
    int difHour=cal.get(Calendar.HOUR_OF_DAY)-cal0.get(Calendar.HOUR_OF_DAY);
    int difMin=cal.get(Calendar.MINUTE)-cal0.get(Calendar.MINUTE);
    int difSec=cal.get(Calendar.SECOND)-cal0.get(Calendar.SECOND);

    int Day60= this.rem60(difDay);
    int Hour60= this.rem60(difHour);
    int Min60= this.rem60(difMin);
    int Sec60= 60-this.rem60(difSec);

    String DD = String.valueOf(Day60);
    String DH = String.valueOf(Hour60);
    String DM = String.valueOf(Min60);
    String DS = String.valueOf(Sec60);




    String day = String.valueOf(cal.get(Calendar.DAY_OF_YEAR));
    String hour = String.valueOf(cal.get(Calendar.HOUR));
    String minute = String.valueOf(cal.get(Calendar.MINUTE));
    String second = String.valueOf(cal.get(Calendar.SECOND));


    Font font = new Font("Serif", Font.BOLD, 12);
    g.setFont(font);

    g.drawString("hour "+ hour + "minute :" + minute + "second :" + second, 14, 30);
    g.drawString("Day 60 :"+DD,14,50);
    g.drawString("Hour 60 : "+DH,14,70);
    g.drawString("Minutes 60 :"+DM,14,90);
    g.drawString("Secundes 60 :"+DS,14,110);



    try {
    double[] freq = {440, 469.86, 495.0,501.75, 528.64, 556.88, 594.39, 626.48, 660,704.79, 742.5, 792.86, 835.31//1
    ,469.86, 501.75, 528.6,564.52,594.67, 634.73, 669, 704.79,752.63,792.89,846.67,892.01 //2
    ,495,528.6,594.73,626.48,668.68, 704.79, 742.50, 792.89, 835.31, 891.97,939.73,//3
    528.64 ,564.52,594.73,635.15,669.07,714.13,752.7,792.97,8 46.79,892.09,952.59,1003.6,//4
    556.88,594.67,626.48,669.07,704.79,752.27,792.89,8 35.31,892.01,939.73,1057.19,//5
    594.39, 634.73, 668.68,714.13,752.27,802.94,846.3,891.58,952.09,10 03.03,1071.06,1128.4,//6
    626.48,669.0,704.79,752.7,792.89,846.3,892.01,939. 73,1003.51,1057.19,1113.75,1189.29,1252.97,//7
    660,704.79,742.5,792.97,835.31,891.58,939.73,990,1 057.19,1113.75,1189.29,1252.97,//8

    704.79,752.63,792.89,846.79,892.01,952.09,1003.51, 1057.19,1128.95,1189.34,1270.01,1338,//9
    742.5,792.89,835.31,892.09,939.73,1003.46,1057.19, 113.75,1189.34,1252.97,1337.95,1409.59,//10
    792.86,846.67,891.97,952.59,1003.46,1071.06,1128.9 ,1189.29,1270.01,1337.95,1428.7,1505.19,//11
    835.31,892.01,939.73,1003.6,1057.19,1128.4,1189.34 ,1252.97,1338.01,1409.59,1505.19,1585.79//12

    };

    for(int i=1; i<55; i++){
    this.sound60(freq[i],100,0.5);//day



    }

    } catch (LineUnavailableException e) {
    e.printStackTrace();
    }



    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    but timer slows down and count time with delays up to 10 seconds
    Where do you use the TImer class? I don't see it in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    I do not use timer class .By timer i mean displaying current time on the screen ,which is slow down .To get time i m using calendar

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    How long does it take the loop at the end of the paint() method to execute? No changes will be made to the GUI while it is executing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    Thank you very much
    Can you please tell me how do i change code?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    How long does it take the loop at the end of the paint() method to execute? No changes will be made to the GUI while it is executing.

    how do i change code?
    How do you want the code's execution changed? You need to decide what the code is to do, before you can make changes to the code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    I want code to show time on display without delay and at the same time play sound using this.sound60(freq[i],100,0.5) without any delay and interference with showing time .Thank you very much

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    You still have not answered this question:
    How long does it take the loop at the end of the paint() method to execute?
    That will be the minimum time between updates to the GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    How can i measure this time ?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    Use the System class's current time method before and after the loop and take the difference.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    I have placed 4 currenttimein milliseconds
    System.out.println("time 1 "+System.currentTimeMillis());
    g.drawString("hour "+ hour + "minute :" + minute + "second :" + second, 14, 30);
    System.out.println("time 2 "+System.currentTimeMillis());
    then another one(3) after declaring array/before for cycle
    and last one(4) after for cycle


    there are results
    time 1 1367607840678
    time 2 1367607840687
    time 3 1367607840687
    time 4 1367607840893
    time 4 1367607841064

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    Now can you do the arithmetic to compute how loop it took for the loop to execute?
    time for loop to execute = end loop time - start loop time

    This is the loop I am talking about:
     
    for(int i=1; i<55; i++){
      this.sound60(freq[i],100,0.5);//day
    }
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    then its
    time 3 1367607840687
    time 4 1367607840893

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    You computer is MUCH faster than mine. Mine takes 19 seconds to execute that loop.
    The time4 - time3 is 0.2 seconds

    How can that be?


    Can you post the code showing where time 3 and time 4 are printed?

    My times:
    Before t=1367601975250
    After t=1367601993590
    Before t=1367601993596
    After t=1367602010747
    Before t=1367602010770
    After t=1367602027937
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    System.out.println("time 3 "+System.currentTimeMillis());

    for(int i=1; i<55; i++){
    this.sound60(freq[i],100,0.5);//day

    System.out.println("time 4 "+System.currentTimeMillis());

    }
    // System.out.println("time 5 "+System.currentTimeMillis());
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    time 5 is the important one.
    What are the times at time3 and time 5?

    Time 4 should print 54 times. What was the last value printed? Compare it to time 3
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    System.out.println("time 1 "+System.currentTimeMillis());

    for(int i=1; i<55; i++){
    System.out.println("time 2 "+System.currentTimeMillis());
    this.sound60(freq[i],100,0.5);//day

    System.out.println("time 3 "+System.currentTimeMillis());

    }
    System.out.println("time 4 "+System.currentTimeMillis());
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    __________________________________________________ ____________
    time 1 1367612820625
    time 2 1367612820626
    time 3 1367612820941
    time 2 1367612820941
    time 3 1367612821161
    time 2 1367612821161
    time 3 1367612821379
    time 2 1367612821379
    time 3 1367612821599
    time 2 1367612821599
    time 3 1367612821822
    time 2 1367612821822
    time 3 1367612822052
    time 2 1367612822052
    time 3 1367612822294
    time 2 1367612822294
    time 3 1367612822532
    time 2 1367612822532
    time 3 1367612822764
    time 2 1367612822765
    time 3 1367612822999
    time 2 1367612822999
    time 3 1367612823226
    time 2 1367612823227
    time 3 1367612823467
    time 2 1367612823467
    time 3 1367612823706
    time 2 1367612823706
    time 3 1367612823941
    time 2 1367612823941
    time 3 1367612824171
    time 2 1367612824172
    time 3 1367612824399
    time 2 1367612824399
    time 3 1367612824629
    time 2 1367612824629
    time 3 1367612824877
    time 2 1367612824877
    time 3 1367612825133
    time 2 1367612825134
    time 3 1367612825372
    time 2 1367612825372
    time 3 1367612825621
    time 2 1367612825622
    time 3 1367612825860
    time 2 1367612825860
    time 3 1367612826107
    time 2 1367612826107
    time 3 1367612826342
    time 2 1367612826342
    time 3 1367612826598
    time 2 1367612826599
    time 3 1367612826837
    time 2 1367612826838
    time 3 1367612827084
    time 2 1367612827084
    time 3 1367612827323
    time 2 1367612827324
    time 3 1367612827589
    time 2 1367612827589
    time 3 1367612827827
    time 2 1367612827827
    time 3 1367612828075
    time 2 1367612828075
    time 3 1367612828306
    time 2 1367612828306
    time 3 1367612828535
    time 2 1367612828535
    time 3 1367612828799
    time 2 1367612828799
    time 3 1367612829039
    time 2 1367612829040
    time 3 1367612829274
    time 2 1367612829275
    time 3 1367612829533
    time 2 1367612829533
    time 3 1367612829772
    time 2 1367612829772
    time 3 1367612830022
    time 2 1367612830022
    time 3 1367612830260
    time 2 1367612830261
    time 3 1367612830502
    time 2 1367612830503
    time 3 1367612830738
    time 2 1367612830738
    time 3 1367612830987
    time 2 1367612830987
    time 3 1367612831220
    time 2 1367612831220
    time 3 1367612831457
    time 2 1367612831457
    time 3 1367612831710
    time 2 1367612831711
    time 3 1367612831951
    time 2 1367612831951
    time 3 1367612832182
    time 2 1367612832182
    time 3 1367612832420
    time 2 1367612832421
    time 3 1367612832659
    time 2 1367612832659
    time 3 1367612832898
    time 2 1367612832898
    time 3 1367612833137
    time 2 1367612833137
    time 3 1367612833376
    time 2 1367612833376
    time 3 1367612833614
    time 4 1367612833614
    time 1 1367612833620
    time 2 1367612833620
    time 3 1367612833861
    time 2 1367612833861
    time 3 1367612834097
    time 2 1367612834097
    time 3 1367612834323
    time 2 1367612834323
    time 3 1367612834552
    time 2 1367612834552
    time 3 1367612834781
    time 2 1367612834782
    time 3 1367612835030
    time 2 1367612835030
    time 3 1367612835268
    time 2 1367612835268
    time 3 1367612835502
    time 2 1367612835502
    time 3 1367612835754
    time 2 1367612835755
    time 3 1367612835991
    time 2 1367612835992
    time 3 1367612836232
    time 2 1367612836232
    time 3 1367612836469
    time 2 1367612836470
    time 3 1367612836710
    time 2 1367612836710
    time 3 1367612836936
    time 2 1367612836937
    time 3 1367612837183
    time 2 1367612837183
    time 3 1367612837419
    time 2 1367612837420
    time 3 1367612837660
    time 2 1367612837660
    time 3 1367612837895
    time 2 1367612837896
    time 3 1367612838123
    time 2 1367612838123
    time 3 1367612838353
    time 2 1367612838354
    time 3 1367612838592
    time 2 1367612838592
    time 3 1367612838832
    time 2 1367612838832
    time 3 1367612839070
    time 2 1367612839071
    time 3 1367612839308
    time 2 1367612839308
    time 3 1367612839547
    time 2 1367612839547
    time 3 1367612839793
    time 2 1367612839793
    time 3 1367612840034
    time 2 1367612840034
    time 3 1367612840279
    time 2 1367612840279
    time 3 1367612840507
    time 2 1367612840507
    time 3 1367612840737
    time 2 1367612840737
    time 3 1367612840966
    time 2 1367612840966
    time 3 1367612841208
    time 2 1367612841208
    time 3 1367612841435
    time 2 1367612841435
    time 3 1367612841657
    time 2 1367612841657
    time 3 1367612841894
    time 2 1367612841894
    time 3 1367612842134
    time 2 1367612842134
    time 3 1367612842372
    time 2 1367612842372
    time 3 1367612842612
    time 2 1367612842612
    time 3 1367612842852
    time 2 1367612842853
    time 3 1367612843096
    time 2 1367612843096
    time 3 1367612843320
    time 2 1367612843320
    time 3 1367612843554
    time 2 1367612843554
    time 3 1367612843780
    time 2 1367612843780
    time 3 1367612844001
    time 2 1367612844001
    time 3 1367612844238
    time 2 1367612844238
    time 3 1367612844482
    time 2 1367612844482
    time 3 1367612844714
    time 2 1367612844714
    time 3 1367612844959
    time 2 1367612844959
    time 3 1367612845183
    time 2 1367612845183
    time 3 1367612845422
    time 2 1367612845422
    time 3 1367612845656
    time 2 1367612845656
    time 3 1367612845896
    time 2 1367612845896
    time 3 1367612846136
    time 2 1367612846136
    time 3 1367612846376
    time 4 1367612846376
    time 1 1367612846385
    time 2 1367612846385
    time 3 1367612846622
    time 2 1367612846622
    time 3 1367612846864
    time 2 1367612846864
    time 3 1367612847104
    time 2 1367612847104
    time 3 1367612847344
    time 2 1367612847345
    time 3 1367612847597
    time 2 1367612847598
    time 3 1367612847824
    time 2 1367612847824
    time 3 1367612848045
    time 2 1367612848045
    time 3 1367612848273
    time 2 1367612848273
    time 3 1367612848505
    time 2 1367612848505
    time 3 1367612848765
    time 2 1367612848765
    time 3 1367612849003
    time 2 1367612849003
    time 3 1367612849229
    time 2 1367612849229
    time 3 1367612849461
    time 2 1367612849462
    time 3 1367612849733
    time 2 1367612849733
    time 3 1367612849974
    time 2 1367612849974
    time 3 1367612850209
    time 2 1367612850210
    time 3 1367612850449
    time 2 1367612850449
    time 3 1367612850676
    time 2 1367612850676
    time 3 1367612850917
    time 2 1367612850917
    time 3 1367612851157
    time 2 1367612851157
    time 3 1367612851402
    time 2 1367612851402
    time 3 1367612851640
    time 2 1367612851640
    time 3 1367612851874
    time 2 1367612851874
    time 3 1367612852114
    time 2 1367612852115
    time 3 1367612852356
    time 2 1367612852357
    time 3 1367612852593
    time 2 1367612852593
    time 3 1367612852833
    time 2 1367612852834
    time 3 1367612853098
    time 2 1367612853098
    time 3 1367612853321
    time 2 1367612853321
    time 3 1367612853551
    time 2 1367612853551
    time 3 1367612853792
    time 2 1367612853793
    time 3 1367612854025
    time 2 1367612854025
    time 3 1367612854254
    time 2 1367612854255
    time 3 1367612854491
    time 2 1367612854492
    time 3 1367612854729
    time 2 1367612854729
    time 3 1367612854969
    time 2 1367612854969
    time 3 1367612855206
    time 2 1367612855206
    time 3 1367612855446
    time 2 1367612855446
    time 3 1367612855690
    time 2 1367612855690
    time 3 1367612855927
    time 2 1367612855927
    time 3 1367612856169
    time 2 1367612856169
    time 3 1367612856407
    time 2 1367612856407
    time 3 1367612856644
    time 2 1367612856644
    time 3 1367612856882
    time 2 1367612856883
    time 3 1367612857122
    time 2 1367612857122
    time 3 1367612857359
    time 2 1367612857360
    time 3 1367612857598
    time 2 1367612857598
    time 3 1367612857837
    time 2 1367612857837
    time 3 1367612858076
    time 2 1367612858076
    time 3 1367612858312
    time 2 1367612858312
    time 3 1367612858548
    time 2 1367612858548
    time 3 1367612858787
    time 2 1367612858787
    time 3 1367612859024
    time 2 1367612859024
    time 3 1367612859260
    time 4 1367612859260
    time 1 1367612859266
    time 2 1367612859266
    time 3 1367612859506
    time 2 1367612859506
    time 3 1367612859732
    time 2 1367612859732
    time 3 1367612859963
    time 2 1367612859964
    time 3 1367612860196
    time 2 1367612860196
    time 3 1367612860424
    time 2 1367612860424
    time 3 1367612860653
    time 2 1367612860654
    time 3 1367612860891
    time 2 1367612860891
    time 3 1367612861116
    time 2 1367612861116
    time 3 1367612861348
    time 2 1367612861348
    time 3 1367612861579
    time 2 1367612861579
    time 3 1367612861794
    time 2 1367612861794
    time 3 1367612861996
    time 2 1367612861996
    time 3 1367612862200
    time 2 1367612862200
    time 3 1367612862409
    time 2 1367612862409
    time 3 1367612862624
    time 2 1367612862624
    time 3 1367612862835
    time 2 1367612862835
    time 3 1367612863052
    time 2 1367612863052
    time 3 1367612863291
    time 2 1367612863291
    time 3 1367612863553
    time 2 1367612863553
    time 3 1367612863790
    time 2 1367612863791
    time 3 1367612864030
    time 2 1367612864030
    time 3 1367612864268
    time 2 1367612864268
    time 3 1367612864504
    time 2 1367612864504
    time 3 1367612864742
    time 2 1367612864742
    time 3 1367612864984
    time 2 1367612864984
    time 3 1367612865231
    time 2 1367612865231
    time 3 1367612865471
    time 2 1367612865471
    time 3 1367612865717
    time 2 1367612865717
    time 3 1367612865946
    time 2 1367612865947
    time 3 1367612866178
    time 2 1367612866178
    time 3 1367612866408
    time 2 1367612866408
    time 3 1367612866642
    time 2 1367612866642
    time 3 1367612866871
    time 2 1367612866872
    time 3 1367612867109
    time 2 1367612867109
    time 3 1367612867347
    time 2 1367612867347
    time 3 1367612867576
    time 2 1367612867576
    time 3 1367612867811
    time 2 1367612867811
    time 3 1367612868039
    time 2 1367612868039
    time 3 1367612868270
    time 2 1367612868270
    time 3 1367612868506
    time 2 1367612868506
    time 3 1367612868744
    time 2 1367612868745
    time 3 1367612868981
    time 2 1367612868981
    time 3 1367612869214
    time 2 1367612869214
    time 3 1367612869446
    time 2 1367612869447
    time 3 1367612869677
    time 2 1367612869677
    time 3 1367612869908
    time 2 1367612869908
    time 3 1367612870143
    time 2 1367612870143
    time 3 1367612870372
    time 2 1367612870372
    time 3 1367612870592
    time 2 1367612870592
    time 3 1367612870831
    time 2 1367612870832
    time 3 1367612871066
    time 2 1367612871066
    time 3 1367612871289
    time 2 1367612871289
    time 3 1367612871517
    time 2 1367612871517
    time 3 1367612871746
    time 4 1367612871747
    time 1 1367612871752
    time 2 1367612871752
    time 3 1367612871987
    time 2 1367612871987
    time 3 1367612872217
    time 2 1367612872217
    time 3 1367612872447
    time 2 1367612872447
    time 3 1367612872677
    time 2 1367612872677
    time 3 1367612872903
    time 2 1367612872903
    time 3 1367612873138
    time 2 1367612873138
    time 3 1367612873366
    time 2 1367612873366
    time 3 1367612873593
    time 2 1367612873594
    time 3 1367612873819
    time 2 1367612873819
    time 3 1367612874061
    time 2 1367612874061
    time 3 1367612874286
    time 2 1367612874286
    time 3 1367612874507
    time 2 1367612874507
    time 3 1367612874733
    time 2 1367612874733
    time 3 1367612874964
    time 2 1367612874964
    time 3 1367612875217
    time 2 1367612875217
    time 3 1367612875449
    time 2 1367612875449
    time 3 1367612875677
    time 2 1367612875677
    time 3 1367612875930
    time 2 1367612875930
    time 3 1367612876168
    time 2 1367612876169
    time 3 1367612876389
    time 2 1367612876389
    time 3 1367612876624
    time 2 1367612876624
    time 3 1367612876870
    time 2 1367612876871
    time 3 1367612877110
    time 2 1367612877110
    time 3 1367612877339
    time 2 1367612877339
    time 3 1367612877567
    time 2 1367612877567
    time 3 1367612877797
    time 2 1367612877797
    time 3 1367612878032
    time 2 1367612878032
    time 3 1367612878264
    time 2 1367612878264
    time 3 1367612878496
    time 2 1367612878496
    time 3 1367612878726
    time 2 1367612878726
    time 3 1367612878966
    time 2 1367612878966
    time 3 1367612879206
    time 2 1367612879206
    time 3 1367612879443
    time 2 1367612879443
    time 3 1367612879681
    time 2 1367612879681
    time 3 1367612879925
    time 2 1367612879925
    time 3 1367612880162
    time 2 1367612880162
    time 3 1367612880392
    time 2 1367612880392
    time 3 1367612880624
    time 2 1367612880624
    time 3 1367612880854
    time 2 1367612880854
    time 3 1367612881096
    time 2 1367612881097
    time 3 1367612881337
    time 2 1367612881337
    time 3 1367612881576
    time 2 1367612881576
    time 3 1367612881811
    time 2 1367612881811
    time 3 1367612882051
    time 2 1367612882051
    time 3 1367612882291
    time 2 1367612882291
    time 3 1367612882527
    time 2 1367612882528
    time 3 1367612882767
    time 2 1367612882767
    time 3 1367612883007
    time 2 1367612883007
    time 3 1367612883263
    time 2 1367612883263
    time 3 1367612883501
    time 2 1367612883501
    time 3 1367612883774
    time 2 1367612883774
    time 3 1367612884011
    time 2 1367612884012
    time 3 1367612884249
    time 2 1367612884250
    time 3 1367612884487
    time 4 1367612884487
    time 1 1367612884493
    time 2 1367612884493
    time 3 1367612884733
    time 2 1367612884733
    time 3 1367612884972
    time 2 1367612884972
    time 3 1367612885230
    time 2 1367612885231
    time 3 1367612885470
    time 2 1367612885470
    time 3 1367612885707
    time 2 1367612885707
    time 3 1367612885962
    time 2 1367612885962
    time 3 1367612886198
    time 2 1367612886198
    time 3 1367612886420
    time 2 1367612886420
    time 3 1367612886654
    time 2 1367612886655
    time 3 1367612886893
    time 2 1367612886893
    time 3 1367612887136
    time 2 1367612887136
    time 3 1367612887373
    time 2 1367612887374
    __________________________________________________ ________________________
    All other println didnt produce any output
    I also noticed that field that showing seconds on display do it with increment 13 seconds ;Earlier it did it with increment 1 second

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    All other println didnt produce any output
    The only two print outs that are important were time 3 (just before the loop) and time 5 (just after the loop) from post#15
    The rest are not needed. Not need to post times inside the loop.

    What is the time used to excute the loop between time3 and time5?
    The time spent executing in the loop is keeping the GUI from updating.

    When posting code be sure to wrap the code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    I just saw the new locations for the printlns in post#17.
    The printlns inside of the loop are not needed. Just the one before the loop and the one after the loop.

    How long was spent executing the loop? <<<<< Do some math and post an answer to that question.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    Difference between time before loop and time after loop is 13301 (obtained using system.currenttimemillis() )
    Thank you very much
    Edward

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    You should NOT run loops that take 13 seconds to complete in the paint() method.
    What is the loop supposed to do? It should be moved outside the paint() method to another thread where its execution will not slow down the execution of the paint() method.
    I assume you want the paint() method to be executed every 1 second by the calls to repaint() in the loop in the run() method.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    Yes that was initial intention : run 2 independent threads
    One for paint ( exec every 1 second )
    Another thread for loop .
    Can I please ask you to add to this program outline of second thread where i can put loop ?
    Thank you so very much
    Edward

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    The paint() method is called by the jvm on the its EDT thread in response to a call to repaint()
    You do not need a separate thread for executing paint().
    Can the existing run() method call the sound method in addition to calling the repaint() method?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    Yes run() can call sound .How can i do it ?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: thread/timer/sound interaction

    How can i do it ?
    Are you asking how to call the sound method from the run method?
    Calling one method from another method is a very common thing to do. Your code has many examples of that.
    Please explain what problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    May 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread/timer/sound interaction

    when i m trying to call this piece from run() i m getting same problem timer increment on 13 seconds
    Where exactly i have to place it ?

    try {

    for(int i=1; i<=55; i++){
    this.sound60(freq[i],100,0.5);//day
    }

    } catch (LineUnavailableException e) { e.printStackTrace();}

Page 1 of 2 12 LastLast

Similar Threads

  1. replacing thread with timer
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 3rd, 2012, 08:49 AM
  2. How to convert thread.sleep() into a timer in an Applet
    By all_pro in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:45 AM
  3. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM
  4. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM
  5. need help with Timer and sound
    By amahara in forum AWT / Java Swing
    Replies: 4
    Last Post: February 18th, 2010, 12:22 PM

Tags for this Thread