Several common problems in Java Multi-threading

Source: Internet
Author: User

As we all know, in Java to implement multi-threading, there are two ways, one is to continue the thread class, and the other is to implement the Runable interface.
1. What is the difference between a process and a thread?
  A process is an application that executes, and a thread is an execution sequence within a process. A process can have multiple threads. Threads are also called lightweight processes.
2. How do I create threads in several different ways? Which one do you like? Why?
There are three ways to create threads:
(1) Inherit the Thread class
(2) Implement Runnable interface
(3) The application can use the Executor framework to create a thread pool
implementing the Runnable interface is more popular because this does not require inheriting the thread class. This requires multiple inheritance (while Java does not support multiple inheritance) in cases where other objects have been inherited in the design of the application, and only interfaces can be implemented. At the same time, the thread pool is very efficient and easy to implement and use.
  3. A summary explanation of the various available states of the thread.
threads can be in the following states during execution:
Ready (Runnable): The thread is ready to run, not necessarily immediately start execution.
Running (Running): The code of the thread that the process is executing.
Waiting (Waiting): The thread is in a blocked state, waiting for the external processing to end.
Sleep (sleeping): The thread is forced to sleep.
I/O blocking (Blocked on I/O): Waits for I/O operation to complete.
Synchronous Blocking (Blocked on synchronization): waits for a lock to be acquired.
Death (Dead): The thread completed execution.
4. What is the difference between a synchronization method and a synchronized code block?
  in the Java language, each object has a lock. A thread can use the synchronized key to get a lock on an object. The synchronized keyword can be applied at the method level (coarse-grained locks) or at the code block level (fine-grained locks).
5. How does thread synchronization work inside the monitor? What level of synchronization should the program do?
monitors and locks are used in Java virtual machines. The monitor monitors a block of synchronization code to ensure that only one thread executes the synchronized code block at a time. Each monitor is associated with an object reference. The thread does not allow synchronization code to be executed until the lock is acquired.
6. What is a deadlock (deadlock)?
  A deadlock occurs when two processes are waiting for the other party to execute until execution is complete. As a result, two processes are trapped in infinite waiting.
7. How do I ensure that n threads can access n resources without causing a deadlock?
   when using multithreading, a very simple way to avoid deadlocks is to specify the order in which locks are acquired and force threads to acquire locks in the order specified. Therefore, if all the threads lock and release the lock in the same order, there will be no deadlock.

Several common problems in Java Multi-threading

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.