Concurrent Programming - Thread Basics

Basic concept:

  1. process and thread

A process is the basic unit for resource allocation by the operating system, and a process is the basic unit for CPU scheduling.
Threads under the same process share some resources in the process, and threads also have their own independent storage space.

  1. Multithreading

Multi-threading refers to the existence of multiple threads in a single process at the same time
. The existence of multiple threads can be compared to the existence of multiple employees in a store at the same time, each responsible for different responsibilities. Make the service faster and smoother
, but the introduction of multi-threading, multiple threads manage the same resource at the same time, will bring consistency problems. This is very similar to the problems that arise when a monolithic architecture transitions to a distributed architecture.

  1. serial, parallel, concurrent

Serial: The same CPU executes code sequentially. Queue execution
Parallel: Multi-core CPU, execute multiple program fragments at the same time
Concurrency: Single-core CPU and fast switching between multiple programs (not the same as high concurrency)

  1. Synchronous and asynchronous, blocking and non-blocking

Synchronous and asynchronous, whether the callee will actively feedback information after executing the program Blocking and non-blocking, whether the caller needs to wait for the result feedback
after executing a certain function

How to create threads

  1. Inherit the Thread class and rewrite the run method
  2. Implement the Runnabble interface and re-run the method 【可以用匿名内部类,或lambda方式】, because the construction of new Thread can receive a Runnable instance,
  3. Implement the Callable interface, rewrite the call method, and cooperate with FutureTask 【获取返回值】, because the construction of new Thread can receive a FutureTask instance, and FutureTask (Future Task) requires a Callable instance.
    All creation methods ultimately need to implement a Runnable interface.

state of the thread

  1. Five states at the operating system level: new-ready-running-terminated, in the running process, the thread can be in the waiting state through method calls
  2. There are 6 states of threads in java: new- runnable(reday+running)- TerminatedIn the runnable state, you can call wait() to let the thread enter waitingthe state, call sleep() or join() to let the program enter the state, and enter the state timed_waitingwhen synchronized does not get the lockblocked

When a thread is new and does not call the start() method, the thread is in the newstate
. When a thread is running, the thread is in the runnablestate
. When a thread does not get the lock, the thread is in the state . When the thread actively calls the wait method, the thread enters the state. When the thread calls the sleep() or join() method, sleep must have parameters .blocked
waiting
timed_waiting
terminated

thread api

method
static Thread currentThread() Gets the current thread
String getName() Get the name of the current thread for identification and troubleshooting.
static void yield() changes the current thread from the running state to the ready state.
static void sleep(long millis) let the thread sleep
static void sleep(long millis) let the thread sleep
void join() Let the thread execute for a period of time or until the end, depending on whether it has parameters
void setDaemon(boolean on) marks the thread as a daemon thread, and will ensure that all non-daemon threads end before the JVM launches
void notify(), notifyAll() need to use lock object to wake up

There are waiting pools and lock pools in the thread. The threads in the waiting pool are waiting to wake up, or wake up by themselves. The threads in the lock pool are waiting for the lock to be released, and then grab the lock to execute the program.

  • thread end

InterrputThe method
isInterrupted() checks the interrupt flag, and the default interrupt is false.
interrupt() interrupts the thread status
interrupted() returns the interrupt status and resets it to false

Interrupt waitingand timed_waitingstate control the state of the thread and execute different code blocks.

Guess you like

Origin blog.csdn.net/u013795102/article/details/131830809