Java interview question 10

Source: Internet
Author: User

How many types of streams are available in java? JDK provides some abstract classes for each type of stream for inheritance. which classes are they? It mainly involves word throttling and dirty streaming. Byte streams inherit from InputStream and OutputStream, while bytes streams inherit from Reader and Writer. In java. there are many other streams in the io package. The lower-layer streams and the higher-layer streams are processed in one byte or character. The higher-layer streams are processed in batches of data, it is mainly used to improve performance and ease of use.. Understanding of the basic concepts of threads, the basic states of threads, and the relationship between states: threads, sometimes called Lightweight processes, are the basic units of CPU usage; it consists of thread ID, program counter, register set, and stack. It shares its code segment, data segment, and other operating system resources (such as opening files and Signals) with other threads belonging to the same process ). A thread is the smallest unit for executing operations in a process, that is, the basic unit for executing Processor Scheduling. If a process is understood as a task completed by the operating system logically, the thread represents one of the many possible subtasks to complete the task. A thread can be scheduled and executed independently on the processor. In this way, several threads can be executed independently on a single processor in a multi-processor environment. The operating system provides threads to achieve this concurrency conveniently and effectively. Benefits of introducing threads: (1) easy scheduling. (2) Improve concurrency. Concurrency can be easily and effectively realized through threads. A process can create multiple threads to execute different parts of the same program. (3) low overhead. Creating a thread is faster than creating a process and requires little overhead .. (4) facilitate the full use of the multi-processor function. By creating a multi-threaded process (that is, a process can have two or more threads), each thread runs on one processor, so as to realize the concurrency of the application, make each processor fully run. Basic status: Ready, running, suspended, and ended. Which of the following methods are available for multithreading? What are the implementation methods of synchronization? What keywords are used to modify the synchronization method? Why is the stop () and suspend () methods not recommended? 1. multithreading implementation method: Inherit the Thread class and implement the Runnable interface. 2. synchronous implementation method: ① synchronized ② combine wait with policy. Modify the synchronization method using synchronized. 3. For stop () and suspend (): it is not safe to use stop. It unlocks all the locks obtained by the thread, and if the object is in an inconsistent state, other threads can check and modify them in that state. It is difficult to check the real problem. The suspend () method is prone to deadlocks. When suspend () is called, the target thread stops, but still holds the lock obtained before that. In this case, no other thread can access the locked resources unless it is "suspended. For any thread, if they want to restore the target thread and attempt to use any locked resource, it will cause a deadlock. Therefore, you should not use suspend (), but set a flag in your Thread class to indicate whether the Thread should be active or suspended. If the flag indicates that the thread should be suspended, use wait () to enter the waiting state. If the flag indicates that the thread should be recovered, restart the thread with a notify. Fifty-nine. After a thread enters a synchronized method of an object, can other threads access other methods of this object? Strictly speaking, we should discuss the situation as follows: 1. If other methods do not have the synchronized keyword before, you can. 2. If the synchronized keyword exists before other methods, there are two possible cases: ① if the method calls wait internally, the object lock is released, and other synchronized methods can be entered. ② If this method does not call wait internally, no. 3. If other methods are static, the synchronization lock is the bytecode of the current class. Non-static methods cannot be synchronized because non-static methods use this. Therefore, this method does not affect synchronization. 60. Understanding of input and output streams: in java, the stream mechanism is used for data transmission. From file to memory, it is an input stream, and from memory to file is an output stream. The input stream can be read through read, the output stream is written in write or print. The stream can be divided into high-level streams and low-level streams. The lower layer processes the stream in bytes or characters, high-flow data is processed in batches.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.