Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multi-threading



change the following:


Thread newThread = new Thread(new Runnable(){

public void run() {
// updateLabel.setText("Merging Audios - " + (int)(percentDoneMax *
100) + "%
complete");

setValue((int)(percentDoneMax * MAXIMUM));
}

});
SwingUtilities.invokeLater(newThread);
repaint();


to:

Runnable r = new Runnable(){

public void run() {
// updateLabel.setText("Merging Audios - " + (int)(percentDoneMax * 100) + "%complete");
setValue((int)(percentDoneMax * MAXIMUM));
repaint;
}
};


SwingUtilities.invokeLater(r);

that is use just a Runnable, not a Thread (since you never "start" it and just call it's run method instead). I don't remember if Runnable was a class or an interface, but anyway it should work, since you're creating a new anonymous class there (maybe won't work with Java1.0, but with 1.1+ it should)

also, don't call repaint in the callback code, but in the invokelater code, as I do above. Also, try removing the repaint altogether, since I'd expect JSlider to call it by itself (it's a JavaBean afterall, it should handle repaint itself)

----- Original Message ----- From: <email@hidden>
To: <email@hidden>; <email@hidden>
Sent: Tuesday, October 25, 2005 5:30 PM
Subject: RE: Multi-threading



I am still having the same problem. I am including a cut down version of my
code. Hopefully I am just making a stupid mistake that can easily be corrected.


thanks,

Courtney

package AudioMerger;

import java.io.File;

import javax.swing.JFrame;

import quicktime.QTException;
import quicktime.QTSession;
import quicktime.io.OpenMovieFile;
import quicktime.io.QTFile;
import quicktime.std.StdQTConstants;
import quicktime.std.StdQTException;
import quicktime.std.movies.Movie;
import ui.AudioMergerProgressBar;

public class AudioMerger implements Runnable{

// private objects and variables
private Movie splicedMovie;

private AudioMergerProgressBar progressBar;

public AudioMerger() {
}

/**
* Returns the AudioMergerProgressBar that is being updated by QT
*
* @return
*/
public AudioMergerProgressBar getProgressBar() {
return progressBar;
}

/**
* Sets the AudioMergerProgressBar to be used by QT in saving out the audio
file.
*
* @param progressBar
*/
public void setProgressBar(AudioMergerProgressBar progressBar) {
this.progressBar = progressBar;
}

/**
* Adds the given 'addFile' to the end of the current audio.
*
* @param addFile
* @throws QTException
* @throws StdQTException
*/
public void addAudio(File addFile) throws QTException, StdQTException {
Movie addMovie;
QTFile addqtfile = new QTFile(addFile);
OpenMovieFile addOpenMovieFile = OpenMovieFile.asRead(addqtfile);
addMovie = Movie.fromFile(addOpenMovieFile);
addMovie.insertSegment(splicedMovie, 0, addMovie.getDuration(),
splicedMovie.getDuration());
}

/**
* Saves out the audio file that has been created.
*
* @param saveFileName
* @throws QTException
*/
public void saveAudio(File saveFileName) throws QTException {
saveFileName.delete();
int flags = StdQTConstants.createMovieFileDeleteCurFile
| StdQTConstants.createMovieFileDontCreateResFile
| StdQTConstants.showUserSettingsDialog;
splicedMovie.setProgressProc(progressBar);
splicedMovie.convertToFile(new QTFile(saveFileName),
StdQTConstants.kQTFileTypeAIFF,
StdQTConstants.kMoviePlayer, flags);
}

/**
* Merges all of the audioFiles into a new audio and saves it out to the given
pathName + fileName
*
* @param audioFiles
* @param pathName
* @param fileName
* @throws QTException
*/
public void mergeAudioFiles(File audioFiles[], String pathName,
String fileName) throws QTException {
mergeAudioFiles(audioFiles, new File(pathName + File.separator
+ fileName));
}


/**
* Merges all of the audioFiels into a new audio and saves it out to the given
outputFile
*
* @param audioFiles
* @param outputFile
* @throws QTException
*/
public void mergeAudioFiles(File audioFiles[], File outputFile)
throws QTException {
QTSession.open();
splicedMovie = new Movie();
for (int i = 0; i < audioFiles.length; i++) {
addAudio(audioFiles[i]);
}


saveAudio(outputFile);
QTSession.close();
}

public static void main(String args[]){
JFrame myFrame = new JFrame("Audio Merger");
AudioMergerProgressBar progressBar = new AudioMergerProgressBar();
myFrame.getContentPane().add(progressBar);
AudioMerger audioMerger = new AudioMerger();
audioMerger.setProgressBar(progressBar);
myFrame.show();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread t = new Thread(audioMerger);
t.setPriority(Thread.MIN_PRIORITY);
t.start();
}

public void run() {
String home = System.getProperty("user.home") + File.separator;
File manyFiles[] = new File[]{new File(home + "audio1.aif"), new File(home +
"audio2.aif")};
File outputFile = new File(home + "outputAudio.aif");
try {
mergeAudioFiles(manyFiles, outputFile);
} catch (QTException e) {
e.printStackTrace();
}
}
}



package ui; import java.awt.FlowLayout;

import javax.accessibility.AccessibleContext;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;

import quicktime.std.movies.Movie;
import quicktime.std.movies.MovieProgress;
/**
*
*/

/**
* @author courtney
*
*/
public class AudioMergerProgressBar extends JProgressBar implements
MovieProgress {

// Final constants
public final int MINIMUM = 0;
public final int MAXIMUM = 1000;

// private declarations
private JLabel updateLabel = null;
private float percentDoneMax;

/**
* Constructor for the AudioSplicerProgressBar Class
*
*/
public AudioMergerProgressBar(){
super();
setMinimum(MINIMUM);
setMaximum(MAXIMUM);
percentDoneMax = 0;
updateLabel = new JLabel(" ");
setLayout(new FlowLayout());
}

public JLabel getLabel(){
return updateLabel;
}

/* (non-Javadoc)
* @see quicktime.std.movies.MovieProgress#execute(quicktime.std.movies.Movie,
int, int, float)
*/
public int execute(Movie arg0, int arg1, int arg2, float percentDone) {
System.out.println("execute: " + percentDone);


// updateLabel.setText("Merging Audios - " + (int)(percentDoneMax * 100) + "%
complete");
if(percentDoneMax > percentDone)
return 0;
percentDoneMax = percentDone;


Thread newThread = new Thread(new Runnable(){

public void run() {
// updateLabel.setText("Merging Audios - " + (int)(percentDoneMax * 100) + "%
complete");


setValue((int)(percentDoneMax * MAXIMUM));
}

});
SwingUtilities.invokeLater(newThread);
repaint();
return 0;
}

public JLabel getUpdateLabel(){
return updateLabel;
}
}


Quoting Thor Kristensen <email@hidden>:

Hi Courtney,

If you want to keep a responsive GUI, you have to set the priority of the
new thread lower than the Request Dispatching Thread (RDT).
I.e.
Thread t = new Thread(new MyRunnableMovieExporter(ArrayList someArgs));
t.setPriority(Thread.MIN_PRIORITY);
t.start();
Continue work in the main thread.
Actually there are several different priority levels to choose from (see the
SUN API), but if you want to be sure that the levels work, stick to the
constants in Thread.



Regards Thor

-----Original Message-----
From: quicktime-java-bounces+tk=email@hidden
[mailto:quicktime-java-bounces+tk=email@hidden] On Behalf
Of email@hidden
Sent: 25. oktober 2005 03:44
To: email@hidden
Subject: Multi-threading


I am having an issue while multithreading. When I try to export a movie to a
file (the exporting is handled by a new thread), my UI will lockup (i.e.
spinning wheel cursor) until the movie has finished exporting. Is this just
part of the beast? Or is there another step that I must take in
multi-threading an application?


thanks,

Courtney
_______________________________________________
Do not post admin requests to the list. They will be ignored.
QuickTime-java mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quicktime-java/email@hidden

This email sent to email@hidden





_______________________________________________ Do not post admin requests to the list. They will be ignored. QuickTime-java mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/quicktime-java/email@hidden

This email sent to email@hidden



_______________________________________________
Do not post admin requests to the list. They will be ignored.
QuickTime-java mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quicktime-java/email@hidden

This email sent to email@hidden
References: 
 >RE: Multi-threading (From: "Thor Kristensen" <email@hidden>)
 >RE: Multi-threading (From: email@hidden)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.