Results 1 to 2 of 2
  1. #1
    edwardone123 is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default I believe it is a thread problem

    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
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: I believe it is a thread problem

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. problem with thread
    By ktden in forum New To Java
    Replies: 7
    Last Post: 04-03-2013, 11:51 PM
  2. thread problem
    By rowanabejjani in forum New To Java
    Replies: 3
    Last Post: 06-02-2011, 07:48 PM
  3. Thread Problem
    By zhein04 in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-07-2011, 12:12 AM
  4. Thread problem.
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 03-16-2010, 04:19 PM
  5. Problem with a thread
    By dotabyss in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-14-2010, 10:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •