Java threads as much as

A concurrent and parallel

  1, concurrent

    It refers to two or more events in the same time period occurred within.

  2, parallel

      Refers to two or more events at the same time occurs (simultaneously).

    

    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 time only one program execution, i.e. the program microscopically is a time-alternating runs, 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 that a plurality of program can be executed simultaneously.

   For multi-core 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, only multiple tasks on a single CPU run concurrently on. Similarly , the thread is the same, from a macro point of view to understand the thread is running in parallel, but the analysis from the microscopic point of view it is a serial operation, a thread that is a thread to run when the system is only one CPU , the thread execute multiple threads in a certain order, we call this situation is called  thread scheduling .

Second, threads and processes

  Process : refers to an application running in memory, each process has a separate memory space, an application can run multiple processes simultaneously; a program execution process also, is the basic unit of the system operating procedures ; system run a program that is created from a process, run to the demise of the process.

  Thread : A thread is a process execution unit , responsible for the implementation of the program in the current process, a process that at least one thread. A process can have multiple threads, the application can also be called multi-threaded programs.

  Summary : There is a process after at least one program is running, a process can contain multiple threads.

  View through the Task Manager process :

  

    Thread:

   Thread scheduling :

    •   Time-sharing scheduling

      All threads take turns using the right to use the CPU, the average time each thread CPU-allocation.

    •   Preemptive scheduling

      Giving priority to high-priority thread use the CPU , if the priority of the thread of the same, then will randomly select one ( thread randomness ) , the Java for the use of preemptive scheduling

      •  Priority thread

      

      •  Detailed preemptive scheduling

        Most operating systems support multiple concurrent processes running, and now almost all operating systems support running multiple programs simultaneously. For example, turn on the computer can run multiple software. . At this point, these programs are running at the same time, " feeling as if the software running at the same time ."

        In fact, the CPU ( central processing unit ) and preemptive scheduling mode for the high speed switching between multiple threads. For the CPU a nuclear terms, at some point, can only execute one thread, while the CPU is in multiple threads between the switching speed is relatively fast to our senses, it 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 CPU usage higher.

      

    Multithreading benefits : high efficiency ① ② does not affect among multiple threads

Third, the main thread

  The main thread thread execution method of primary (main):

  Single-threaded program : java program, only one thread, execution begins from the main method of execution from top to bottom

  

Third, create a thread

  1, create a way 

    Java uses java.lang.Thread class represents a thread, all the thread object must be an instance of the Thread class or subclass.

    The role of each thread is to complete a certain task is 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 by inheritance Thread class to create and launch a multi-threaded steps are as follows:

      ① 定义Thread类的子类,并重写该类的run()方法,该run()方法的方法体就代表了线程需要完成的任务,因此把
run()方法称为线程执行体。

      ② 创建Thread子类的实例,即创建了线程对象

      ③ 调用线程对象的 start() 方法来启动该线程

         void start() 使该线程开始执行;Java 虚拟机调用该线程的 run 方法。

         结果是两个线程并发地运行;当前线程(main线程)和另一个线程(创建的新线程,执行其 run 方法)。

         多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。

         java程序属于抢占式调度,哪个线程的优先级高,那个线程优先执行;同一个优先级,随机选择一个执行。

    Demo:

 1 // 自定义线程类
 2 public class MyThread extends Thread {
 3     //定义指定线程名称的构造方法
 4     public MyThread(String name) {
 5         //调用父类的String参数的构造方法,指定线程的名称
 6         super(name);
 7     }
 8 /**
 9 * 重写run方法,完成该线程执行的逻辑
10 */
11     @Override
12     public void run() {
13         for (int i = 0; i < 10; i++) {
14             System.out.println(getName()+":正在执行!"+i);
15         }
16     }
17 }
18 // 测试类
19 public class Demo01 {
20     public static void main(String[] args) {
21         //创建自定义线程对象
22         MyThread mt = new MyThread("新的线程!");
23         //开启新线程
24         mt.start();
25         //在主方法中执行for循环
26         for (int i = 0; i < 10; i++) {
27             System.out.println("main线程!"+i);
28         }
29     }
30 }
View Code

  2、创建方式二

四、Thread 和 Runnable 的区别

五、匿名内部类方式实现线程的创建

Guess you like

Origin www.cnblogs.com/niujifei/p/11454913.html