When update an entry of HashMap
Imagine there is an HashMap with entry properties, mapped from String to Object, this value only contains a refrence to the value field. Thus you can directly call the set methods of the object, and it will get reflected in the HashMap. The situation also applies to arrayList as value of this entry.
3 approaches to execute a function after certain timeinterval
using simple thread
This is very simple, which creates the simple thread puts it run in forever with use of while loop and makes use of sleep method to put the interval between running.
<% start codeblock %>
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!");
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
<% endcodeblock %>
using the Timer and TimerTask
This has much more benefits than previous they are as follows
- control over when start and cancel task
- first execution can be delayed if wanted, provides useful
<% start codeblock %>
import java.util.Timer;
import java.util.TimerTask;
public class Task2 {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
@Override
public void run() {
// task to run goes here
System.out.println(“Hello !!!”);
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay,
intevalPeriod);
} // end of main
}
<% endcodeblock %>
using ScheduledExecutorService
This is introduced in java.util.concurrent from Java SE 5 as Concurrency utilities. This is preferred way to achieve the goal.
It provides following benefits as compared to previous solutions
- pool of threads is used to execute as compared TImer’s single thread
- Provides the flexibility for delaying first execution
- Provides nice conventions for providing the time intervals
<% start codeblock %>
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Task3 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
}
<% endcodeblock %>
Corner trace of Java SE
Can use Integer.equals() to compare whether 2 Integer has equal values, but the first parameter where the Integer is called on shouldn’t be null. m1.equals(null) can be called to compare.
Using equals to compare 2 objects, if the equals methods has been overriden, these overriden methods will be called, otherwise just use == to compare whether 2 object reference to the same object.