Results 1 to 5 of 5
  1. #1
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default Thread calculations, and another thread to use the calculations

    Hi. I'm calculating a sine wave based on two given values. Now I have a thread calculating the sine wave values, but I'd like to have another thread to use them calculations and send them out through my sound card.

    I'm processing the calculations whilst I'm moving the mouse cursor of an area of my jpanel.

    The sound is really crackly due to the sound card is process the calculations, whilst newer calculations are being sent to it.

    I'm thinking along the lines of:

    Thread 1: calculations
    Thread 2: sound card


    Thread 2 = Locked
    Thread 1 = calculates the values
    Thread 2 = Unlocked

    Thread 1 = Locked
    Thread 1 --- Sends data to Thread 2 (Sound card)
    Thread 1 = Unlocked

    Any hints please if this is possible?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    maybe using the wait and notify api of threads?

    Thread 2: Thread2.wait()
    Thread 1: calculate. Thread2.notify(), Thread1.wait();
    Thread 2: sends data to sound card. Thread1.notify();
    // and repeat

  3. #3
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    i'll try that. I'm not how i'm gonna pass the calculations to the thread controlling processing the sound. i'm passing a byte buffer to the thread that processes the sound

  4. #4
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    This is what I'm trying to calculate:

    Thread 1:

    private void calculateFreq(){
    double RAD = 2.0 * Math.PI;
    for (int i = 0; i < _buf.length; i++) {
    _buf[i] = (byte) (Math.sin(RAD * _frequency / 8000 * i) *_volume);
    }
    }

    Thread 2:

    Sendind the results from that through my sound card. I dunno HOW I will be able to pass the results from that, into Thread 2 to calculate the results.

    All of this is happening within my GUI when I move the mouse over an area, So I guess I'd need to put the Swing thread to sleep too whilst these threads are working?

    Thanks for your help.

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    how about using a PipedInputStream and a PipedOutputStream pair to communicate between the threads. ? where thread 1 does the calculation and write value to the output stream, which is piped to the input stream in thread 2 that is read from.

    For example,
    Java Code:
    package thread;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    import java.util.Random;
    
    public class TwoThreads {
    
    	public static void main(String [] args) {
    		
    		PipedInputStream pin = null;
    		PipedOutputStream pout = null;
    		try {
    			pin = new PipedInputStream();
    			pout = new PipedOutputStream(pin);
    		}
    		catch (IOException ex) {
    			//
    		}
    		
    		Thread1 t1 = new Thread1(pout);
    		Thread2 t2 = new Thread2(pin);
    		
    		Thread[] threads = new Thread[2];
    		threads[0] = new Thread(t1);
    		threads[1] = new Thread(t2);
    		
    		for (Thread t : threads) {
    			t.start();
    		}
    		
    		// just wait for the threads to exit.
    		try {
    			for (Thread t : threads) {
    				t.join();
    			}
    		} 
    		catch (InterruptedException ex) {
    		}
    		System.out.println("finished.");
    		
    	}
    }
    
    /**
     * Performs calculations, writes to output stream
     */
    class Thread1 implements Runnable {
    	OutputStream out;
    
    	public Thread1(OutputStream out) {
    		this.out = out;
    		// pass in other variables and initialize here.
    	}
    	
    	@Override
    	public void run() {
    		// do the read of the input buffer and the calculation here
    		Random r = new Random();
    		try {
    			for (int i = 0; i < 10; i++) {
    				int value = r.nextInt(255);
    				System.out.println("thread1 writing value: " + value);
    				out.write(value);
    			} // sample for loop
    			out.flush();
    		}
    		catch (IOException ex) {
    		
    		}
    		finally {
    			try {
    				out.close();
    			}
    			catch (IOException ex) {
    				// empty
    			}
    		}
    	}
    }
    
    /**
     * Reads stuff from the calculation thread.
     */
    class Thread2 implements Runnable {
    	InputStream in;
    	
    	public Thread2(InputStream in) {
    		this.in = in;	
    	}
    	
    	@Override
    	public void run() {
    		// read from the input stream and write to the soundcard.
    		int value = 0;
    		try {
    		while ( (value = in.read()) != -1) {
    			System.out.println("thread 2 writing value " + value);
    		}
    		} catch (IOException ex) {
    			// unexpected error
    		}
    		finally {
    			try {
    				in.close();
    			}
    			catch (IOException ex) {
    				// empty
    			}
    		
    		}
    	}
    }

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 08:36 PM
  3. [SOLVED] Simple Calculations in Java
    By fullmetaljacket in forum New To Java
    Replies: 9
    Last Post: 05-19-2009, 04:19 AM
  4. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 04:36 AM
  5. Replies: 0
    Last Post: 01-28-2008, 08:02 AM

Posting Permissions

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