I genius official free tutorial 41: Java Essentials thread deadlock

Source: Internet
Author: User

Thread Deadlockrefers to two or more than two threads in the execution process, because of competing resources or due to the communication between each other caused by a blocking phenomenon, if there is no external force, they will not be able to proceed. At this point the system is in a deadlock state or the system produces a deadlock, and these threads that are always waiting for each other are called deadlock threads.
For example: A store has two people PS and PB in the transaction, PS hand holding the goods to PB said, you give me the money I give you goods, and PB took the money to PS said you first give me the goods I am giving you money. Two of people in this stalemate, will never be able to make a deal, which constitutes a deadlock.
Example: Package thread.deadlock;/** * Create Deadlockdemo class: for testing deadlocks * @author Genius Federation-Yukun */public class Deadlockdemo {public static voi D main (string[] args) {//Create a shop object shop shop = new Shop ();//Create two thread objects threadbuy tbuy = new Threadbuy (shop); Threadsell Tsell = new Threadsell (shop); Tbuy.start (); Tsell.start ();}} /** * Create Cargo class: Get object locks just to create objects * @author Genius Federation-Yukun */class cargo{}/** * Create money (Cargo) Classes: Get object locks just to create objects * @author Genius Alliance-Zhao Chan */class money{}/** * Create shop (Store) class * @author Genius Federation-Yukun */class Shop {//Create Cargo object private Cargo Cargo = new Cargo ();//Create Mon EY Object Private Money Money = new ();//buyer method completes the function that the buyer wants public void buyer () {//Buyer holding the cash in his hand, corresponding to the code is to get a lock synchronized (Money) {System.out.println ("The buyer takes the money and says: first delivery"); try {//simulate the time to wait for the seller to deliver thread.sleep;} catch (Interruptedexception e) { E.printstacktrace ();} The buyer is holding the money at the same time want to get the goods in hand, and then pay synchronized (cargo) {System.out.println ("Buyer: Deal");}} Seller method to complete the function that the seller wants public void seller () {//Sellers hold the goods in their hands, corresponding to the code is to obtain cargo object lock synchronized (cargo) {System.out.println (" The seller said with the goods: "Pay first"); try {//Simulate waiting time for buyer to pay thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();} The seller is holding the goods at the same time want to get the money in hand, and then hand over the shipping synchronized ("The buyer: The deal") {System.out.println ("buyers");}}} /** * Create Threadbuy class * @author Genius Alliance-yukun */class Threadbuy extends Thread{private shop shop;public threadbuy (shop shop) {thi S.shop = shop;} @Overridepublic void Run () {//Purchased thread, method to execute purchase shop.buyer ();}} /** * Create Threadsell class * @author Genius Alliance-yukun */class Threadsell extends thread{private shop shop;public Threadsell This.shop = shop;} @Overridepublic void Run () {//Sell thread, execute Sell method Shop.seller ();}} Running results: The seller took the goods said: first pay the buyer to take the money said: first delivery
Deadlocks cause deals to never be achieved

Thread Prioritythe intent of the thread priority is to identify the probability of the thread acquiring the CPU, but thread scheduling is not scheduled in absolute order of priority, which is related to the operating system and the JVM.
The priority of threads in Java is defined by 10 levels, Range: 1 (thread.min_priority) ~ thread.max_priority), but different systems have different thread priority ranges, This makes it possible for multiple thread priorities to correspond to the same priority in an operating system, so many times the prioritization does not see the difference between the different priority levels.
The priority of a custom thread is the priority of the parent thread class by default
The default priority for the Thread class is 5 (thread.norm_priority)
Set thread priority using the SetPriority (int) method

Example: Package thread.priority;/** * Creating a parent Thread * @author Genius Federation-Yukun */public class Fatherthread extends Thread {//declares a parameterless constructor method publ IC Fatherthread () {//sets the priority of the current class object to 8this.setpriority (8);}} Package thread.priority;/** * Create Child threads * @author Genius Federation-Yukun */public class Sonthread extends Fatherthread {public static void Main (string[] args) {//Create Sonthread object and invoke the Start method with the created object to start the thread new Sonthread (). Start (); @Overridepublic void Run () {/* * uses the keyword this to call the GetPriority method, gets the priority of the current thread * and System.out.println the priority output to be obtained through the OUTPUT statement */ System.out.println ("sub-thread Priority:" + this.getpriority ());}} Run Result: Sub-thread Priority: 8

Background Threadruns in the background of the system and does not interact directly with the user, a thread called a background thread (also known as a service thread or a daemon thread).
Example: File download
Setdaemon ();//set thread as background thread


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

I genius official free tutorial 41: Java Essentials thread deadlock

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.