Byte Beat Test Development Interview 12.29

(1) The difference between coroutine and thread: https://www.cnblogs.com/guolei2570/p/8810536.html

    We know that multiple threads are relatively independent, have their own context, and the switching is controlled by the system; and the coroutine is also relatively independent and has its own context, but its switching is controlled by itself, and the current coroutine is switched to other coroutines by the current coroutine To control. 
    The difference between coroutine and thread: coroutine avoids meaningless scheduling, which can improve performance, but therefore, programmers must bear the responsibility of scheduling. At the same time, coroutine also loses the ability of standard threads to use multiple CPUs.
    Thread execution overhead is small, but it is not conducive to resource management and protection; while the process is just the opposite
    (1) The core of the computer is the CPU, which undertakes all computing tasks. It is like a factory, always running.
    (2) Assuming that the power of the factory is limited, it can only be used by one workshop at a time. In other words, when one workshop starts, all other workshops must stop. The implication behind this is that a single CPU can only run one task at a time.
    (3) A process is like a workshop in a factory, it represents a single task that the CPU can handle. At any time, the CPU always runs a process, and other processes are in a non-running state
    (4) A workshop can have many workers. They work together to complete a task. Threads are like workers in the workshop. A process can include multiple threads.
    (5) The space in the workshop is shared by workers, for example, many rooms are accessible to every worker. This symbolizes that the memory space of a process is shared, and each thread can use these shared memory.
    (6) However, the size of each room is different, and some rooms can only accommodate one person at most, such as a toilet. When there are people inside, no one else can enter. This means that when a thread uses some shared memory, other threads must wait for it to end before using this memory.
    (7) A simple way to prevent others from entering is to add a lock at the door. Those who arrived first locked the door, and those who arrived later saw the lock and lined up at the door, waiting for the lock to open before entering. This is called "mutual exclusion" (Mutual exclusion, abbreviated as Mutex), which prevents multiple threads from reading and writing a certain memory area at the same time.
    (8) There are also some rooms that can accommodate n people at the same time, such as the kitchen. In other words, if the number of people is greater than n, the extra people can only wait outside. This is like some memory area, which can only be used by a fixed number of threads. 
    (9) The solution at this time is to hang n keys at the door. Those who enter take a key, and hang the key back when they come out. Those who arrived later found that the key was empty, and knew that they had to wait in line at the door. This approach is called "semaphore" (Semaphore), used to ensure that multiple threads will not conflict with each other.
    (10) It is not difficult to see that mutex is a special case of semaphore (when n=1). In other words, the latter can be used instead of the former. However, because mutex is relatively simple and efficient, this design is still used when resource monopoly must be guaranteed.
    (11) The design of the operating system can therefore be summarized into three points:

    (11.1) In the form of multiple processes, multiple tasks are allowed to run simultaneously;

    (11.2) In the form of multithreading, a single task is allowed to be divided into different parts to run;

    (11.3) Provide a coordination mechanism to prevent conflicts between processes and threads on the one hand, and allow resources to be shared between processes and threads on the other hand.

(2) The difference between get and post and put: https://blog.csdn.net/qq_36183935/article/details/80570062
    PUT request: If the two requests are the same, the latter request will overwrite the first request. (So ​​PUT is used to change resources)
    Post request: The latter request will not overwrite the first request. (So ​​Post is used to increase resources)
    GET generates one TCP data packet; POST generates two TCP data packets. For a GET request, the browser will send the http header and data together, and the server will respond with 200 (return data); for POST, the browser will first send the header, the server will respond with 100 continue, the browser will then send data, and the server will respond. 200 ok (return data)

(3) Advantages and disadvantages of arrays and linked lists:
    arrays apply for a contiguous memory, which is fast to
    access and slow to modify. Linked lists are slow to access and fast to modify

(4) Fast queue

public class Test {
    public static void main(String[] args) {
//        System.out.println("test");
        int[] array = {1,3,5,4,2};
        int low = 0;
        int high = array.length-1;

        quickSort(array, low, high);

        for(int tmp : array){
            System.out.println(tmp);
        }

    }

    private static void quickSort(int[] array, int low, int high){
        if(low < high){
            int tmp = getIndex(array, low, high);
            quickSort(array, low, tmp-1);
            quickSort(array, tmp+1, high);
        }
    }

    private static int getIndex(int[] array, int low, int high) {
        int cur = array[low];
        while(low < high){
            while(low < high && array[low] < cur){
                low++;
            }
            while(high > low && array[high] > cur){
                high--;
            }
            int tmp = array[low];
            array[low] = array[high];
            array[high] = tmp;
        }
        array[low]=cur;
        return low;
    }
}

 

Guess you like

Origin blog.csdn.net/u011939633/article/details/103759283