Dark Horse Programmer--8 multi-line Cheng (ii)

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

How to synchronize static functions

If the synchronization function is statically decorated, what is the lock used?

Through validation, the discovery is not in this. This cannot be defined in a static method. When static into memory, there is no such object in memory, but there must be a corresponding bytecode file object for that class. Such as:

Class name. Class The type of the object is class

This is the lock used by the static function. While the static synchronous method, the lock used is the bytecode file object of the class in which the method resides. Class name.

Deadlock: When synchronization is nested in sync, there is a possibility of deadlock, because the lock is not released.

Inter-thread communication: In fact, multiple threads are operating the same resource, but the action is different.

(1) Wait (), notify (), Notifyall (), to manipulate the thread why is it defined in the object class?

A, these methods exist and are synchronized.

B, these methods must be used to identify the synchronized locks that belong to. The same thread that locks wait can only be awakened by the notify of the same lock.

C, a lock can be any object, so the method called by any object must be defined in the object class.

(2) What is the difference between wait () and sleep ()?

Wait (): Releases the CPU execution and releases the lock.

Sleep (): Releases the CPU execution and does not release the lock.

(3) Why do we have to define notifyall?

Because when you need to wake the other thread. If you use only notify, it is easy to wake up this thread only. Causes the threads in the program to wait.

Replace the synchronization synchronized with the displayed lock operation. Replaces Wait,notify,notifyall in object with the condition object. The condition object can be obtained through a lock lock and supports multiple related condition objects.

Stopping a thread

Before the JDK 1.5 release, there was a stop thread method, but after the upgrade, this method was obsolete.

So what if we stop the thread now?

There is only one way, that is to let the Run method end.

1, open multi-threaded operation, running code is usually the loop structure. As long as you control the loop, you can let the Run method end, that is, the thread ends, so long as in the main function or other threads, after the thread executes for a period of time, the tag flag is assigned false, the Run method will end and the thread will stop.

2, the above 1 method can solve the general situation, but there is a special case: that is, when the thread is in a frozen state. The tag is not read. Then the thread will not end.

When a frozen thread is returned to a running state without a specified way, the freeze needs to be purged. Forces the thread to revert to the running state. This makes it possible to manipulate the tag to end the thread. The thread class provides the method interrupt ();

Thread Other methods

1. Join method

When a thread executes the. Join () method of the B-thread, the A thread waits, and a thread executes until the B thread finishes executing. (At this point B and other threads run alternately.) Join can be used to temporarily join thread execution.

2, SetPriority () method to set the priority level

Max_priority Highest priority 10

Min_priority Lowest Priority 1

Norm_priority the default priority assigned to a thread

3. The yield () method can pause the current thread and allow other threads to execute.

Dark Horse Programmer--8 multi-line Cheng (ii)

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.