I am translating some Javascript code into Java, and need to know how to do Timeouts in Java. How would I translate the following into Java?
var x = setTimeout(function(){ ... }, 2000);
clearTimeout(x);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Create a Launch a new
Thread. In case you need a delay in the execution, you can usesleep()Anyway it is basically different, in JS you do not have a ‘Main thread’ (JS is run by the browser threads) while in Java you have it. So, if what you want to do is to call a function separately at specified times, you create just one thread and in this thread you make a loop with sleep and your logic (and mark the thread as daemon).