Java multi-threaded acquaintance with the simple security problems solved

Multithreading

table of Contents

1. Process

1.1 Understanding the process
  • It is a simple process of running the program, referred to as a process.
  • Single-core CPU at any time on a node can only run one thread.
  • On windows10 shortcut key ctrl + shift + esc key combination to bring up the Task Manager
    Here Insert Picture Description
    can detect processes currently in use, with the back mount process
    win10 by entering wmic in cmd to open a command window
    and enter the cpu get NumberOfCores check their computer Audit
    simple to understand a core thread
    Here Insert Picture Description
    on Figure 4 represents my 4-core computer audit can execute four instructions simultaneously process

Note : mononuclear macroscopic parallel, serial micro

2. Thread

2.1. What is the thread
  • Lightweight process
  • A sequence program in the control flow, but also the basic scheduling unit CPU
  • But also the basic scheduling unit CPU, the process by a plurality of threads, do different things with each other, alternately executed, called multi-threading.
  • JVM virtual machine is a process, by default contains the main thread (Main function), you can create multiple threads execute concurrently with the main through code
Composition 2.2. Thread

Any thread has basic components:

  • CPU time slice: The operating system (OS) allocates execution time for each thread.
  • Operating data:
    1. heap space: object storage threads to be used. Multiple threads can share memory heap
    2. stack space: local variables to store a thread needs to use, each thread has a separate stack
2.3. Create a thread
  • Embodiment 1:
    1. Thread class inheritance
    2. Method override run
    3. subclass object start () call
public class TestThread{
	public static void main(String[] args){
		// 3.创建线程
		Thread th = new ThreadOne();
		// 4.调用线程
		th.start();
	}
}
// 线程类 1.继承Thread类
class ThreadOne extends Thread{
	// 2.重写run方法
	@Override
	public void run(){
		// 这里是线程要执行的内容
	}
}
  • Embodiment 2:
    1. implement Runnable
    2. override run method
    3. new interface to implement a class object
    4. Create a thread object
    5. The start () call
public class TestThread{
	public static void main(String[] args){
		// 3.创建任务
		ThreadOne one = new ThreadOne(); 
		// 4.创建线程
		Thread th = new Thread(one);
		// 5.调用线程
		th.start();
	}
}
// 线程类 1.继承Thread类
class ThreadOne implements Runnable{
	// 2.实现run方法
	@Override
	public void run(){
		// 这里是线程要执行的内容
	}
}
  • Otherwise do not do a detailed explanation listed here
2.4. The state of the thread
  • The initial state of New
  • Ready Ready
  • Running status Running
  • Termination status Terminated
  • Blocked Blocked
  • Timed Waiting waiting period
  • Wait indefinitely Waiting
    the JDK 1.5 after the ready state and run state collectively Runnable

Here Insert Picture Description

2.5. A common method thread
  • Dormancy

    • public static void sleep(long millis)
    • The current active thread to sleep Millis ms
  • give up

    • public static void yield()
    • Current thread time slice initiative to give up and return to the ready state, a time slice competitive.
  • Combined:

    • public final void join()
    • Allow other threads to be added to the current thread

join an indefinite wait: wait condition for the thread after calling the join method is completed and then enter the ready state, competition time slice!

Here Insert Picture Description

Look for more ways on their own java 1.8 api

3. Thread safety issues

  • When the multi-threaded concurrent access to critical resources, if broken ring atomic operation, may result in inconsistent data
  • Critical Resources: Shared Resource (same object), allow only one thread to use, is available to ensure its correctness
  • Atomic operations: an integral multiple steps, is considered as a whole, and the order can not be disrupted or default step

For example: access to bank money case.
Here are two solutions

  • A method, synchronized () {} synchronized block
  • Second method, coupled with the synchronized keyword on the method, the method statement said thread-safe
Published 29 original articles · won praise 33 · views 5103

Guess you like

Origin blog.csdn.net/lxn1214/article/details/104802428