I'm developing an application in JavaFX who "scan" Mp3 files to get ID3tag.
Actually my program works, but the application is slow when there is a lot of songs (mp3).
I use a thread who scan every files from an list of mp3files (path).
I'm wondering if it's possible to use more threads to scan every files 4 by 4 rather than 1 by 1 ?
I'm low with multi-threading from java so I ask for your help. Any advice would be great.
Here is my function :
private ArrayList checkMp3files(String sDir)
{
this.currentData = 0;
this.filesCorrupted.clear();
ArrayList<DataSong> newLs = new ArrayList<>(); // ArrayList with Data from all Songs scanned
Task taskScan = new Task<ArrayList<DataSong>>() {
@Override
protected ArrayList<DataSong> call() throws InterruptedException, IOException, UnsupportedTagException
{
String absoluteLoc;
String location;
for(String mp3file : Mp3FileData)
{
DataSong ds = new DataSong();
updateMessage("Scanning " + ++currentData + " of " + nbrOfFiles + " Mp3files");
updateProgress(currentData, nbrOfFiles);
try {
mp3 = new Mp3File(mp3file);
} catch (InvalidDataException ex) {
filesCorrupted.add(mp3file);
}
long durationLong = mp3.getLengthInSeconds();
int durationInt = (int) durationLong;
ds.setLenghtOfMp3inSec(durationInt);
ds.setBitRateOfMp3(mp3.getBitrate());
ds.setSampleRate(mp3.getSampleRate());
ds.setVbrOrCbr(mp3.isVbr());
if(mp3 != null){
ds.setAbsoluteLocation(mp3.getFilename());
ds.setLocation(removeSDir(mp3.getFilename(), sDir));
if(mp3.hasId3v2Tag())
{
String artist = "";
String album = "";
String title = "";
String track = "";
String year = "";
String genDesc = "";
String composer = "";
String publisher = "";
String originalArtist = "";
String albumArtist = "";
String copyright = "";
String url = "";
ID3v2 id3v2Tag = mp3.getId3v2Tag();
try{
artist = id3v2Tag.getArtist();
album = id3v2Tag.getAlbum();
title = id3v2Tag.getTitle();
track = id3v2Tag.getTrack();
year = id3v2Tag.getYear();
genDesc = id3v2Tag.getGenreDescription();
composer = id3v2Tag.getComposer();
publisher = id3v2Tag.getPublisher();
originalArtist = id3v2Tag.getOriginalArtist();
albumArtist = id3v2Tag.getAlbumArtist();
copyright = id3v2Tag.getCopyright();
url = id3v2Tag.getUrl();
}
catch(Exception ex){
System.out.println(mp3file);
filesCorrupted.add(mp3file);
}
if(!(artist == null) && !(artist.isEmpty()))
{
ds.setArtist(artist);
}
if(!(album == null) && !(album.isEmpty()))
{
ds.setAlbum(album);
}
if(!(title == null) && !(title.isEmpty()))
{
ds.setTitle(title);
}
if(!(track == null) && !(track.isEmpty()))
{
ds.setTrackOnAlbum(track);
}
if(!(year == null) && !(year.isEmpty()))
{
ds.setYearReleased(year);
}
if(!(genDesc == null) && !(genDesc.isEmpty()))
{
ds.setGenre(genDesc);
}
if(!( composer == null) && !(composer.isEmpty()))
{
ds.setComposer(id3v2Tag.getComposer());
}
if(!(publisher == null) && !(publisher.isEmpty()))
{
ds.setPublisher(publisher);
}
if(!(originalArtist == null) && !(originalArtist.isEmpty()))
{
ds.setOriginArtist(originalArtist);
}
if(!(albumArtist == null) && !(albumArtist.isEmpty()))
{
ds.setAlbumArtString(albumArtist);
}
if(!(copyright == null) && !(copyright.isEmpty()))
{
ds.setCopyright(copyright);
}
if(!(url == null)&& !(url.isEmpty()))
{
ds.setUrl(url);
}
}
}
newLs.add(ds);
}
}
return newLs;
}
};
btnScan.disableProperty().bind(taskScan.runningProperty());
listAdvance.textProperty().bind(taskScan.messageProperty());
pi.progressProperty().bind(taskScan.progressProperty());
taskScan.stateProperty().addListener(new ChangeListener<Worker.State>(){
@Override
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue)
{
if(newValue == Worker.State.SUCCEEDED)
{
System.out.println("Error : " + taskScan.getException());
listAdvance.textProperty().unbind();
btnScan.disableProperty().unbind();
}
if(newValue == Worker.State.FAILED)
{
listAdvance.textProperty().unbind();
btnScan.disableProperty().unbind();
System.out.println("Error : " + taskScan.getException());
}
}
});
Thread toScanFiles = new Thread(taskScan);
toScanFiles.start();
return newLs;
}
I already thank you for your answers.