Reputation: 4991
I need to perform some medium load on shutdown but in AWS the machine is giving only 2 or less seconds to the thread to perform and only 45-47% of load is consume.
Is there is a way to increase this? or is nothing i can do!
Thread started.
07:22:09.476 [Thread-61] INFO com.company.main.Main.lambda$run$0(307) - Shutdown process start with thread: [Thread-61]
And the last line.
07:22:11.026 [Thread-61] DEBUG c.p.w.company.lambda$sort$1(341)
As you can see like 1.5 seconds is giving to thread to finish and i need to do finish the method :(
We are using EC2!
Sorry if the question is plain but this is giving me nightmares.
The code is not mine but is something like this.
public class Main extends Thread {
In public static method we create some Threads.
for (int i = 0; i < threads; i++) {
Main main = (Main) ctx.getBean("beanName");
main.setName("main-" + i);
main.start();
}
The Main class run method override
@Override
public void run() {
if (StringUtils.equals("main-0", Thread.currentThread().getName())) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
doWork();
}));
}
As you can see we are creating the shutdown on the 1 Main thread created, i did created the shutdown on the main thread as well but the same thing happens.
Upvotes: -1
Views: 264
Reputation: 6412
Normally projects use "transactions" for this kind of cases. You can't really avoid a machine being shutted down, and sometimes you will not even get a notification. The only solution to reliable deal with this problem is to use ATOMIC operations aka "transactions"
There is no way to guarantee a shutdown hook always finish or even being fired at all, otherwise you could hang the shutdown process indefinitally in the shutdown hook. shutdown hook is to give you chance to shutdown gracefully - eg. notify other components of the system so they will not waste time on trying to connect, or to maybe try to save something, which might be usefull. But this can be used for kind of shutdown optimization to be as gracefull as possible, but it is not for relying on it for data or operation integrity. That's it, and if you will think about it it makes perfect sense.
Upvotes: -2