Java multithreading problems and processing notes

Source: Internet
Author: User

1. Deadlock

Multithreaded programming in the actual development of the network program, in the implementation of the client program is relatively simple, but in the server-side program implementation is not only a lot of use, and will appear more than the client problems.

Another multi-threaded problem that is easy to appear on the server side is the deadlock. A deadlock means that two or more threads are waiting indefinitely for a critical resource to be used. Or in front of the bathroom example to explain the deadlock, such as two people have reached the bathroom at the same time, and two people are more polite, the first person and the second person said: "You first, the second person and the first person said:" You first. These two people have been so courteous to each other that no one enters, this phenomenon is a deadlock. The two people here are like threads, and the bathroom is the critical resource here, and because the two threads have been humility, no one uses critical resources.

Deadlock not only makes the program can not achieve the expected function, but also waste the resources of the system, so in the server-side program, the harm is relatively large in the actual server-side program development, need to pay attention to avoid deadlock.

And deadlock detection is troublesome, and not necessarily every time appear, this requires that when testing the server-side program, be patient enough to carefully observe the performance of the program execution, if the performance is found to be significantly reduced, it is likely to be a deadlock, and then specifically find the cause of the deadlock and resolve the deadlock problem.

Deadlock is the most essential reason for the logical processing is not rigorous, in the consideration is not very comprehensive, so the general need to modify the program logic can be a good solution to deadlock.

2. Thread Priority

In daily life, such as train ticket window and so on often see "xxx First", then multithreading programming in each thread can also set priority?

In multithreaded programming, you support setting priorities for each thread. Higher-priority threads get more CPU execution time and receive faster response when queued for execution. In the actual program, you can set the thread that needs to be processed in time to a higher priority according to the need of logic, and set the thread with the low request to the lower priority level.

In the thread class, the totals specify three priority levels, respectively:

L max_priority--Highest priority

L norm_priority--Common priority, also default priority

L min_priority--Lowest Priority

In the thread object created earlier, the thread default priority is norm_priority, and when it is used, you can set the priority of the thread as needed using the SetPriority method in the thread class, which is declared as:

public final void setPriority(int newPriority)

If T is an initialized thread object and you need to set the priority of T to the highest level, the code that you implement is:

t. setPriority(Thread. MAX_PRIORITY);

This allows for more execution opportunities when the thread executes, which is priority execution. If the priority of the thread is not allowed to be set for security reasons, SecurityException exceptions are thrown.

The following is a thread that uses a simple output number to demonstrate the use of thread priorities, with the following sample code implemented:

Package priority;
/**

* Test Thread Priority

* Author by http://www.bt285.cn http://www.5a520.cn

*/

Public Class Testpriority {

public static void Main (string[] args) {

Printnumberthr EAD P1 = new Printnumberthread ("high priority");

Printnumberthread p2 = new Printnumberthread ("normal priority");

Printnumberthread p3 = new Printnumberthread ("low priority");

P1.setpriority (thread.max_priority);

P2.setpriority (thread.norm_priority);

P3.setpriority (thread.min_priority);

P1.start ();

P2.start ();

P3.start ();

}

}

Package priority

/**

* Output Number of threads

*/

public class Printnumberthread extends thread {

Str ing name;   

       Public Printnumberthread (String name) {

THIS.name = name;
                           
}

public void Run () {

try{

for (int i = 0;i < 10;i++) {

System.out.println (name + ":" + i);

}

}catch (Exception e) {}

}

}

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.