java - Timeout for function -


How can I do this in Java? To be more preses I would like to end the time for my function.

Pseudo-code:

  public string aLotOfWork () {hardwork (return)); If (hardwalk (still being executed after 30 seconds) return "timeout"; }   

I can use timer for this, but I can not return any value to the timer function run () from my upper function

  Timer task = new timer tasks () {public zero run () {return ""; // timer task run should be zero }}; Timer timer = new timer (); Timer Scheduler (work, 50000, 1);   

Check the class This is a code called "timecode" (timeout, time unit). Provides . Public string aLotOfWork () {Future Task Function = New Future Tasks & lt; String & gt; (New callable & lt) Load hardwork task to futuresksk class load

  ; String & gt; () {Public string call () {return hard ();}}); Try {Return task.get (30, time unit SECONDS); } Hold (timeout Exception e) {return "timeout"; } Hold (exception e) {return "timeout"; // or whatever you like}}    

Comments

Popular posts from this blog

python - Writing Greek in matplotlib labels, titles -

c# - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method -

Pygame memory leak with transform.flip -