Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ A summary of Java multi-threaded programming - acquaintance multithreading     - Linux, modify / retrieve the root password (Linux)

- Ubuntu download install and configure Plank Dock (Linux)

- Use libpq under Ubuntu 14.04 (Linux)

- Java class HashSet (Programming)

- CentOS Linux Optimization and real production environment (Linux)

- Mount NFS network file system (Linux)

- Understanding the Linux load average on other UNIX-Like systems (Linux)

- Introduction to thread pooling and simple implementation (Programming)

- OpenSUSE GPG Comments (Linux)

- Use a soft Raid play multiple SSD hard drive performance and enhance data security (Linux)

- Linux rename command usage in learning to modify the file name (Linux)

- OpenvSwitch 2.1.2 shell script to start and stop (Linux)

- Sublime Text 3 (Linux)

- Help you to see Linux system architecture type 5 Common Commands (Linux)

- Getting Started with Java NIO (Programming)

- Archlinux installation tutorial (Linux)

- Python 3.5 will support Async / Await Asynchronous Programming (Programming)

- Linux see whether there is a hacker program (Linux)

- Use OpenSSL for RSA encryption and decryption (Linux)

- Minimum period string problem (Programming)

 
         
  A summary of Java multi-threaded programming - acquaintance multithreading
     
  Add Date : 2018-11-21      
         
         
         
  The concept of process, multi-process, thread, multi-thread

Process (process): execution path of the CPU. Popular to say that the system is running the program. For example, we open a browser, QQ and so on, these programs run once opened, the so-called process.

Multi-process: the system running multiple programs simultaneously. We should not be difficult to understand this, open the browser and we can also QQ chat, CS stand-alone games.

Thread (thread): In the process of running the operation unit. Such as Thunder download one of our tasks is to download a thread.

Multithreading: Similarly we can see, each of which has multiple independent process or have mutual cooperation relations run unit which we call multi-threading.

The first multi-threaded program

Java multi-threaded implementation in two ways: to inherit the parent class Thread achieve Runnable interface.

First, we implement a simple multi-threaded alternately through the program and outputs 1 to 20.

package MyJavaThread;

public class MyFirstThreadTest {
    
    public static void main (String [] args) {
        new Thread (new Runnable () {
            public void run () {
                int i = 1;
                while (i <= 20) {
                    System.out.println (Thread.currentThread () getName () + ":" + i ++.);
                }
            }
        .}) Start ();
        
        int i = 20;
        while (i> 0) {
            System.out.println (Thread.currentThread () getName () + ":" + i--.);
        }
    }
}

There is no real sense of multithreading

We know, CPU can only be assigned to a program at the same time resource (monocytes case), which is given the power to run a program, then we can see that once the program is actually run several CPU implementation of the right to switch back and forth to produce results. Thus make people feel that run concurrently, but very fast switching speed CPU Bale.

State of the thread

Thread has its own state, we call the life cycle: the initial state, operating status, freezing and final states.

Thread sunrise - initialization state

Initialize the thread of what we call a thread is created, which is an instance of a subclass of Thread, waiting to be start.

Thread youth - run state

When we call the method start time, when the thread is in the running state. But rigorous said at this time is not necessarily the thread will run immediately, it is a temporary state, namely the implementation of the queue among the CPU, CPU wait for execution reincarnation, get the execution right.

Thread cold - frozen

After the thread is called sleep method or by calling the wait method, give up the implementation of the CPU. However, after the freeze can return to running again, reacquire the execution right of the CPU. Of course, you can also interrupt or exception occurs when the direct-to-death state.

Thread ending - dead state

If the thread execution logic appeared fatal exception or the thread is completed, which means that the thread is to say byebye curtain call. Thread after death can not return to any state.

to sum up

A process has at least one thread running
Main function itself is a thread, we called the main thread
Multi-threaded approach can be inherited Thread class, you can also implement Runnable
No concurrent strict sense
JVM itself has a lot of background threads running, supporting our program
Initialization state only to the operating state
Status can run into a frozen state and state death
Can a frozen state to a running state and a state of death
The state of death can only accept the fact of death
     
         
         
         
  More:      
 
- Java Collections Framework interfaces map (Programming)
- Oracle 11g maintenance partitions (Seven) - Modifying Real Attributes of Partitions (Database)
- Let OpenCV face detection score output codes (Programming)
- Git uses a standard process (Linux)
- Learning OpenCV: (VS2010-openCV2.4.3-win7 configuration instructions) (Linux)
- How to configure security services under Linux (Linux)
- GNU Linux system variables (sysctl configuration commands) integrated use (Linux)
- Linux Shell Scripting multithreading (Programming)
- Linux console password solution (Programming)
- Comparison of one-time transaction and CTE insert data (Database)
- Use the Find command to help you find those files that need to be cleaned (Linux)
- Linux operating system to solve a serious fault handling (Linux)
- linux remote control software (Linux)
- To share some very useful Vim command (Linux)
- How to use the Linux command compress JPEG images (Linux)
- Linux Monitoring Command (Linux)
- How to use Linux to download music from Grooveshark (Linux)
- Android judgment toward camera pictures (Programming)
- Ubuntu Series Installation Docker (Linux)
- SQL Server automatic backup script (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.