Java multi-thread programming

Source: Internet
Author: User

Java thread classes can have two options: one is to extend the Thread class, and then reload its run method; the other is to implement the runnable class to implement its run method, then, use the new thread (class implementing runnable) method to generate the corresponding thread object. Operations on this thread are implemented in the run method.

The running of a thread is to call its start method. It will call the run method. Remember not to call the run method directly.

After the thread is generated, the thread control and communication are completed.

Before talking about this, let's talk about the thread status. The thread has five states: Start, ready, run, blocked, and end. When a thread is ready after it starts, the thread can run as long as the CPU allocates time for it to run. After a thread obtains the CPU time, it starts to run, at this time, it is in the running state, and it is switched to the stopped state after the running is completed, or it is switched to the blocking state because it sleep for the CPU time, waiting for the results of other threads, and waiting for Io operations; A thread in the blocking state must be awakened before it can be switched to the ready state, waiting for the CPU time to be allocated. For more information, see the operating system.

You can use sleep (long Mills) of the thread class to sleep the thread, use yield to give the thread the CPU time for other threads to use, and use thread2.join () the method causes the thread that calls this method to be in the blocking state, and waits until the thread2 thread is in the ready state after it finishes running. The communication methods include wait, notify, and yyall. Wait does not need to wait until the thread ends to wake up, if the waiting thread meets a certain condition and calls y or yyall, it can wake up a thread or all threads waiting for it. The preceding methods can be used to implement inter-thread communication and collaboration.

Due to limited resources, if multiple threads use the same resources, conflicts may occur, that is, competition. In this case, synchronization is required, that is, the resource is locked. You can use the synchronized keyword to synchronize the resources to be used. There are two ways to use it: one is to use the critical section of the resource, that is, to perform operations on the resource using a method, declare this method as synchronize; one is to use synchronize (expr) {}, expr must be able to find the reference of the object, usually the resource to be used, the operation on the resource is in {}Code.

Well, since the thread can lock resources, there may be deadlocks. For example, thread 1 locks resource a first, thread 2 locks resource B first, thread 1 locks resource B, and thread 2 locks resource. Because thread 2 has locked resource B, thread 1 must wait for thread 2 to release resource B to run. Similarly, thread 2 must wait until thread 1 releases resource A to run, in this way, a deadlock occurs when the first, the second, and the second are equal to one.

The Method to Solve the deadlock is to use the resource sorting technology, that is, to define an order for each object to be locked, to ensure that each thread locks the object in this order. In the preceding example, if two objects are ordered by resource A and resource B, thread 2 must first lock resource a before locking resource B. Once thread 1 locks resource A, thread 2 can only wait for resource A to lock. Therefore, thread 1 can lock resource B without deadlock.

I have learned about Java multi-thread programming. If you have any other content, I would like to share it with you.

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.