Select 30 Java Multi-threaded face questions

Source: Internet
Author: User
Tags thread class volatile

1. The difference between threads and processes

A process is an execution instance of an application. For example, when you double-click the Microsoft Word icon, you start the process of running Word. The thread is the path in the execution process. In addition, a procedure can contain multiple threads. When you start Word, the operating system creates a process and starts executing the main thread of the process.

Because a process can be made up of multiple threads, a thread can be considered a "lightweight" process. Therefore, the essential difference between a thread and a process is that each one is used to complete the work. Threads are used for small tasks, while processes are used for more ' heavyweight ' tasks

2. What are the different ways to implement threading?

(1) Inheriting the thread class for multithreading

(2) Implement Runnable interface mode to realize multithreading

(3) using Executorservice, callable, and future to implement multi-threading with return results (not commonly used)

3. What are the various states of threads? How do they flow between them?

(1) New status

(2) Ready status

(3) Operating status

(4) Blocking status

(5) Death status

4. What is the difference between the start () and run () methods in a thread ?

Run () method: Calls the Runnable object's run () method within this thread, and can be repeated multiple calls;
Start () method: Starts a thread, calls the Runnable object's Run () method, and cannot start a thread more than once

5. How to terminate a thread? How gracefully to terminate a thread?

(1) Use the exit flag to cause the thread to exit normally, that is, the thread terminates when the Run method completes.

(2) Use the Stop method to forcibly terminate the thread (this method is deprecated, because stop and suspend, resume, can also cause unpredictable results).

(3) Use the interrupt method to break the thread.

6. What role does threadlocal play in multi-threading?

  It provides a new idea to solve the concurrency problem of multi-thread.

7. What is the difference between the wait () and sleep () methods in a thread?

The main difference between sleep and wait is that sleep and wait can cause threads to wait, but sleep does not release resources and wait frees resources.

Also, the wait method can only be executed in a synchronous block or in a synchronous method.

8. What are the various methods of multithreading synchronization?

1. Synchronization method

 Public synchronized void Save () {}

2. Synchronizing code blocks

synchronized (object) {}

3. Using special domain variables (volatile) for thread synchronization

classBank {//variables that need to be synchronized plus volatile            Private volatile intaccount = 100;  Public intGetaccount () {returnAccount ; }            //We don't need synchronized here anymore .             Public voidSaveintMoney ) { Account+=Money ; }}

9. What is a deadlock? How do I avoid deadlocks?

The so-called deadlock: refers to two or more than two processes in the execution process, because of the competition for resources caused by a mutual waiting phenomenon, if there is no external force, they will not be able to proceed. At this point the system is in a deadlock state or the system generates a deadlock, and these processes, which are always waiting on each other, are called deadlock processes. Because the resource consumption is mutually exclusive, when a process requests resources, so that the process without external assistance, never allocated the necessary resources and can not continue to run, which creates a special phenomenon of deadlock.

Although the process may occur during the operation of the deadlock, but the occurrence of deadlocks must also have certain conditions, the occurrence of deadlocks must have the following four necessary conditions.

1) Mutually exclusive condition: refers to the process of allocating resources to the exclusive use, that is, for a period of time a resource is occupied by only one process. If there are other processes requesting resources at this time, the requestor can wait until the resource-occupying process is freed.

2) Request and hold condition: means that the process has maintained at least one resource, but a new resource request has been made, and the resource has been occupied by another process, at which time the request process is blocked, but the other resources that you have obtained remain.

3) Non-deprivation conditions: refers to the process has been obtained resources, before the end of use, can not be deprived, can only be released by themselves when the use is complete.

4) Loop wait condition: refers to a deadlock, there must be a process-a circular chain of resources, that is, the process set {p0,p1,p2,,pn} P0 is waiting for a P1 to occupy the resources; P1 is waiting for P2 to occupy the resources, ... PN is waiting for resources that have been consumed by P0.

After a deadlock has occurred in the system, the deadlock should be detected in time, and appropriate measures should be taken to remove the deadlock. The current method of handling deadlocks can be attributed to the following four types:

1) Prevent deadlocks.

This is a simpler and more intuitive approach to proactive prevention. The method is to prevent deadlocks by setting certain constraints to destroy one or more of the four necessary conditions that produce deadlocks. The prevention of deadlocks is a more easily implemented method and has been widely used. However, due to the often restrictive conditions imposed, the system resource utilization and system throughput can be reduced.

2) Avoid deadlocks.

This method is also a pre-prevention strategy, but it does not have to take a variety of restrictive measures to destroy the four necessary conditions for deadlocks, but in the dynamic allocation process of resources, in a way to prevent the system from entering the unsafe state, so as to avoid the deadlock.

3) Detection of deadlocks.

This method does not need to take any restrictive measures beforehand, nor does it have to check if the system has entered the unsafe zone, which allows the system to deadlock during operation. However, the detection mechanism set up by the system can detect the occurrence of the deadlock in time, and determine the process and resources related to the deadlock, then take appropriate measures to remove the deadlock from the system.

4) Unlock the deadlock.

This is a measure to match the detection of deadlocks. When a deadlock has been detected in the system, the process must be freed from the deadlock state. A common practice is to undo or suspend some processes in order to reclaim some resources, and then assign those resources to a process that is already in a blocked state and make it ready to continue running. Deadlock detection and lifting measures, it is possible to make the system to achieve better resource utilization and throughput, but the implementation of the most difficult.

10, how to communicate between multiple threads?

Https://www.cnblogs.com/hapjin/p/5492619.html

11. How do threads return results? How do I get it?

12. What is the use of Violatile key words, and what is the difference between synchronized?

13, if new T1, T2, T3 three threads, how to ensure that they are executed sequentially?

14, how to control the same time only 3 threads run?

15. Why use a thread pool?

16, say a few common thread pool and talk about the working principle.

17. What is the difference between the thread pool startup threads submit () and execute ()?

18, talk about multi-threaded concurrency control in the countdown device, the loop fence is what, what application scenarios?

19, what is a live lock, hunger, no lock, deadlock?

20. What is atomicity, visibility, and order?

21. What is a daemon thread? What's the use?

22, how to interrupt a thread? How to ensure the interruption of business is not affected?

23. What is the yield () method used for?

24. What is the difference between a re-entry lock and a synchronized lock?

25. What kinds of usages do synchronized have?

26. What does the Fork/join framework do?

27, how to pass parameters to the thread?

28. Talk about thread-safe and unsafe collections.

29, what is the CAS algorithm? What are the applications in multi-threading.

30. What multithreading problems have you encountered? How did it all work out?

Select 30 Java Multi-threaded face questions

Related Article

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.