Found 4336 Articles for Java 8

Compute elapsed time in seconds in Java

Ankith Reddy
Updated on 26-Jun-2020 09:29:32

11K+ Views

To compute the elapsed time of an operation in seconds in Java, we use the System.currentTimeMillis() method. The java.lang.System.currentTimeMillis() returns the current time in milliseconds.Declaration −The java.lang.System.currentTimeMillis() is declared as follows −public static long currentTimeMillis()The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).Let us see a program to compute the elapsed time of an operation in Java −Example Live Demopublic class Example {    public static void main(String[] args) throws Exception {       // finding the time before the operation is executed       long start = ... Read More

Compare two int arrays in a single line in Java

Arjun Thakur
Updated on 26-Jun-2020 09:30:01

323 Views

Two int arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two int arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new int[] { 45, 12, 90 }, new int[] { 45, 12, 90 });       System.out.println("The two int arrays are equal? ... Read More

Call the run() method of the Timer Task in Java

Chandu yadav
Updated on 26-Jun-2020 09:31:30

643 Views

The java.util.TimerTask.run() method looks onto the action to be performed by the task. It is used to carry out the action performed by the task.Declaration −The java.util.TimerTask.run() method is declared as follows −public abstract void run()Let us see an example program to call the run() method of the Timer Task −Example Live Demoimport java.util.*; class MyTask extends TimerTask {    public void run() {       System.out.println("Running");    } } public class Example {    public static void main(String[] args) {       // creating timer task, timer       TimerTask task = new MyTask();       ... Read More

Cancel the Timer Task in Java

George John
Updated on 26-Jun-2020 09:32:11

5K+ Views

In order to cancel the Timer Task in Java, we use the java.util.TimerTask.cancel() method. The cancel() method returns a boolean value, either true or false. The cancel() method is used to cancel the timer task.Declaration −The java.util.TimerTask.cancel() method is declared as follows −public boolean cancel()The cancel() methods returns true when the task is scheduled for one-time execution and has not executed until now and returns false when the task was scheduled for one-time execution and has been executed already.Let us see a program to illustrate the use of the java.util.TimerTask.cancel() method −Example Live Demoimport java.util.*; class MyTask extends TimerTask {   ... Read More

Schedule a task for repeated fixed delay execution in Java

Ankith Reddy
Updated on 26-Jun-2020 09:34:27

873 Views

In fixed-delay execution, each execution is scheduled with respect to the original execution time of the preceding execution. If an execution is delayed for a particular reason (case in point, garbage collection), the subsequent executions will be delayed as well.There are two ways in which a task can be scheduled for repeated fixed-delay execution. They are as follows −Scheduling a task for repeated fixed-delay execution at a specified timeScheduling a task for repeated fixed-delay execution after a specified delayScheduling a task for repeated fixed-delay execution at a specified timeThe void schedule(TimerTask task, Date firstTime, long period) method schedules tasks for ... Read More

How to schedule tasks in Java to run for repeated fixed-delay execution, beginning at the specified time

Arjun Thakur
Updated on 26-Jun-2020 09:35:29

454 Views

One of the methods in the Timer class is the void schedule(TimerTask task, Date firstTime, long period) method. This method schedules tasks for repeated fixed-delay execution, beginning at the specified time.In fixed-delay execution, each execution is scheduled with respect to the original execution time of the preceding execution. If an execution is delayed for a particular reason (case in point, garbage collection), the subsequent executions will be delayed as well.Declaration −The java.util.Timer.schedule(TimerTask task, Date firstTime, long period) is declared as follows −public void schedule(TimerTask task, Date firstTime, long period)Here, task is the task to be scheduled, firstTime is the first ... Read More

Schedule a task for execution in Java after a given delay

Chandu yadav
Updated on 26-Jun-2020 09:37:39

1K+ Views

One of the methods in the Timer class is the void schedule(Timertask task, long delay) method. This method schedules the specified task for execution after the specified delay.Declaration −The java.util.Timer.schedule(Timertask task, long delay) is declared as follows −public void schedule(Timertask task, long delay)There are few exceptions thrown by the schedule(Timertask task, long delay) method. They are as follows −IllegalArgumentExceptionThis exception is thrown if delay is negative, or delay + System.currentTimeMillis() is negative.IllegalStateExceptionThis exception is thrown if task was scheduled or cancelled beforehand, timer was cancelled, or timer thread terminated.NullPointerExceptionThis exception is thrown if the task is null.Let us see a ... Read More

Schedule a task for execution in Java

Chandu yadav
Updated on 26-Jun-2020 09:43:24

5K+ Views

One of the methods in the Timer class is the void schedule(Timertask task, Date time) method. This method schedules the specified task for execution at the specified time. If the time is in the past, it schedules the task for immediate execution.Declaration −The java.util.Timer.schedule(Timertask task, Date time) is declared as follows −public void schedule(Timertask task, Date time)There are few exceptions thrown by the schedule(Timertask task, Date time) method. They are as follows −IllegalArgumentExceptionThis exception is thrown if time.getTime() is negativeIllegalStateExceptionThis exception is thrown if task was scheduled or cancelled beforehand, timer was cancelled, or timer thread terminated.NullPointerExceptionThis exception is thrown ... Read More

Remove all cancelled tasks from the timer's task queue in Java

George John
Updated on 26-Jun-2020 09:46:03

267 Views

One of the methods of the Timer class is the int purge() method. The purge() method removes all the canceled tasks from the timer’s task queue. Invoking this method does not affect the behavior of the timer, rather it eliminates references to the canceled tasks from the queue. The purge() method came into existence since JDK 1.5.The purge() method acts as a medium for space-time tradeoff where it trades time for space. More specifically, the time complexity of the method is proportional to n + c log n, where n is the number of tasks in the queue and c ... Read More

Terminate the timer in Java

Ankith Reddy
Updated on 26-Jun-2020 09:47:44

1K+ Views

One of the methods of the Timer class is the cancel() method. It is used to terminate the current timer and get rid of any presently scheduled tasks.The java.util.Timer.cancel() method is declared as follows −public void cancel()Let us see a program which uses the cancel() methodExample Live Demoimport java.util.*; public class Example {    Timer t;    public Example(int seconds) {       t = new Timer();       t .schedule(new Running(), seconds);    }    class Running extends TimerTask {       public void run() {          System.out.println("Task is cancelled");         ... Read More

Advertisements