Results 1 to 5 of 5
- 12-12-2010, 01:34 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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?
- 12-12-2010, 02:00 AM #2
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
- 12-12-2010, 02:10 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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
- 12-12-2010, 01:42 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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.
- 12-12-2010, 04:32 PM #5
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
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
how to reduce the thread sleep time and wake up the thread
By baktha.thalapathy in forum Threads and SynchronizationReplies: 2Last Post: 06-24-2010, 07:36 PM -
[SOLVED] Simple Calculations in Java
By fullmetaljacket in forum New To JavaReplies: 9Last Post: 05-19-2009, 03:19 AM -
decimal calculations?
By arnab321 in forum CLDC and MIDPReplies: 5Last Post: 11-19-2008, 03:36 AM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks

Bookmarks