Java Concurrency and multithreading (1): Multithreading

A concurrent and parallel

1. Concurrent
refers to two or more events at the same time occurs within.
2. Parallel
refers to two or more events occur at a time (carried out together) .
Here Insert Picture Description
In the operating system, the installation of a plurality of programs concurrently refers to the period of time a plurality of programs are running on the macro , in which single-CPU system, each of
a time only one program execution, i.e. the program microscopically time-sharing is alternately run , just gives the impression that run at the same time, it is because the time-interleaved time is running very short.

In multiple CPU system, these programs can execute concurrently can be assigned to a plurality of processors (CPU) , multi-parallel execution of tasks,
i.e., with each processor to process a program can execute concurrently, so multiple programs can be executed simultaneously. He said the current computer market multicore
CPU, multi-core processor is, the more cores, more parallel processing program, can greatly improve the efficiency of a computer operation.

Note: The computer must be single-core processor can not handle multiple tasks in parallel, multiple tasks can only be run concurrently on a single CPU. The same
reason, the thread is the same
. Understood from a macro perspective threads run in parallel, but from the microscopic point of analysis is to run serially, i.e., a
thread is a thread to run, when a system is only the CPU, a plurality of threads in a thread executes a certain order we call this situation is called
thread scheduling.
Here Insert Picture Description

Second, threads and processes

1.进程
是指一个内存中运行的应用程序(加载到内存中滴),每个进程都有一个独立的内存空间,一个应用程序可以同时运行多个进程;进程也是程序的一次执行过程,是系统运行程序的基本单位;系统运行一个程序即是一个进程从创
建、运行到消亡的过程。

Here Insert Picture Description
2.线程
线程是进程中的一个执行单元,负责当前进程中程序的执行(路),一个进程中至少有一个线程。一个进程
中是可以有多个线程的,这个应用程序也可以称之为多线程程序。

Here Insert Picture Description

简而言之:一个程序运行后至少有一个进程,一个进程中可以包含多个线程

线程调度

分时调度
所有线程轮流使用 CPU 的使用权,平均分配每个线程占用 CPU 的时间。

抢占式调度
优先让优先级高的线程使用 CPU,如果线程的优先级相同,那么会随机选择一个(线程随机性),Java使用的为
抢占式调度。

设置线程的优先级
Here Insert Picture Description
抢占式调度详解
大部分操作系统都支持多进程并发运行,现在的操作系统几乎都支持同时运行多个程序。比如:现在我
们上课一边使用编辑器,一边使用录屏软件,同时还开着画图板,dos窗口等软件。

此时,这些程序是在同时运行,”感觉这些软件好像在同一时刻运行着“。

** In fact, CPU (central processing unit) preemptive scheduling mode using the rapid switching between multiple threads. ** For a core CPU and
words, at some point, you can only execute one thread, and the CPU speed switching between multiple threads we feel relatively faster, looked like
at the same time run. In fact, multi-threaded program does not improve the operating speed of the program, but the program can improve operational efficiency, so that the CPU
usage higher.

Here Insert Picture Description

Third, create a thread class

Java uses java.lang.Thread class represents a thread, all the thread object must be a Thread class or subclass instance. The role of each thread is to
complete certain tasks, actually executing code stream of a program that is a period of the order of execution . Java uses thread of execution to represent this program flow.
Java step in and start to create multi-threaded through inheritance Thread class is as follows:

  1. Subclass definition Thread class and override the class run () method , the method body run () method represents the thread needs to complete the task , so the
    run () method is called thread of execution.
  2. Create a Thread subclass instance, the thread object is created
  3. Call the thread object's start () method to start the thread

Here Insert Picture Description

code show as below:

/**
 * 自定义一个线程类extends Thread
 * @author Mango
 */
public class MainThread extends Thread{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName() + ":正在执行!"+ i);
        }
    }

}
/**
 * 测试类
 * @author Mango
 */
public class Dome1 {

    public static void main(String[] args) {
        MainThread mainThread = new MainThread();

        mainThread.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("main线程" + i);
        }
    }
}
Published 47 original articles · won praise 18 · views 4862

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/101673451