Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips

Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel best practices, Artikel core java, Artikel Java multithreading Tutorials, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips
link : Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips

Baca juga


Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips

What is TimeUnit inwards Java
TimeUnit
inwards Java is a flat on java.util.concurrent package, introduced inwards Java five along amongst CountDownLatch, CyclicBarrier, Semaphore as well as several other concurrent utilities. TimeUnit provides a human readable version of Thread.sleep() method which tin sack last used inwards house of former. From long fourth dimension Thread's sleep() method is criterion agency to time out a Thread inwards Java as well as almost every Java programmer is familiar amongst that. In fact, slumber method itself is a really pop as well as has appeared on many Java interviews. The divergence betwixt hold off as well as sleep is i of the tough Java questions to answer. If y'all cause got used Thread.sleep() earlier as well as I am certain y'all do, y'all powerfulness last familiar amongst a fact similar it's a static method, it doesn't free the lock when pausing Thread as well as it throws InterruptedException

But what many of us doesn't regard a potential termination is readability. Thread.sleep() is an overloaded method as well as bring long millisecond as well as long nanosecond, which makes it difficult for programmers to detect out precisely how many seconds, minutes, hours or twenty-four hours electrical flow Thread is sleeping. Look at below illustration of Thread's sleep() method:

Thread.sleep(2400000);


By merely having a cursory glance, tin sack y'all figure out how long electrical flow Thread volition wait ? Some of y'all may last but its hardly readable for many Java programmer as well as y'all require to convert milliseconds farther into seconds as well as thus into minutes. Let's bring a hold off on some other illustration of Thread.sleep() method which is slightly to a greater extent than readable than previous example.

Thread.sleep(4*60*1000);

This is much improve than previous i but nevertheless its non perfect as well as until y'all are aware that slumber times are inwards millisecond its non tardily to approximate that electrical flow Thread volition hold off for 4 minutes. TimeUnit flat resolves this termination yesteryear providing indicator for DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS as well as NANOSECONDS. java.util.concurrent.TimeUnit is perfect illustration of powerful Java Enum. All TimeUnit are Enum instances. Let's meet how to slumber Thread for 4 minutes using TimeUnit :

TimeUnit.MINUTES.sleep(4);  // sleeping for 4 minutes

Similarly y'all tin sack innovate pauses on seconds, minutes as well as hour level. By looking this code y'all tin sack detect 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 as well as throws InterruptedException. You tin sack likewise depository fiscal establishment jibe JDK code to verify that. Here is a sample code illustration which demonstrate how to job TimeUnit's sleep() method inwards Java.

/**
 *
 * Java plan to demonstrate how to job TimeUnit.sleep() method inwards Java.
 * TimeUnit is a novel agency of introducing time out inwards 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);
    }
}


Apart from sleep() functionality, TimeUnit likewise supply convenient method to convert fourth dimension into dissimilar Unit. For illustration if y'all desire to convert seconds into milliseconds than y'all tin sack job next code :

TimeUnit.SECONDS.toMillis(44)

It volition render 44,000. It's readable as well as convenient agency to convert fourth dimension into dissimilar unites e.g. micro seconds, Milli seconds etc.



TimeUnit vs Thread.sleep()

Java programmer knows close it as well as yesteryear looking Thread.sleep() they come upward to know that Thread is going to pause. This is non truthful amongst TimeUnit class, amongst ii argue start its non really pop at to the lowest degree compare to Thread.sleep() as well as 2nd it's non inwards Thread class, much similar wait as well as notify which are likewise non inwards Thread class.. Anyway none of these are big issues as well as it volition bring some fourth dimension to last adopted as well as choke an criterion way, given the size of Java community or thus the world.


In Summary prefer TimeUnit.sleep() method if y'all would similar to innovate brusk time out inwards your plan as well as other places where y'all mean value of Thread.sleep(). It non exclusively improves readability of code but likewise makes y'all familiar amongst java.util.concurrent package, which is telephone substitution API for concurrent programming inwards Java.

Further Learning
Multithreading as well as Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
Write a Java plan to create deadlock as well as create it – Interview Question
  • What is race status inwards Java – an example
  • How to write thread prophylactic code inwards Java
  • Difference betwixt Runnable as well as Thread class
  • How to job ThreadLocal variable inwards Java – Beware of retentivity leak
  • How to solve producer consumer job using BlockingQueue inwards Java


  • Demikianlah Artikel Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips

    Sekianlah artikel Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

    Anda sekarang membaca artikel Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips dengan alamat link https://bestlearningjava.blogspot.com/2019/04/prefer-timeunit-slumber-over.html

    Belum ada Komentar untuk "Prefer Timeunit Slumber Over Thread.Sleep - Coffee Coding Tips"

    Posting Komentar

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel