Java thread processing

[Java thread processing]

This is a memo of Java thread processing used in business. I used it during development, so I summarized it.

What is a thread?

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.

Thread processing

1. Inherit thread class and override run () method.

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.)

2. At the place where you want to start the execution of another thread, instantiate the class ① and call the start () method.

(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

run () is created and start () runs a new thread.

It is wise not to use stop () etc. that can lead to fatal obstacles.

How to use Thread.sleep

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.

* Threads need to be afraid correctly.

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
}

Digression

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

Java thread processing
Java string processing
Notes on Android (java) thread processing
[Java] Multi-thread processing
[Java] Stream processing
java iterative processing
[Java] Thread and Runnable
Thread safe summary ~ Java ~
JAVA constructor call processing
Java random, various processing
Java Programming Thread Runnable
[Java] New Thread generation method (2)
[Java] Multi-thread processing --Exclusive control
Java
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Timer processing implementation method
Java thread safe for you
Measured parallel processing in Java
Understanding Java Concurrent Processing (Introduction)
Java
Java thread to understand loosely
Summary of java error processing
[Java] New Thread generation method (1)
Date processing in Java (LocalDate: Initialization)
Delegate some Java processing to JavaScript
[Java] Loop processing and multiplication table
Run node.js from android java (processing)
thread safe process in java language
[Processing × Java] How to use variables
What is thread safe (with Java)
Server processing with Java (Introduction part.1)
Surprisingly deep Java list inversion-Stream processing
Basic processing flow of java Stream
About file copy processing in Java
Deleting files using recursive processing [Java]
[Processing × Java] How to use arrays
[Java] Processing time measurement method memo
Java local variables are thread safe
[Java] Exception types and basic processing
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
Data processing using stream API from Java 8
java (override)
java (method)
Java Day 2018
Java string
[Processing x Java] Construction of development environment
Java static