Skip to content

Commit

Permalink
A few synchronization issues identified and fixed
Browse files Browse the repository at this point in the history
- SimpleDateFormat and DecimalFormat are not thread-safe, they need to be synchronized externally (cloning in this case)
- StringBuffer is not needed in local variables, replaced with StringBuilder
- CopyOnWriteArraySet is thread-safe, no need to synchronize method which is touching it
  • Loading branch information
bonifaido committed Mar 5, 2015
1 parent 6be0246 commit 3cb4508
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 27 deletions.
10 changes: 3 additions & 7 deletions src/main/java/com/exxeleron/qjava/QCallbackConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public void close() throws IOException {
* already exists, nothing happens.
*/
public synchronized void startListener() {
if ( messageListener == null ) {
messageListener = new QListener();
listenerThread = new Thread(messageListener, "qJava-listener" + this.toString());
listenerThread.start();
}
startListener("qJava-listener" + this.toString());
}

/**
Expand Down Expand Up @@ -128,7 +124,7 @@ public synchronized void stopListener() {
* @param listener
* a {@link QMessagesListener} to be registered
*/
public synchronized void addMessagesListener( final QMessagesListener listener ) {
public void addMessagesListener( final QMessagesListener listener ) {
messagesListeners.add(listener);
}

Expand All @@ -138,7 +134,7 @@ public synchronized void addMessagesListener( final QMessagesListener listener )
* @param listener
* a {@link QMessagesListener} to be unregistered
*/
public synchronized void removeMessagesListener( final QMessagesListener listener ) {
public void removeMessagesListener( final QMessagesListener listener ) {
messagesListeners.remove(listener);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/exxeleron/qjava/QDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static QDate fromString( final String date ) {
}
}

private static synchronized DateFormat getDateformat() {
return dateFormat;
private static DateFormat getDateformat() {
return (DateFormat) dateFormat.clone();
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/exxeleron/qjava/QDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static QDateTime fromString( final String date ) {
}
}

private static synchronized DateFormat getDateformat() {
return dateFormat;
private static DateFormat getDateformat() {
return (DateFormat) dateFormat.clone();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/exxeleron/qjava/QDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public boolean equals( final Object obj ) {
if ( areValuesArray ) {
return Utils.deepArraysEquals(keys, d.keys) && Utils.deepArraysEquals(values, d.values);
} else {
return Utils.deepArraysEquals(keys, d.keys) && ((QTable) values).equals(d.values);
return Utils.deepArraysEquals(keys, d.keys) && values.equals(d.values);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/exxeleron/qjava/QMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static QMonth fromString( final String date ) {
}
}

private static synchronized DateFormat getDateformat() {
return dateFormat;
private static DateFormat getDateformat() {
return (DateFormat) dateFormat.clone();
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/exxeleron/qjava/QTimespan.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Date toDateTime() {
public String toString() {
final Date dt = toDateTime();
return dt == null ? NULL_STR : (value < 0 ? "-" : "") + (Math.abs(value) / NANOS_PER_DAY) + getDateformat().format(dt)
+ nanosFormatter.format(Math.abs(value) % NANOS_PER_SECOND);
+ getNanosformat().format(Math.abs(value) % NANOS_PER_SECOND);
}

/**
Expand Down Expand Up @@ -159,11 +159,11 @@ public static QTimespan fromString( final String date ) {
}
}

private static synchronized DateFormat getDateformat() {
return dateFormat;
private static DateFormat getDateformat() {
return (DateFormat) dateFormat.clone();
}

private static synchronized NumberFormat getNanosformat() {
return nanosFormatter;
private static NumberFormat getNanosformat() {
return (NumberFormat) nanosFormatter.clone();
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/exxeleron/qjava/QTimestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Date toDateTime() {
@Override
public String toString() {
final Date dt = toDateTime();
return dt == null ? NULL_STR : getDateformat().format(dt) + nanosFormatter.format(value % NANOS_PER_SECOND);
return dt == null ? NULL_STR : getDateformat().format(dt) + getNanosformat().format(value % NANOS_PER_SECOND);

}

Expand Down Expand Up @@ -154,11 +154,11 @@ public static QTimestamp fromString( final String date ) {
}
}

private static synchronized DateFormat getDateformat() {
return dateFormat;
private static DateFormat getDateformat() {
return (DateFormat) dateFormat.clone();
}

private static synchronized NumberFormat getNanosformat() {
return nanosFormatter;
private static NumberFormat getNanosformat() {
return (NumberFormat) nanosFormatter.clone();
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/exxeleron/qjava/QWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ public void writeLongBigEndian( final long value ) {
final byte[] arr = new byte[] { (byte) ((value >> 56) & 0xff), (byte) ((value >> 48) & 0xff), (byte) ((value >> 40) & 0xff),
(byte) ((value >> 32) & 0xff), (byte) ((value >> 24) & 0xff), (byte) ((value >> 16) & 0xff),
(byte) ((value >> 8) & 0xff), (byte) ((value >> 0) & 0xff) };
for ( int i = 0; i < arr.length; i++ ) {
writeByte(arr[i]);
for (byte anArr : arr) {
writeByte(anArr);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/exxeleron/qjava/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static String arrayToString( final Object list ) {
return "[]";
} else {
final int length = Array.getLength(list);
final StringBuffer buffer = new StringBuffer("[");
final StringBuilder buffer = new StringBuilder("[");

Object obj = Array.get(list, 0);
buffer.append(obj == null ? null : obj.getClass().isArray() ? arrayToString(obj) : obj);
Expand Down

0 comments on commit 3cb4508

Please sign in to comment.