Hengtai essay

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


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(){
            public void run(){


    static void pong(){

The result is:

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();
            String str = it.next();
            System.out.print(str+" ");

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

        System.out.println("Mode III");      //Error!!!!!!!
//        for(int i=0;i<strList.length;i++){
//            String str = strList[i];
//        }
        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;
        //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;

    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);

    //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) {
        //Recursion is used
        System.out.print(root.val + " ");

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

    public static void postPrintTreeNode(TreeNode root) {
        if (root == null) {
        //Recursion is used
        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?


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.

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.


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.

Blocking queue, where executable threads are stored.

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

Topics: Java