[Java] How to use Thread.sleep to pause the program

Programming study diary

December 23, 2020 I was developing Android in Android Studio, and I used Thread.sleep when I wrote a program that plays music for a certain period of time and then pauses, so I will briefly summarize how to use it.

What is a thread?

A thread is one of the execution units of processing, and refers to a series of flows from the beginning to the end of a program. There are single thread and multithread.

Single thread is also called serial processing. From the beginning to the end of the program is processed by one. Multithreading is also called concurrency. Perform two or more processes in parallel. There are four steps required to take advantage of multithreading.

  1. Create a class that inherits the Thread class
  2. Execute the run method in a class that inherits the Thread class
  3. Create an instance of a class that inherits the Thread class
  4. Execute the start method on the created instance

What is the sleep method?

The sleep method is used to pause the program. Often used in multithreading. The merit of using the sleep method is that if an infinite loop is executed with multiple threads, the load on the CPU will increase and the operation of the personal computer will become heavy. In such a case, you can use the sleep method during multithreaded processing to pause the processing to reduce the CPU load.

How to use Thread.slepp

There are only two points to use the sleep method!

--The argument of the sleep method uses a long type, and the unit is milliseconds (1 second = 1000 milliseconds). --Always add exception handling when using the sleep method

public class Sample {  
     public static void main(String[] args) {
        try {
            //Pause for 5 seconds
            Thread.sleep(5000);
        } catch(InterruptedException e){
            e.printStackTrace();
        }      
    }    
}

Execute sleep method in multithread

public class ThreadSample {
 
    public static void main(String[] args) {
        MultiThread1 mt1 = new MultiThread1();
        MultiThread2 mt2 = new MultiThread2();
        mt1.start();
        mt2.start();
    }
}
 
class MultiThread1 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
 
class MultiThread2 extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

References

Pause the program in Java, explain how to use Thread.sleep and how it works [Introduction to Java] How to stop Thread for a certain period of time with the sleep method

Recommended Posts

[Java] How to use Thread.sleep to pause the program
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
How to use the replace () method (Java Silver)
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
How to use the link_to method
How to use Java HttpClient (Get)
How to use the include? method
How to use the form_with method
How to use Java HttpClient (Post)
[Java] How to use join method
How to use the wrapper class
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
How to use submit method (Java Silver)
[Rails] How to use the map method
[Easy-to-understand explanation! ] How to use Java instance
[Introduction to Java] How to write a Java program
[Java] How to measure program execution time
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to use Java Scanner class (Note)
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
To use the "java" command line tool ... How to avoid popping up
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
How to use the camera module OV7725 (ESP32-WROVER-B)
[Java] How to use FileReader class and BufferedReader class