{"id":563,"date":"2004-02-02T09:33:11","date_gmt":"2004-02-02T13:33:11","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=563"},"modified":"2004-02-02T09:33:11","modified_gmt":"2004-02-02T13:33:11","slug":"asynchronous-method-invocation-in-java","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/02\/02\/asynchronous-method-invocation-in-java\/","title":{"rendered":"Asynchronous Method Invocation in Java"},"content":{"rendered":"<p>I&#8217;m working on a web application that needs to kick off a process that could take anywhere from 5 seconds to an hour, so I need to be able to call a method and have it return immediately.  A google search for &#8216;<a href=\"http:\/\/www.google.com\/search?q=java+asynchronous+method&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;start=40&amp;sa=N\">java asynchronous method<\/a>&#8216; didn&#8217;t return much, except <a href=\"http:\/\/www7.scu.edu.au\/programme\/docpapers\/1890\/com1890.htm\">this<\/a>, which seems to indicate that simply spawning a new thread within a method will do the job.  Basically you end up with something like this:<br \/>\n<code><br \/>\npublic class LongProcess implements Runnable {<br \/>\n&nbsp;&nbsp;public void startProcess() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;Thread t = new Thread(this);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;t.start();<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;public void run() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ perform the long running process<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nand then you can kick the process off like this:<br \/>\n<code><br \/>\nLongProcess p = new LongProcess();<br \/>\np.startProcess();<br \/>\n<\/code><br \/>\nThe other solution I thought of would be to use an <a href=\"http:\/\/ws.apache.org\/xmlrpc\/client.html\">asychronous xml-rpc call<\/a>, although this obviously has more overhead.<\/p>\n<p>Are there any solutions I&#8217;m missing? What would you do?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on a web application that needs to kick off a process that could take anywhere from 5 seconds to an hour, so I need to be able to call a method and have it return immediately. A google search for &#8216;java asynchronous method&#8216; didn&#8217;t return much, except this, which seems to indicate that &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/02\/02\/asynchronous-method-invocation-in-java\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Asynchronous Method Invocation in Java<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/563"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=563"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/563\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}