Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Я имел в виду именно захват текущего стектрейса при создании задания (метод clientTrace). Он будет выполняться всегда, вне зависимости от того, будет исключение или не будет.
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));
final Date date = sdf.parse("2015-03-29 01:59:59.999 CET");
System.out.println(sdf.format(date) + " : " + date.getTime());
final Date date2 = new Date(date.getTime() + 1L);
System.out.println(sdf.format(date2) + " : " + date2.getTime());
2015-03-29 01:59:59.999 CET : 1427590799999
2015-03-29 03:00:00.000 CEST : 1427590800000
/**
* Returns the current time in milliseconds. Note that
* while the unit of time of the return value is a millisecond,
* the granularity of the value depends on the underlying
* operating system and may be larger. For example, many
* operating systems measure time in units of tens of
* milliseconds.
*
* <p> See the description of the class <code>Date</code> for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
public static native long currentTimeMillis();
Обратите внимание, что блокирующий ввод/вывод не пробрасывает InterruptedException (что прискорбно)
emails.parallelStream().forEach(this::sendEmail);
В Java 8 был представлен более мощный класс CompletableFuture. Пожалуйста, используйте его там, где это возможно.
final Future<Integer> division = executorService.submit(() -> 1 / 0); //ниже будет выброшено ExecutionException, вызванное ArithmeticException division.get();
Примечательно, что даже в Spring framework допустили эту ошибку в Async, см.: SPR-8995 и SPR-12090.
public static void main(String[] args) {
try {
System.out.println(
Executors.newSingleThreadExecutor()
.submit(() -> 1.0d / 0).get()
);
} catch (Throwable tr) {
System.out.println(tr.getLocalizedMessage());
}
}
10 советов по использованию ExecutorService