[Java] Java threading

2 minute read

[Java thread processing]

It is a memo of Java thread processing used in business. I have summarized it because it is used during development.

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 if there is only one cash register when it is crowded with people, such as lunch breaks. If the cash register functions at two or three locations, it can handle people in a short time and is efficient.

The program is the same, and if you run multiple processes at the same time, you can process efficiently in a short time. That mechanism is a thread. Threads are also commonly used in everyday life.

Thread processing

1, inherit the thread class and override the run() method.

Write the contents to be processed by the thread in run( ).

(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 the 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 a new thread is executed by start( ).

It is wise not to use stop( ), etc. which may lead to fatal failures.

How to use ###Thread.sleep

It can be used when you want to suspend the processing of a thread or wait for some processing in the operation of a Java program. sleep is generally paired 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.

Consider how 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 development period and system. → Difficult to test and identify problems. It may affect the entire development process. Because 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.

Using the same variable or method from multiple threads at the same time may result in data corruption.

// In order to avoid data destruction, perform exclusive control processing with synchronized.
//Only one thread can be executed.

synchronized (target instance) {
  // Code 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.