I. Processes and Threads
1. What are programs, processes, and threads
A program is an ordered set of instructions and data, which itself has no meaning of running, and is a static concept. Almost all operating systems support running multiple tasks at the same time, and a task is usually a program, and each running program is a process. When a program runs, the internal may contain multiple sequential execution flows, and each sequential execution flow is a thread.
A program has only one process, but a process can have multiple threads.
2. There are 3 characteristics of the process:
A. Independence. Each process has its own independent memory space, and without the process itself, a user process cannot directly access the address space of another process.
B. Dynamic nature. The difference between a process and a program is that the program is just a static set of instructions, and the process is a collection of instructions that are being active in the system, adding the concept of time to the process, which has its own life cycle and various different states.
C. Concurrency. Multiple processes can execute concurrently on a single processor, with no interaction between multiple processes.
concurrency (concurrency) and parallelism (parallel) are two concepts in which parallel means that at the same time, multiple instructions are executed concurrently on multiple processors, and that only one instruction executes at the same time, but multiple process instructions are executed quickly. Makes it possible for the macro to have multiple simultaneous execution of the process effect.
3. Process 3 states
Ready state, running state, and blocking status
4. Threads
A thread is an execution path to a process, sharing a memory space, free switching between threads, concurrent execution, and a process with at least one thread.
The execution of a thread is preemptive.
Two. Thread creation and startup
Java uses the thread class to represent threads, and all thread objects must be instances of the thread class or its subclasses.
1. Two ways to create a thread: (1) inherit the thread class (2) Implement the Runnable Interface (common)
Which is better in two ways: the preferred way to implement the Runnable interface, because he can inherit other classes.
(1) Inherit the thread class
1 Public classThreaddemo {2 Public Static voidMain (string[] args) {3MyThread MyThread =NewMyThread ();4 Mythread.start ();5 for(inti = 0; I < 100; i++) {6System.out.println (Thread.CurrentThread (). GetName () + "--" +i);7 }8 }9 }Ten One classMyThreadextendsThread { A @Override - Public voidrun () { - for(inti = 1; I < 100; i++) { theSystem.out.println (Thread.CurrentThread (). GetName () + "--" +i); - } - } -}
(2) Implement runnable Interface (Common)
1 Public classRunnabledemo {2 Public Static voidMain (string[] args) {3Myrunnable Mr =Newmyrunnable ();4Thread thread =NewThread (MR);5 Thread.Start ();6 //Main thread Output7 for(inti = 0; I < 200; i++) {8System.out.println (Thread.CurrentThread (). GetName () + "--" +i);9 }Ten } One } A - classMyrunnableImplementsRunnable { - @Override the Public voidrun () { - for(inti = 0; I < 200; i++) { -System.out.println (Thread.CurrentThread (). GetName () + "--" +i); - } + } -}
Multithreading (i)