Thread Basics (second part) Java thread defects and side effects several solutions

Source: Internet
Author: User

<--the second part of the thread basics, we'll look at the flaws and side effects of using Java threads, and how to modify the running mechanism of threads in sun JDK 1.2-->

In the previous article, "Thread Basics for Java 101", we introduced the concept of threading and how to use threads. This month, we'll move on to more advanced topics, including threading flaws and side effects, and how to improve the threading mechanism in Sun JDK 1.2.

Synchronize (Sync)

Let's recall what was said in the previous article: threads allow two or more processes to execute simultaneously. In fact, these threads can also share objects and data, in which case you know that different threads cannot access the same data at the same time, because when you first design Java, you use the concept of threading, the Java language defines a special keyword synchronize (sync), This keyword can be applied to code blocks, and the code block also includes the entry method, which is designed to prevent multiple threads from executing code within the same code block at the same time.

Define a synchronized method, in the following format:

[Public|private] synchronized {type}

MethodName (...)

A simple example of applying the keyword synchronization to a method:

public class someClass {
public void aMethod() {
// Some code
synchronized(this) {
// Synchronized code block
}
// more code.
}
}

A synchronized keyword guarantees that only one thread at a time can execute the code snippet, and any other thread that uses that piece of code will be blocked until the first thread finishes executing the piece of code.

Deadlock and starvation

Definition of hunger-because other concurrent activation processes persist in resource requirements, it is not possible for an asynchronous process to be activated during the time of the passenger forecast.

The most commonly encountered thread of two defects is deadlock and starvation. A deadlock occurs when one or more processes, in a given task, interact, interfere, and cause one or more processes to wait forever. Similarly, hunger occurs when a process is permanently occupied with resources, leaving other processes without the resources.

First, let's look at the deadlock problem. Consider a simple example, if you get money from an ATM, but you see the following message "There is no cash now, please try again later." "You need money, so you wait a while and try again, but you see the same message," he said. At the same time, behind you, an armored vehicle is waiting to put the money in the ATM, but the armored truck won't make it to the ATM because your car is blocking the road. And you have to take the money before you leave the place. In this case, a deadlock occurs.

In the case of starvation, the system is not in a deadlock state because one process is still being processed, but the other processes will never be able to execute. In what kind of environment, will cause the occurrence of hunger, there is no predetermined good rules. And if one of the following four situations occurs, it can lead to deadlocks.

Mutual exclusion: a thread or process always occupies a shared resource, for example, to monopolize the resource.

Loop wait: Process A waits for process B, the latter is waiting for process C, and process C is waiting for process a.

Partial allocation: resources are partially allocated. For example, both processes A and B need to access a file, and all use the printer, process a obtains the file resources, process B obtains the printer resources, but two processes do not have full resources.

Lack of priority: a process accesses a resource, but it does not release the resource even if the process is in a blocked state.

If none of the above four scenarios occur, the system will not have a deadlock. Take a look at the example of the file/printer just now, and when one of the processes determines that it does not have the second resource it needs to release the first resource it has, then the second tutorial will have two resources and be able to run.

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.