PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 1, 2022

[FIXED] How to create a CompletableFuture which waits forever?

 October 01, 2022     concurrency, java     No comments   

Issue

I want to return a CompletableFuture in one method, which will succeed only once a second method of that object is called with the result value, something like

public class Foo {
   private CompletableFuture<String> f;

   public CompletableFuture<String> method1() {
      f = CompletableFuture.waitForever???
      return f; 
   }

   public void method2(String s) {
      if (f != null) { 
         f.complete(s);
      }
   } 
}

Is this possible? Do I overlook a less crazy way to do that?


Solution

You can use the Object.wait() method to achieve this behavior:

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    try {
        synchronized (this) { // needed to prevent an IllegalMonotirStateException (see https://stackoverflow.com/a/1537133/8178842)
            wait();
        }
    }
    catch (InterruptedException e) {
        // should never happen
        throw new RuntimeException(e);
    }
});

A simple UnitTest shows the behavior:

@Test
public void test() {
    // create a CompeltableFuture that just waits
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        try {
            synchronized (this) {// needed to prevent an IllegalMonotirStateException (see https://stackoverflow.com/a/1537133/8178842)
                wait(); // wait till the CompletableFuture.complete method is called (or till this thread is notified, what shouldn't happen)
            }
        }
        catch (InterruptedException e) {
            // should never happen
            throw new RuntimeException(e);
        }
    });
    
    new Thread(() -> {
        try {
            Thread.sleep(3000); // wait for three seconds
        }
        catch (InterruptedException e) {
            // ignore
        }
        
        // complete the future manually after three seconds
        future.complete(null);
    }).start();
    
    try {
        future.get(); // block until the future is completed
    }
    catch (InterruptedException | ExecutionException e) {
        // can be ignored here
    }

    // the future.get() method will be blocking for 3 seconds
}

Because of the suspended thread that completes the CompletableFuture, the test will wait for 3 seconds and then finish waiting.



Answered By - Tobias
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing