Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Is Your Java Eclipse Plugin Thread Safe?

DZone's Guide to

Is Your Java Eclipse Plugin Thread Safe?

A look at race conditions, what they are, and how they work inside Eclipse Luna.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

...Or Does It Contain Race Conditions?

A race condition happens when the outcome of the program depends on the sequence or timing of other uncontrollable events. It becomes a bug when events do not happen in the order the programmer intended. https://en.wikipedia.org/wiki/Race_condition

We searched inside Eclipse for race conditions to see the most common types of race conditions inside Eclipse and its plugins.

The following types of race conditions were found by vmlens inside Eclipse Luna during startup and debugging of a jJava project:

races

No Synchronization At All

The most common cause for race conditions was accessing the same field from different threads without any synchronization at all.

Object

Count

Concurrently Accessed Fields

2065

Fields Traced

27114

Monitors

7162

Locks

427

Threads

52

Volatile Fields

2836

During this run 2065 different fields were accessed by more than one thread, 4 of them without synchronization.

2836 volatile fields were used. For 3 more fields it would have been necessary to declare them as volatile. This leads to the second type of race conditions found, visibility problems.

Visibility

A field is accessed by many threads, but not declared as volatile.

The JVM does not directly write field updates into the main memory but first in registers or a cache of your CPU. As long as your program is running on one core of your PC, this is not a problem. But if the threads runs on different cores, they probably will not see the updates to the fields.

This problem appears most often with boolean flags. Like the terminated field of org.eclipse.equinox.internal.util.impl.tpt.timer.TimerImpl in the run method:

public void run() {
        TimerQueueNode n = null;
        while (!terminated) {
            synchronized (sync) {
                if (n == null && queue.isEmpty()) {
                    try {
                        sync.wait();
                    } catch (Exception e) {
                    }
                    // todo check if isEmpty is necessary
                    if (queue.isEmpty() || terminated) {
                        continue;
                    }
                }
            }

Conclusion

For 2065 concurrently accessed fields, vmlens found 7 race conditions. All other were correctly synchronized by 7162 monitors or declared as volatile.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,eclipse 4 ,race conditions

Published at DZone with permission of Thomas Krieger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}