Skip to content

Commit

Permalink
Try to fix Potential Thread Synchronization Issue in LoadSeries.java #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Dec 27, 2024
1 parent b616d49 commit 229e03e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public final int getImageIndex(E source, Filter<E> filter, Comparator<E> sort) {
return -1;
}
Iterable<E> list = getMedias(filter, sort);
synchronized (this) {
synchronized (medias) {
int index = 0;
for (E e : list) {
if (e == source) {
Expand Down Expand Up @@ -242,8 +242,7 @@ public final E getMedia(int index, Filter<E> filter, Comparator<E> sort) {
}

@Override
public void dispose() {
// forEach implement synchronized
public synchronized void dispose() {
medias.forEach(
m -> {
if (m instanceof ImageElement imageElement) {
Expand Down Expand Up @@ -319,10 +318,8 @@ public void firePropertyChange(final ObservableEvent event) {
}

@Override
public int size(Filter<E> filter) {
synchronized (this) {
return filter == null ? medias.size() : Filter.size(filter.filter(medias));
}
public synchronized int size(Filter<E> filter) {
return filter == null ? medias.size() : Filter.size(filter.filter(medias));
}

@Override
Expand Down Expand Up @@ -416,10 +413,9 @@ public void setFocused(boolean focused) {

public boolean hasMediaContains(TagW tag, Object val) {
if (val != null) {
synchronized (this) {
synchronized (medias) {
for (E media : medias) {
Object val2 = media.getTagValue(tag);
if (val.equals(val2)) {
if (val.equals(media.getTagValue(tag))) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -140,6 +141,7 @@ public enum Status {

private final AtomicInteger errors;
private volatile boolean hasError = false;
private final AtomicBoolean seriesInitialized = new AtomicBoolean(false);

public LoadSeries(
DicomSeries dicomSeries,
Expand Down Expand Up @@ -858,6 +860,10 @@ private File getDicomTmpDir() {

/** Download file. */
private boolean process() throws IOException, URISyntaxException {
boolean firstImage =
dicomSeries != null
&& dicomSeries.size(null) == 0
&& seriesInitialized.compareAndSet(false, true);
boolean cache = true;
File tempFile = null;
DicomMediaIO dicomReader = null;
Expand Down Expand Up @@ -904,7 +910,7 @@ private boolean process() throws IOException, URISyntaxException {
FileUtil.safeClose(stream);

dicomReader = new DicomMediaIO(tempFile);
if (dicomReader.isReadableDicom() && dicomSeries.size(null) == 0) {
if (dicomReader.isReadableDicom() && firstImage) {
// Override the group (patient, study and series) by the dicom fields except the UID of
// the group
MediaSeriesGroup patient = dicomModel.getParent(dicomSeries, DicomModel.patient);
Expand Down Expand Up @@ -941,7 +947,7 @@ private boolean process() throws IOException, URISyntaxException {
final DicomMediaIO reader = dicomReader;
// Necessary to wait the runnable because the dicomSeries must be added to the
// dicomModel before reaching done() of SwingWorker
GuiExecutor.invokeAndWait(() -> updateUI(reader));
GuiExecutor.invokeAndWait(() -> updateUI(reader, firstImage));
} else if (reading == Reading.ERROR) {
errors.incrementAndGet();
}
Expand Down Expand Up @@ -1098,15 +1104,13 @@ public int writFile(InputStream in, File tempFile, int[] overrideList)
}
}

private void updateUI(final DicomMediaIO reader) {
boolean firstImageToDisplay;
private void updateUI(final DicomMediaIO reader, boolean firstImageToDisplay) {
Function<DicomSpecialElementFactory, DicomSpecialElement> buildSpecialElement =
factory -> factory.buildDicomSpecialElement(reader);

DicomMediaIO.ResultContainer result = reader.getMediaElement(buildSpecialElement);
DicomImageElement[] medias = result.getImage();
if (medias != null) {
firstImageToDisplay = dicomSeries.size(null) == 0;
if (firstImageToDisplay) {
MediaSeriesGroup patient = dicomModel.getParent(dicomSeries, DicomModel.patient);
if (patient != null) {
Expand Down

0 comments on commit 229e03e

Please sign in to comment.