This is a memo of Java thread processing used in business. I used it during development, so I summarized it.
A thread is a mechanism for running multiple processes on a program at the same time.
Java has an API that executes multiple processes at the same time. → Use thread class (thread creation)
・ Consider the cash register at a convenience store as an example. It is inconvenient because it takes time if there is only one cash register when it is crowded with people such as lunch breaks. If the cash register is functioning at two or three places, it will be efficient to handle people in a short time.
If the program is the same and multiple processes are run at the same time, the process can be performed efficiently in a short time. The mechanism is threads. Threads are often used in everyday life.
In run (), write the content you want to process in the thread.
(Overriding is to inherit the parent class and overwrite the members of the parent class on the child class side when declaring the child class.)
(A method is a name that combines multiple statements into a single process. The smallest unit of a part)
//In the class that inherits Thread, write the process you want to execute in Thread in the run method.
class Sample extends Thread {
public void run() {
System.out.println("thread");
}
}
//Create an instance of the class that inherits Thread, call the start method, and run the thread.
class SampleSample {
public static void main(String[] args) {
ThreadSample t = new ThreadSample();
t.start();
}
}
//Execution result:thread
It is wise not to use stop () etc. that can lead to fatal obstacles.
It can be used when you want to pause the processing of a thread or wait for some processing in the operation of a Java program. sleep is generally set with try-catch. However, it is quite normal for catch to be empty, as it usually does nothing if an exception occurs during sleep.
Think of a way to avoid using threads as much as possible. For multithreaded programs, the entire program needs to be considered from the design stage. If you have to use threads, be prepared to take great risks.
Allow sufficient margin for the development period and system. → Difficult to test and identify problems. It may affect the entire development process. Since it is difficult to execute tests at the same time, it may not be possible to perform rigorous tests and it may go into production.
Even if an error occurs, it is not reproducible and it is difficult to identify the cause and fix the problem.
If the same variable or method is used from multiple threads at the same time, there is a risk of data corruption.
//Exclusive control processing is performed by synchronized in order to avoid data corruption.
//Only one thread can be executed.
synchronized(Target instance){
//Code that you want to protect from running concurrently from multiple threads
}
It's hard to remember because new terms come up one after another in my work, but I'm enjoying my work so I think I can continue to do my best in the future.
Recommended Posts