跳至主要内容

Concurrency: How to Wait

Basic wait

When we want to coordinate multiple threads to run in order, there are many useful synchronizer classes in Java library for us to use, like Semaphore, CyclicBarrier, CountDownLatch etc.

A common usage of synchronizer class is consumer and producer problem. If we decide to use some basic method from internal lock, we can do it like following:

// thread one
while (ready) {
    obj.wait();
}

// thread two
ready = true;
obj.notify(); // or, depends on situation, obj.notifyAll();

Or we can use explicit lock: Lock & Condition to do similar thing.

condition.await();
condition.signal();

And the above code is more a practice than a real usage. If you really need to write a producer and consumer solution, BlockingQueue may be more convenient to do it.

waitForAny

When we get familiar with the common use of wait, we are going to implement waitForAny utility function based on other library code of java.

Definition

As what we always do, we have to define our problem before to solve it. Here we are going to implement the function with following signature:

/**
 * wait until any object invoke notify/notifyAll
 * @return index of which object that was notified
 */
int waitForAny(Object[] objs) {
}

Simple thought

The simple thought is the translation of the function name: let each object wait on a thread, and if any one is notified, it can cancel all waiting and return from this method.
But it is easier said than done. We have a lot of details to cover:

  • First, we have to let the caller thread wait.
  • Second, we have to let each object wait on separate thread.
  • Next, we notify the caller thread to run.
  • Finally, caller thread cancel all waiting thread when any object is notified.

And the first and second steps should be atomic operation, or we may miss some notification before we starting with waiting. So we have to choose a giant lock to protect waitForAny and notify. For the convenience, we just choose the class which contain this method as the lock.
Then we have the following method skeleton:

public synchronized int waitForAny() {
    // finish step one -> four
    return 0;
}

public synchronized void notify(Object o) {
    o.notify();
}

We can use executors to run multiple jobs at the same time, and we wait on each object’s lock in separated thread like following code:

for (int i = 0; i < objects.length; i++) {
    int finalI = i;
    service.submit(() -> {
        synchronized (objects[finalI]) {
            objects[finalI].wait();
        }
        // (1) notify caller here
        return null;
    });
}
// (2) caller wait here

// cancel other waiting to release thread
for (Object object : objects) {
    notify(object);
}        

What should be noticed is that we should not use Executors.newFixedThreadPool() to run waiting jobs, because if more jobs than thread, some jobs will not start until others are finished. And if the object that being notified doesn’t start waiting, we will be in deadlock, waiting forever.

Now, we almost finish the code, we only need a synchronizer class to wait and notify. We have a lot of choices here (Object, Semaphore etc. ), but we just show some of them.

CountDownLatch

This is so handy to handle this case, you can think it as the latch to stop caller. When any object end the waiting, you release the latch, and caller is free to continue.

CountDownLatch latch = new CountDownLatch(1);

// (1) notify caller here
latch.countDown();

// (2) caller wait here
latch.await();

AQS

The AQS is the abbreviation of AbstractQueuedSynchronizer, which is the class used to implement many synchronizer class in Java. So we can definitely use it to finish the job. You can try to understand how AQS works and define you customized synchronizer.

invokeAny

We finish the job using synchronizer to get familiar the library class. But for this problem, executor has more clear way to do it:

List<Callable<Void>> jobs = new ArrayList<>(objects.length);
for (Object object : objects) {
    jobs.add(() -> {
        synchronized (object) {
            object.wait();
        }
        return null;
    });
}
service.invokeAny(jobs);

Because invokeAny is blocked until any job is finished and it will cancel other automatically, which just what we need, this code is much more clear.

Practice

How to implement waitForAll(i.e. wait until all objects invoke notify)?

Some trials can be found in my git repo and welcome your advice.

Discussion

Do you have any other ideas? Share us in comment.

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (