An overview of the

A process is a running program, it is an independent unit of system resource scheduling, and a process can perform multiple tasks, and the thread is the program to execute the task, it is the basic unit of the program using CPU, so it can also be said that the thread is dependent on the process.

process

A process is a running program. It is an independent unit of system resource scheduling as well as the basic unit of operating system operation. Each process does not affect each other because the system allocates different space and resources to them. It is divided into single process and multi process.

Single process of a computer can only do one thing at a time, and the processes of a computer can do a do different things, such as listening to music, listen to play the game, although the two things to feel is going on at the same time, but is actually doing the CPU efficient switching between programs, that makes us feel was conducted at the same time.

thread

Thread is the task performed by the program (process), which is divided into single thread and multi-thread.

Single-threaded means that you do one thing and you don’t get distracted from doing anything else, so that your program has only one path of execution; Multithreading is can devote more road to do the same thing, also is the application of multiple execution paths, such as the three partners get lost, you go to ask the way people respectively, finally we set at the destination, so the existence of multithreading, not improve the execution speed, is in order to improve the utilization rate of application, And execution are vying for the CPU resources, or rob the executive power of CPU, and one of them if a process execution path is more, there will be a higher chance to get the executive power of the CPU, but the process is random, don’t know what a thread where will account for a moment the resource, so the thread of execution have randomness.

A program must have at least one process, and a process must have at least one thread.

The advantages and disadvantages

Thread execution costs little, but is not conducive to resource management and protection. Threads are suitable for running on SMP machines (dual CPU systems).

Process execution is expensive, but it can manage and protect resources well. Processes can be moved forward across machines.

How many ways can multithreading be implemented? What’s the difference?

There are two ways to implement multithreading :(there have been three since JDK1.5, the last one is not commonly used)

1. Inherit the Thread class

2. Implement Runnable interface (Callable interface)

A class that implements the Runnable interface or inherits the Thread class is a multithreaded class. To implement multithreading, you need to override the run() method, so the run() method is the entry point to multithreading.

The difference between the two implementations of multithreading:

1.Thread is a subclass of the Runnable interface. The implementation of the Runnable interface overcomes the limitations of Java single inheritance

2. The Runnable interface implements multithreading to better describe the concept of data sharing than inheriting Thread classes

Code demo

Buy train tickets, a total of 100 tickets, and it has 3 ticket window ticket, please design a program to simulate the train ticket system, by implementing the Runnable interface to achieve (simulate network delay).

To solve the thread-safety problem of allowing only one thread to enter and preventing another from entering, use Synchronize or inherit a Lock.

Java memory model provides for all variables are stored in main memory, each thread has its own working memory, working memory saved the variables used by the thread (these variables from main memory copy), the thread of variables all the operations (read, assignment) must be done in working memory, Different threads cannot directly access variables in each other’s working memory, and the transfer of variable values between them needs to be completed through the main memory. To solve this problem, Java provides the volatile keyword, which guarantees memory visibility.

The synchronize keyword code block

Public class Ticket implements Runnable{// Set Ticket = 100; Object obj = new Object(); @Override public voidrun() {
        while (true){
            synchronized (obj) {
                if(ticket > 0) { try { Thread.sleep(1000); // Simulate network latency} catch (InterruptedException e) {e.printStackTrace(); } System.out.println(Thread.currentThread().getName() +"For sale" + (ticket--) + "Ticket"+"= = = = ="+Thread.currentThread().getPriority());
                }else{// The pawn ticket does not meet the condition to exit the loopbreak;
                }
            
            }

        }
        	
    }
    
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        
        Thread thread1 = new Thread(ticket);
        thread1.setName("Window 1");
        
        Thread thread2 = new Thread(ticket);
        thread2.setName("Window 2");
        
        Thread thread3 = new Thread(ticket);
        thread3.setName("Window 3"); Thread1.start (); thread2.start(); thread3.start(); }}Copy the code

Inherit Lock

Public class Ticket implements Runnable{// Set Ticket = 100; Lock lock = new ReentrantLock(); @Override public voidrun() {
        while (true){
                lock.lock();
                if(ticket > 0) { try { Thread.sleep(50); // Simulate network latency} catch (InterruptedException e) {e.printStackTrace(); } System.out.println(Thread.currentThread().getName() +"For sale" + (ticket--) + "Ticket"+"= = = = ="+Thread.currentThread().getPriority());
                    lock.unlock();
                }else{// The pawn ticket does not meet the condition to exit the loopbreak;
                }

        }
        	
    }

    public static void main(String[] args) {
        Ticket2 ticket = new Ticket2();
        
        Thread thread1 = new Thread(ticket);
        thread1.setName("Window 1");
        thread1.setPriority(4);
        
        Thread thread2 = new Thread(ticket);
        thread2.setName("Window 2");
        thread2.setPriority(5);
        
        Thread thread3 = new Thread(ticket);
        thread3.setName("Window 3"); thread2.setPriority(7); Thread1.start (); thread2.start(); thread3.start(); }}Copy the code

The execution result

100 tickets for sale at window 1 =====5 99 tickets for sale at window 1 =====5 98 tickets for sale at window 1 =====5 97 tickets for sale at window 1 =====5 96 tickets for sale at window 1 =====5 95 tickets for sale at window 1 94 tickets for sale at window 1 =====5 93 tickets for sale at window 3 =====5 92 tickets for sale at window 2 =====5 91 tickets for sale at window 2 =====5 90 tickets for sale at window 3 =====5 89 tickets for sale at window 1 88 tickets for sale at window 1 =====5 87 tickets for sale at window 3 =====5 86 tickets for sale at window 3 =====5 85 tickets for sale at window 3 =====5 84 tickets for sale at window 2 =====5 83 tickets for sale at window 2 82 tickets for sale at window 3 =====5 81 tickets for sale at window 1 =====5 80 tickets for sale at window 1 =====5 79 tickets for sale at window 1 =====5 78 tickets for sale at window 1 =====5 77 tickets for sale at window 1 76 tickets for sale at window 3 =====5 75 tickets for sale at window 2 =====5 74 tickets for sale at window 3 =====5 73 tickets for sale at window 1 =====5 72 tickets for sale at window 3 =====5 71 tickets for sale at window 2 70 tickets for sale at window 3 =====5 69 tickets for sale at window 1 =====5 68 tickets for sale at window 3 =====5 67 tickets for sale at window 2 =====5 66 tickets for sale at window 3 =====5 65 tickets for sale at window 3 64 tickets for sale at window 1 =====5 63 tickets for sale at window 3 =====5 62 tickets for sale at window 2 =====5 61 tickets for sale at window 2 =====5 60 tickets for sale at window 3 =====5 59 tickets for sale at window 3 58 tickets for sale at window 3 =====5 57 tickets for sale at window 1 =====5 56 tickets for sale at window 1 =====5 55 tickets for sale at window 1 =====5 54 tickets for sale at window 1 =====5 53 tickets for sale at window 1 52 tickets for sale at window 3 =====5 51 tickets for sale at window 3 =====5 50 tickets for sale at window 3 =====5 49 tickets for sale at window 3 =====5 48 tickets for sale at window 3 =====5 47 tickets for sale at window 3 46 tickets for sale at window 3 =====5 45 tickets for sale at window 2 =====5 44 tickets for sale at window 3 =====5 43 tickets for sale at window 1 =====5 42 tickets for sale at window 1 =====5 41 tickets for sale at window 1 40 tickets for sale at window 1 =====5 39 tickets for sale at window 1 =====5 38 tickets for sale at window 1 =====5 37 tickets for sale at window 1 =====5 36 tickets for sale at window 1 =====5 35 tickets for sale at window 1 34 tickets for sale at window 1 =====5 33 tickets for sale at window 1 =====5 32 tickets for sale at window 1 =====5 31 tickets for sale at window 1 =====5 30 tickets for sale at window 1 =====5 29 tickets for sale at window 1 Window 1 is selling 28 tickets =====5 Window 1 is selling 27 tickets =====5 Window 1 is selling 26 tickets =====5 window 1 is selling 25 tickets =====5 window 1 is selling 24 tickets =====5 window 1 is selling 23 tickets =====5 22 tickets for sale at window 3 =====5 21 tickets for sale at window 3 =====5 20 tickets for sale at window 3 =====5 19 tickets for sale at window 2 =====5 18 tickets for sale at window 3 =====5 17 tickets for sale at window 3 16 tickets for sale at window 3 =====5 15 tickets for sale at window 3 =====5 14 tickets for sale at window 1 =====5 13 tickets for sale at window 1 =====5 12 tickets for sale at window 1 =====5 11 tickets for sale at window 1 Window 1 is selling 10 tickets =====5 Window 1 is selling 9 tickets =====5 Window 1 is selling 8 tickets =====5 Window 1 is selling 7 tickets =====5 Window 3 Is selling 6 tickets port 5 window 3 is selling 5 tickets port 5 window 3 is selling 4 tickets port 5 Window 3 tickets are selling 3 = = = = = 5 window 2 tickets are sold in that 3 = = = = = 5 window 3 tickets are selling 1 = = = = = 5Copy the code