In a hurry, from the workplace to make a multithreaded program in Java I will summarize what I learned in haste to answer unreasonable requests.
If the path to subthread startup is small & you do not want to keep the subthread as an instance, combine lambda expressions
ThreadStartTemplate.java
new Thread( () -> {Method or process you want to execute in a subthread} ).start()
Looks good. It's convenient.
I put off installing Eclipse on my own and used paiza.io to check the grammar for the time being.
It seems that threads can be implemented with the following two patterns.
It seems that Java has a Runnable interface, and by overriding run (), it seems that the processing executed at the time of thread can be implemented. The usage is as follows.
The sample code written according to this street is RunnableSample.java.
RunnableSample.java
import java.util.*;
public class Main {
private static class RunnableSample implements Runnable{
private String thread_no;
RunnableSample(String str){
this.thread_no = str;
}
public void run(){
System.out.println("RunnableThread Starting No."+thread_no);
}
}
public static void main(String[] args) throws Exception {
String str1 = "1";
String str2 = "2";
RunnableSample runner1 = new RunnableSample(str1); // 1.
RunnableSample runner2 = new RunnableSample(str2); // 1.
Thread thread1 = new Thread(runner1); // 2.
Thread thread2 = new Thread(runner2); // 2.
thread1.start(); // 3.
thread2.start(); // 3.
Thread.sleep(200);
System.out.println("MainThread End");
}
}
Execution result
RunnableThread Starting No.1
RunnableThread Starting No.2
MainThread End
The RunnableSample class is defined as a local class of the Main class. Create an object of RunnableSample class from the main thread and execute it as two sub threads.
RunnableThread Starting No.1 RunnableThread Starting No.2 May be output in reverse order. The Main Thread End will usually be output last because it has a 200ms sleep in between.
Next, I will write about how to implement the processing of subthreads by inheriting the Thread class. The usage is as follows. The code is ThreadSample.java.
ThreadSample.java
import java.util.*;
public class Main {
private static class ThreadSample extends Thread{
private String thread_no;
ThreadSample(String str){
this.thread_no = str;
}
public void run(){
System.out.println("RunnableThread Starting No."+thread_no);
}
}
public static void main(String[] args) throws Exception {
String str1 = "1";
String str2 = "2";
ThreadSample thread1 = new ThreadSample(str1); // 1.
ThreadSample thread2 = new ThreadSample(str2); // 1.
thread1.start(); // 2.
thread2.start(); // 2.
Thread.sleep(200);
System.out.println("MainThread End");
}
}
Execution result
RunnableThread Starting No.2
RunnableThread Starting No.1
MainThread End
The execution result will be the same (this time, the output order of Thread1 and Thread2 was reversed).
Basically, it seems that there are many advantages to using the Runnable interface.
--Java does not allow multiple inheritance, so if you inherit the Thread class, other classes will not be able to inherit it. Any class that implements the Runnable interface can inherit from other classes. --You don't have to inherit the overhead of the Thread class. --The code seems to be cleaner if you inherit the Thread class.
It would be perfect if I could write it neatly even with the Runnable interface. I will try it.
Try to simplify the code with lambda expressions by referring to Understanding Java 8 lambda expressions.
LambdaThreadSample.java
import java.util.*;
public class Main {
static private void subthread_run(String thread_no){
System.out.println("RunnableThread Starting No."+thread_no);
}
public static void main(String[] args) throws Exception {
String str1 = "1";
String str2 = "2";
new Thread( () -> {subthread_run(str1);} ).start();
new Thread( () -> {subthread_run(str2);} ).start();
Thread.sleep(200);
System.out.println("MainThread End");
}
}
Execution result
RunnableThread Starting No.2
RunnableThread Starting No.1
MainThread End
For the detailed description method, refer to Understanding Java 8 Lambda Expressions, but ~~ difficult to explain ~~ The RunnableSample class is an anonymous class with a subthread_run method, and the Thread class object is new and started. By using the lambda expression, the program became much cleaner and the readability was greatly improved. This is a good way to write if you don't need to manage Thread as an instance. Next, I would like to summarize Synchronized.
150 questions to train programming skills to fight in the world Chapter.16: Threads and locks
Recommended Posts