Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips
What is TimeUnit inward Java
TimeUnit inward Java is a course of report on java.util.concurrent package, introduced inward Java five along alongside CountDownLatch, CyclicBarrier, Semaphore in addition to several other concurrent utilities. TimeUnit provides a human readable version of Thread.sleep() method which tin live on used inward house of former. From long fourth dimension Thread's sleep() method is measure means to suspension a Thread inward Java in addition to almost every Java programmer is familiar alongside that. In fact, slumber method itself is a really pop in addition to has appeared on many Java interviews. The deviation betwixt hold back in addition to sleep is i of the tough Java questions to answer. If y'all get got used Thread.sleep() earlier in addition to I am certain y'all do, y'all mightiness live on familiar alongside a fact similar it's a static method, it doesn't unloose the lock when pausing Thread in addition to it throws InterruptedException.
But what many of us doesn't reckon a potential resultant is readability. Thread.sleep() is an overloaded method in addition to convey long millisecond in addition to long nanosecond, which makes it difficult for programmers to discovery out just how many seconds, minutes, hours or twenty-four hours electrical flow Thread is sleeping. Look at below instance of Thread's sleep() method:
By simply having a cursory glance, tin y'all figure out how long electrical flow Thread volition wait ? Some of y'all may live on but its hardly readable for many Java programmer in addition to y'all take away to convert milliseconds farther into seconds in addition to thus into minutes. Let's convey a expect on some other instance of Thread.sleep() method which is slightly to a greater extent than readable than previous example.
Further Learning
Multithreading in addition to Parallel Computing inward Java
Java Concurrency inward Practice - The Book
Write a Java plan to create deadlock in addition to ready it – Interview QuestionWhat is race status inward Java – an example How to write thread condom code inward Java Difference betwixt Runnable in addition to Thread class How to utilization ThreadLocal variable inward Java – Beware of retention leak How to solve producer consumer work using BlockingQueue inward Java
TimeUnit inward Java is a course of report on java.util.concurrent package, introduced inward Java five along alongside CountDownLatch, CyclicBarrier, Semaphore in addition to several other concurrent utilities. TimeUnit provides a human readable version of Thread.sleep() method which tin live on used inward house of former. From long fourth dimension Thread's sleep() method is measure means to suspension a Thread inward Java in addition to almost every Java programmer is familiar alongside that. In fact, slumber method itself is a really pop in addition to has appeared on many Java interviews. The deviation betwixt hold back in addition to sleep is i of the tough Java questions to answer. If y'all get got used Thread.sleep() earlier in addition to I am certain y'all do, y'all mightiness live on familiar alongside a fact similar it's a static method, it doesn't unloose the lock when pausing Thread in addition to it throws InterruptedException.
But what many of us doesn't reckon a potential resultant is readability. Thread.sleep() is an overloaded method in addition to convey long millisecond in addition to long nanosecond, which makes it difficult for programmers to discovery out just how many seconds, minutes, hours or twenty-four hours electrical flow Thread is sleeping. Look at below instance of Thread's sleep() method:
Thread.sleep(2400000);
By simply having a cursory glance, tin y'all figure out how long electrical flow Thread volition wait ? Some of y'all may live on but its hardly readable for many Java programmer in addition to y'all take away to convert milliseconds farther into seconds in addition to thus into minutes. Let's convey a expect on some other instance of Thread.sleep() method which is slightly to a greater extent than readable than previous example.
Thread.sleep(4*60*1000);
This is much amend than previous i but however its non perfect in addition to until y'all are aware that slumber times are inward millisecond its non slow to approximate that electrical flow Thread volition hold back for 4 minutes. TimeUnit course of report resolves this resultant yesteryear providing indicator for DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS in addition to NANOSECONDS. java.util.concurrent.TimeUnit is perfect instance of powerful Java Enum. All TimeUnit are Enum instances. Let's run into how to slumber Thread for 4 minutes using TimeUnit :
TimeUnit.MINUTES.sleep(4); // sleeping for 4 minutes
Similarly y'all tin innovate pauses on seconds, minutes in addition to hour level. By looking this code y'all tin discovery out that, this is much to a greater extent than readable than Thread's slumber method. Remember that TimeUnit.sleep() internally calls Thread.sleep() for sleeping in addition to throws InterruptedException. You tin likewise banking concern stand upwards for JDK code to verify that. Here is a sample code instance which demonstrate how to utilization TimeUnit's sleep() method inward Java.
/**
*
* Java plan to demonstrate how to utilization TimeUnit.sleep() method inward Java.
*
* Java plan to demonstrate how to utilization TimeUnit.sleep() method inward Java.
* TimeUnit is a novel means of introducing suspension inward Java program.
* @author Javin
*/
public class TimeUnitTest {
public static void main(String args[]) throws InterruptedException {
System.out.println("Sleeping for 4 minutes using Thread.sleep()");
Thread.sleep(4 * 60 * 1000);
System.out.println("Sleeping for 4 minutes using TimeUnit sleep()");
TimeUnit.SECONDS.sleep(4);
TimeUnit.MINUTES.sleep(4);
TimeUnit.HOURS.sleep(1);
TimeUnit.DAYS.sleep(1);
}
}
* @author Javin
*/
public class TimeUnitTest {
public static void main(String args[]) throws InterruptedException {
System.out.println("Sleeping for 4 minutes using Thread.sleep()");
Thread.sleep(4 * 60 * 1000);
System.out.println("Sleeping for 4 minutes using TimeUnit sleep()");
TimeUnit.SECONDS.sleep(4);
TimeUnit.MINUTES.sleep(4);
TimeUnit.HOURS.sleep(1);
TimeUnit.DAYS.sleep(1);
}
}
Apart from sleep() functionality, TimeUnit likewise render convenient method to convert fourth dimension into dissimilar Unit. For instance if y'all desire to convert seconds into milliseconds than y'all tin utilization next code :
TimeUnit.SECONDS.toMillis(44)
It volition render 44,000. It's readable in addition to convenient means to convert fourth dimension into dissimilar unites e.g. micro seconds, Milli seconds etc.
TimeUnit vs Thread.sleep()
Java programmer knows close it in addition to yesteryear looking Thread.sleep() they come upwards to know that Thread is going to pause. This is non truthful alongside TimeUnit class, alongside 2 argue offset its non really pop at to the lowest degree compare to Thread.sleep() in addition to 2nd it's non inward Thread class, much similar wait in addition to notify which are likewise non inward Thread class.. Anyway none of these are big issues in addition to it volition convey some fourth dimension to live on adopted in addition to perish an measure way, given the size of Java community some the world.
In Summary prefer TimeUnit.sleep() method if y'all would similar to innovate curt suspension inward your plan in addition to other places where y'all intend of Thread.sleep(). It non entirely improves readability of code but likewise makes y'all familiar alongside java.util.concurrent package, which is primal API for concurrent programming inward Java.
Further Learning
Multithreading in addition to Parallel Computing inward Java
Java Concurrency inward Practice - The Book
Write a Java plan to create deadlock in addition to ready it – Interview Question
Komentar
Posting Komentar