Skip to content

Instantly share code, notes, and snippets.

@murayama333
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murayama333/c1fe600d406ae8bf3e04 to your computer and use it in GitHub Desktop.
Save murayama333/c1fe600d406ae8bf3e04 to your computer and use it in GitHub Desktop.
package com.example.app;
import java.util.Random;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Response;
@Path("/async")
public class AsyncResource {
@GET
public void asyncGet(@Suspended AsyncResponse asyncResponse) {
new Thread(() -> {
int sleepMillis = sleep();
log("runnable thread end.(" + sleepMillis + ")");
Response response = Response.ok("Success (" + sleepMillis + ")").build();
asyncResponse.resume(response);
}).start();
log("server thread end.");
}
private int sleep() {
try {
int millis = new Random().nextInt(5) * 1000;
Thread.sleep(millis);
return millis;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private void log(String message) {
System.out.println(message
+ " / " + Thread.currentThread().getId()
+ " / " + Thread.currentThread().getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment