0

I have a use case where I am continuously testing a workflow. I have an entry made to a mysql database to store a student record, retrieve it, update it with some details, and then finally delete it.

In order to test these workflows, I have each of these steps as separate methods. Now, in order to test it continuously, I am possibly thinking of having the entire workflow execute in a multi threaded model.

The difficulty I am having is that how I have the other operations (get, update and delete) wait till the create operation is complete. I was looking up Thread.join() and notify methods but I was unsure if that's the most elegant way to do so.

Here is a sample (pretty naive version) of my code I have so far:

public class Test {
    public static void main(String[] args) {

        while (true) {
            String studentId = createStudent();
            Thread.sleep(1000);
            Runnable calls[] = new Runnable {
                () -> getStudent(studentId),
                () -> updateStudent(studentId),
                () -> delete(studentId)
            };

            for (Runnable call: calls) {
                call.run();
            }
        }

    }

    private String createStudent() {
        ...
        ...
        ...
        return studentId;
    }

    private void getStudent(String studentId) 
        ...
        ...
        ...
    }

    private void updateStudent(String studentId) 
        ...
        ...
        ...
    }

    private void deleteStudent(String studentId) 
        ...
        ...
        ...
    }

}

I am pretty sure my way of introducing sleep is a pretty naive implementation and is definitely not recommended as well. If I had to use notify or join, is there a best practice suggestion I can look up?

chrisrhyno2003
  • 3,906
  • 8
  • 53
  • 102
  • Some suggestions on how to do this here: http://stackoverflow.com/questions/8799373/waiting-for-a-runnable-to-complete-before-running-another-runnable – tima May 15 '17 at 03:18

1 Answers1

0

This can be solve by thread wait and notify thread communication way or using countdown latch.Below example explain countdown latch way.

public class ConditionAwaiter {

    private final Callable<Boolean> asyncCondition;
    private final Condition condition;
    private final CountDownLatch latch;
    private Throwable throwable = null;

    public ConditionAwaiter(Callable<Boolean> asyncCondition, Condition condition) {
        this.asyncCondition = asyncCondition;
        this.condition = condition;
        this.latch = new CountDownLatch(1);
    }

    public void await() {
        boolean processCompleted = false;
        try {
            new WaitThread(asyncCondition, condition).start();
            Duration maxWaitTime = condition.getWaitDuration();

            if (maxWaitTime.getValue() == WaitStrategy.WAIT_FOREVER) {
                latch.await();
                processCompleted = true;
            } else {
                processCompleted = latch.await(maxWaitTime.getValue(), maxWaitTime.getTimeUnit());
            }

        } catch (Exception e) {
            throwable = e;
        } finally {
            if (throwable != null || !processCompleted) {
                throw new IllegalStateException("Not able to complete on time");

            }
        }
    }

    class WaitThread extends Thread {
        private final Callable<Boolean> asyncCondition;
        private final Condition condition;
        private Duration pollInterval;
        private Lock lock = new ReentrantLock();
        private java.util.concurrent.locks.Condition lockCondition = lock.newCondition();

        public WaitThread(Callable<Boolean> asyncCondition, Condition condition) {
            this.asyncCondition = asyncCondition;
            this.condition = condition;
            pollInterval = condition.getPollInterval();
        }

        @Override
        public void run() {
            lock.lock();
            try {
                try {
                    while (!asyncCondition.call()) {
                        lockCondition.await(pollInterval.getValue(), pollInterval.getTimeUnit());
                    }
                } catch (InterruptedException e) {

                }

                if (asyncCondition.call()) {
                    latch.countDown();
                }
            } catch (Exception e) {
                throwable = e;
            } finally {
                lock.unlock();
            }
        }

    }

}
gati sahu
  • 2,576
  • 2
  • 10
  • 16