This time, I will explain how to implement Java multithreading. Multithreading is used when you want to perform multiple processes in parallel.
Also, multithreading uses a class called Thread, I would like to introduce a method that is often used in the Thread class.
After that, it will be explained in the following versions and environments.
IDE:eclipse Java version: 8 OS:Mac OS
Then, I will explain it immediately.
The folder structure of the sample source created this time is as follows.
As for the contents to be implemented in this sample, we will implement a basic program that starts the main class "Main.java" and starts the subclasses "SubThreadA.java" and "SubThreadB.java" in parallel. ..
First, implement the subclass that you want to process in parallel this time.
To create a multithreaded program, inherit the Thread class and write the run method in that class. Describe as follows.
class subclass name extends Thread{
public void run() {
processing
}
}
In this sample program, let's implement a program that outputs the following words to the console in each thread.
SubThreadA.java → "Thread A has started." SubThreadB.java → "Thread B has started."
SubThreadA.java
package main;
public class SubThreadA extends Thread {
public void run() {
System.out.println("Thread A has started.");
}
}
SubThreadB.java
package main;
public class SubThreadB extends Thread {
public void run() {
System.out.println("Thread B has started.");
}
}
Next, implement the process to start the thread created earlier in the main class.
As for the implementation method, the process of the run method is started in multithread by calling the start method from the object of the subclass created earlier.
Describe as follows.
public static void main(String[] args) {
Subclass name mt=new subclass name();
mt.start();
}
Now, let's describe the process of calling "SubThreadA" and "SubThreadB".
main.java
package main;
public class Main {
public static void main(String[] args) {
//Creating a subclass object that inherits the Thread class
SubThreadA mt1 = new SubThreadA();
SubThreadB mt2 = new SubThreadB();
//Subclass execution
mt1.start();
mt2.start();
}
}
After completing the above description, right-click Main.java> Run Java Application to execute it. It is OK if the following words are output on the console screen.
Thread A has started.
Thread B has started.
Here, we will explain how to stop a thread for a certain period of time with the sleep method of the Thread class.
The sleep method is used by specifying the time you want to stop in the argument in milliseconds. (1000 milliseconds = 1 second)
Also, in the sleep method, an InterruptedException exception may occur due to an interrupt, etc., so it is necessary to enclose it in a try-catch statement and handle the exception.
Now, let's execute "Sub Thread A" with a delay of 3 seconds.
SubThreadA.java
package main;
public class SubThreadA extends Thread {
public void run() {
try {
//3 seconds sleep
Thread.sleep(3000);
System.out.println("Thread A has started.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Right-click on Main.java> Run Java Application to run it. It is OK if thread A is started 3 seconds after thread B and the words are output to the console screen in the following order.
Thread B has started.
Thread A has started.
Use the join method when you want to wait until the processing of another thread finishes. Code the thread object .join () that you want to monitor for termination.
Also, as with the sleep method, an InterruptedException exception may occur due to an interrupt, etc., so it is necessary to enclose it in a try-catch statement and handle the exception.
Now, let's try to process "SubThreadB" after the processing of "SubThreadA" is completed.
Main.java
package main;
public class Main {
public static void main(String[] args) {
try {
SubThreadA mt1 = new SubThreadA();
SubThreadB mt2 = new SubThreadB();
mt1.start();
mt1.join(); //Wait until SubThreadA finishes
mt2.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Right-click on Main.java> Run Java Application to run it. After a while, it is OK if threads A → B are executed in this order and the following words are output on the console screen.
Thread A has started.
Thread B has started.
I started my personal blog in 2020!
Based on the knowledge and experience gained as a freelance engineer, we plan to distribute content such as information about freelance engineers, IT technical information, industry information, and engineer life hacks.
The number of articles is still small, but it is updated weekly, so if you are interested, I would be grateful if you could take a look.
https://yacchi-engineer.com/
Recommended Posts