Niuke.com Wrong Question Collection 9

1. Pipes

 The pipeline is actually a fixed-size buffer. The pipeline is a file for the processes at both ends of the pipeline, but it is not an ordinary file. It does not belong to a certain file system, but is an independent portal and constitutes a file alone. system, and only exists in memory. It is similar to the process communication mechanism of half-duplex channel in communication. A pipe can realize bidirectional data transmission, and at the same time, there can only be transmission in one direction at most, and cannot be carried out in both directions at the same time. The size of the pipe is usually one page in memory, and its size is not limited by the size of the disk. When the pipe is full, the process is blocked when writing to the pipe, and when the pipe is empty, the process is blocked when reading the pipe.
 
Because the pipeline adopts half-duplex communication mode. Therefore, data can only flow in one direction. The

pipe  is a buffer managed by the kernel, and its capacity is affected by many factors, including the size of the buffer, the size of the disk, etc.

When there is no information in the pipe, read from the pipe The fetching process will wait until the process on the other end puts the message. When the pipe is full of messages, the process trying to put the message will wait until the process on the other end gets the message. When both processes are terminated, the pipe also automatically disappears. Processes reading and writing to the pipe may be blocked, so C correctly 

connects one end of the pipe to the output of a process. This process will put information into the pipe. The other end of the pipeline is connected to the input of a process, and this process takes out the information put into the pipeline. The pipeline can read the process and write the process at the same time. 

Reference URL: http://www.cnblogs.com/biyeymyhjob/archive/2012/11/ 03/2751593.html 


2.

  Let's first look at a piece of code:


public abstract class Test {
    public static void main(String[] args) {
        System.out.println(beforeFinally());
    }

    public static int beforeFinally() {
        int a = 0;
        try {
            a = 1;
            return a;
        } finally {
            a = 2;
        }
    }
} /**
 * output: 1*/

From the results, it seems that the statements in `finally` are executed after `return`, but it is not. In fact, the statements in `finally` are executed before `return`. So the question is, since it is executed before, why is the value of `a` not overwritten?
The actual process is as follows: when the program executes the return method in the try{} statement, it will do one thing, store the result to be returned in a temporary stack, and then the program will not return immediately, but execute The program in finally{}, when executing `a = 2`, the program just overwrites the value of a, but does not update the value to be returned in the temporary stack. After the execution is completed, the main program will be notified that "finally the program is executed, and the request can be returned." At this time, the value in the temporary stack will be taken out and returned. Now it should be clear that the value to be returned is saved to the temporary stack.
Let's look at another example, slightly modifying the above program:
public abstract class Test {
    public static void main(String[] args) {
        System.out.println(beforeFinally());
    }

    public static int beforeFinally() {
        int a = 0;
        try {
            a = 1;
            return a;
        } finally {
            a = 2;
            return a;
        }
    }
} /**
 * output: 2
 */

Here, there is also a return in finally{}, then when this return is executed, the value in the temporary stack will be updated. Similarly, after the execution of finally, the main program will be notified that the request has returned, that is, the value in the temporary stack will be taken out and returned. So the return value is 2.



Compare the following

 (Multiple choice)

What is the output of the following program?




public class Test {
    public static String output = " ";

    public static void foo(int i) {
        try {
            if (i == 1) {
                throw new Exception();
            }
        } catch (Exception e) {
            output += "2";
            return;
        } finally {
            output += "3";
        } output += "4";
    }

    public static void main(String[] args) {
        foo(0);
        foo(1);
        System.out.println(output);
    }
}

Answer 3423



Refer to the following code

public class Test {
    public static String output = "";

    public static String foo(int i) {
        try {
            if (i == 1) {
                throw new Exception();
            }
        } catch (Exception e) {
            output +="2";
            return output;
        } finally {
            output +="3";
        } output +="4";
        return output;
    }

    public static void main(String[] args) {
//        foo(0);
//        System.out.println(output);//输出34
//        foo(1);
//        System.out.println(output);//输出23
// System.out.println(foo(0));//output 34
// System.out.println(foo(1));//output 2
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726681&siteId=291194637