Hengtai essay

Posted by chuspy on Thu, 23 Sep 2021 15:05:37 +0200

choice

What is incorrect about ArrayBlockingQueue?

  • Its main application scenario is the "producer consumer" model
  • It is thread safe
  • Allow element to be null
  • Set capacity that must be displayed

There is no parameterless structure, so the length must be initialized to prevent it from becoming an unbounded data queue. There may be a problem of excessive memory
The ReentrantLock lock lock is implemented internally, so the thread is safe,

Which of the following streams is character oriented

  • ObjectInputStream
  • BufferedWriter
  • FileInputStream
  • InputStreamReader

CharArrayReader reads the input stream from the character array
BufferedReader buffers the input character stream
Pipedereader input pipeline
InputStreamReader converts bytes into input streams of characters
FilterReader filters the input stream
StringReader reads the input stream from the string
LineNumberReader appends a line number to the input data
PushbackReader returns a character and puts this byte back into the input stream
Input stream read from file by FileReader

The following statement about thread communication is wrong

  • wait() has multiple overloaded methods, and the waiting time can be specified
  • You can call wait(), notify(), and notifyAll() to realize thread communication
  • wait() must be used in a synchronized method or code block
  • wait(), notify(), and notifyAll() are methods and subclasses provided by the Object class, which can be overridden

wait(), notify(), and notifyAll() are modified by final. Subclasses cannot be overridden

In java. What kind of data structure is used in the implementation of util.concurrent.locks.abstractqueuesynchronizer?

  • array
  • Hashtable
  • Bidirectional linked list
  • aggregate

View the source code, it is easy to know that it is a two-way linked list

Check the running results of the code

public class Demo1 {
    public static void main(String[] args) {


        Thread t = new Thread(){
            @Override
            public void run(){
                pong();
            }
        };

        t.run();
        System.out.println("ping");
    }

    static void pong(){
        System.out.println("pong");
    }
}

The result is:
pong
ping

There is a variable List strList, which loops through the correct items in the List

System.out.println("Mode 1:");
        Iterator<String> it = strList.iterator();
        while(it.hasNext()){
            String str = it.next();
            System.out.print(str+" ");
        }
        System.out.println();

        System.out.println("Mode 2:");
        for(String str:strList){
            System.out.print(str+" ");
        }
        System.out.println();

        System.out.println("Mode III");      //Error!!!!!!!
//        for(int i=0;i<strList.length;i++){
//            String str = strList[i];
//        }
        System.out.println();
        
        System.out.println("Mode 4:");     //Error!!!!!
//        List.foreach(strList,function(str){
//            
//        });

Brief answer

Given the preorder traversal and inorder traversal of binary tree, find the subsequent traversal?

public class Solution {
    public static TreeNode reConstructBinaryTree(int[] prev, int[] in) {
        //No matter what traversal method, the result length must be the same, which is the sum of points
        if (prev.length != in.length || prev.length < 1) {
            return null;
        }

        //There is only one node, the root node
        if (prev.length == 1) {
            return new TreeNode(prev[0]);
        }

        //Find the root node in the middle order traversal result
        int index = -1;
        for (int i = 0; i < in.length; i++) {
            if (in[i] == prev[0]) {
                index = i;
                break;
            }
        }
        //No, it means there is a problem with the data
        if (index == -1) {
            return null;
        }

        //Root node found
        TreeNode root = new TreeNode(prev[0]);
        //Get the preorder traversal result of the left subtree
        int[] lChildPrev = new int[index];
        System.arraycopy(prev, 1, lChildPrev, 0, index);
        //The middle order traversal result of the left subtree is obtained
        int[] lChildin = new int[index];
        System.arraycopy(in, 0, lChildin, 0, index);
        //Through recursion, the left subtree structure is obtained
        root.left = reConstructBinaryTree(lChildPrev, lChildin);

        //The preorder traversal result of the right subtree is obtained
        int[] rChildPrev = new int[in.length - 1 - index];
        System.arraycopy(prev, index + 1, rChildPrev, 0, in.length - 1 - index);
        //The middle order traversal result of the right subtree is obtained
        int[] rChildin = new int[in.length - 1 - index];
        System.arraycopy(in, index + 1, rChildin, 0, in.length - 1 - index);
        //Through recursion, the right subtree structure is obtained
        root.right = reConstructBinaryTree(rChildPrev, rChildin);
        //The complete binary tree structure is obtained
        return root;
    }

    //test
    public static void main(String[] args) {
        int[] prev = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
        TreeNode root = reConstructBinaryTree(prev, in);
        prevPrintTreeNode(root);
        System.out.println();
        inPrintTreeNode(root);
    }

    //test result
//1 2 4 7 3 5 6 8
//4 7 2 1 5 3 8 6
    public static void inPrintTreeNode(TreeNode root) {
        if (root == null) {
            return;
        }
        //Recursion is used
        inPrintTreeNode(root.left);
        System.out.print(root.val + " ");
        inPrintTreeNode(root.right);
    }

    public static void prevPrintTreeNode(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        //Recursion is used
        prevPrintTreeNode(root.left);
        prevPrintTreeNode(root.right);
    }

    public static void postPrintTreeNode(TreeNode root) {
        if (root == null) {
            return;
        }
        //Recursion is used
        postPrintTreeNode(root.left);
        postPrintTreeNode(root.right);
        System.out.print(root.val + " ");
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

The difference between synchronized and Lock

reference resources: https://blog.csdn.net/hefenglian/article/details/82383569

An SQL question, find out all duplicate fields, and the duplicate fields are output only once

SELECT  distinct(email) FROM person 

In the constructor parameters of java.util.ThreadPoolExecutor, there are three parameters: corePoolSize, maximumSize and workQueue. How do these three parameters affect the number of threads in the thread pool when adding tasks to the thread pool?

corePoolSize:

The basic size of the thread pool, and the default number of initialization threads of the thread pool; New threads are created on this basis only when the work queue is full.

Note: when the ThreadPoolExecutor is just created, the thread will not start immediately, but will not start until a task is submitted, unless the core thread is started in advance by calling prestartCoreThread/prestartAllCoreThreads. Considering the influence of keepAliveTime and allowCoreThreadTimeOut timeout parameters, the size of the thread pool is not necessarily corePoolSize when no task needs to be executed.

maximumPoolSize:
The maximum number of threads allowed in the thread pool. The current number of threads in the thread pool will not exceed this value. If the task in the queue is full and the current number of threads is less than maximumPoolSize, a new thread will be created to execute the task.

Note the largestPoolSize, which records the maximum number of threads in the thread pool in the whole life cycle. Why once? Because after the thread pool is created, you can call setMaximumPoolSize() to change the maximum number of running threads.

poolSize:

The number of current threads in the thread pool. When the value is 0, it means that there are no threads and the thread pool will terminate; At the same time, the poolSize will not exceed the maximumPoolSize.

workQueue:
Blocking queue, where executable threads are stored.

reference resources: https://blog.csdn.net/aitangyong/article/details/38822505

Topics: Java