2

I have java app which supports multiple workflows. Workflow is chosen using arguments passed to it from command line. In one of the workflow app needs to run for infinite time. I am achieving the same using following code

switch (args[0]) { 
    case "-runForever":
     // Some Computation
     Thread.sleep(Long.MAX_VALUE);
     break;
    case "otherCase:
     //dosomething
     break;
}

Is it a good way of achieving the required functionality?

xlm
  • 6,854
  • 14
  • 53
  • 55
vikrant
  • 393
  • 4
  • 15
  • It will keep running for a long time, vut it won't actually be doing anything... Unless you made it multithreaded? – JohannisK Apr 28 '15 at 21:32
  • @JohannisK in //some computation block this thread will register some callback handler with other services get triggered as and when needed. – vikrant Apr 28 '15 at 21:39
  • Maybe better: System.in.read() ... see https://stackoverflow.com/questions/6032118/make-the-console-wait-for-a-user-input-to-close – oo_dev Nov 04 '19 at 12:44

2 Answers2

5

You could use an infinite loop:

while(true){}

However, that would eat up the CPU for no reason. Instead, you could just call the wait() method:

synchronized{
   wait();
}

Then to resume, you'd call notify() from another thread.

More info here.

You could also just start another non-daemon Thread.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • I will for sure explore this approach. But is this better then what I am doing? – vikrant Apr 28 '15 at 21:41
  • 1
    @vikrant I'm not sure either one is better or worse. Go with whichever makes the most sense to you, because that will be the easiest to maintain. – Kevin Workman Apr 28 '15 at 21:43
-2

If all you want is for it to never reach the end why not

while (1)