Kenjii
Kenjii

Reputation: 1

Demonstrate that this class is thread safe?

I have to prove (using code) that a class (in a .jar file) is NOT thread safe.

I understand what the question is asking, I'm just not sure how to approach the coding portion of this question.

Upvotes: 0

Views: 163

Answers (2)

Stephen C
Stephen C

Reputation: 719446

A proof that the class is not thread-safe could simply be some code that demonstrates that it can make the class behave in a non-thread-safe way. (Proof by existence.)

For example, this might be a program with two threads:

  • One thread sets the counter to N and sleeps.
  • A second thread decrements the counter N times
  • The first thread wakes up and tests if the counter is depleted.

Repeat this a few (hundred, thousand, million) times

If there is a thread-safety problem, then the depleted test may give the wrong answer.

But note that black-box testing for thread-safety issues like this is a bit hit and miss:

  • The actual behavior of the test is liable to vary depending on the hardware platform, etc.

  • If the code under test is treated as a black box, you have no way of knowing for sure what thread-safty issues it might have, or how (exactly) to trigger them.

  • When designing a test like this, it is easy do something that does some kind of serendipitous synchronization ... which could prevent the thread-safety issue from manifesting itself.

The fact that you can't clearly demonstrate thread-safety issues does not mean they are not there. You can't prove that code is thread-safe by testing it.


Finally, the API design clearly is not reentrant. Since the methods are static, there is only one counter1 and it can only be used for one thing at a time. However, that isn't a thread-safety issue. I would call that2 an API design / usability issue rather than a thread-safety issue.

1 - ... unless the implementation is really weird; e.g. is uses thread-local state. However, that ought to be documented in the API spec.

2 - If the person who set the exam question was not really up-to-speed with accepted concurrency terminology, they might disagree with me on that.

Upvotes: 4

thatguy
thatguy

Reputation: 22119

As Stephen C has already given a comprehensive answer, I want to provide a Java example for his proposal and the methods you have provided:

public class Tester {

    private static int value;

    public static void main(String[] args) {

        Runnable decrementRunnable = new Runnable() {
            @Override
            public void run() {

                while (!depleted()) {
                    decrement();
                }

            }
        };

        set(10);

        Thread decrementThread = new Thread(decrementRunnable);
        decrementThread.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (depleted()) {
            System.out.println("Not thread-safe.");
        }

    }

    private static void set(int i) {
        value = i;
    }

    private static boolean depleted() {
        return value <= 0;
    }

    private static void decrement() {
        value--;
    }

}

Upvotes: 0

Related Questions