Re-learning java basics (8): Basic knowledge of locks and java Basics

Source: Internet
Author: User

Re-learning java basics (8): Basic knowledge of locks and java Basics

1. thread status

/*** Synchronization method ** @ author tomsnail * @ date 12:15:30 on January 1, April 18, 2015 */public synchronized void test1 () {test1 ();}

 

2). Fairness/unfairness

Under fair conditions, all threads that need to obtain the lock have the same chance to obtain the lock. On the contrary, some threads can obtain the lock first.

    /**     * Creates an instance of {@code ReentrantLock}.     * This is equivalent to using {@code ReentrantLock(false)}.     */    public ReentrantLock() {        sync = new NonfairSync();    }    /**     * Creates an instance of {@code ReentrantLock} with the     * given fairness policy.     *     * @param fair {@code true} if this lock should use a fair ordering policy     */    public ReentrantLock(boolean fair) {        sync = fair ? new FairSync() : new NonfairSync();    }
The ReentrantLock construction method provides a fair/unfair mechanism, while synchronized does not have a clear guarantee of fairness/non-fairness, because it is scheduled by the operating system. The specific implementation and principles of fair locks and non-fair locks will be studied later.

4. deadlock

Java is a thread-level deadlock. I borrow a deadlock from a process: it refers to the situation where multiple threads wait for resources occupied by other threads cyclically and are deadlocked for an indefinite period of time.

Conditions for deadlock:

A). mutex Conditions

A resource can only be occupied by one thread for a period of time

B). Non-preemption Conditions

Other threads cannot be preemptible before the resource is released.

C). Occupation and application conditions

This thread already occupies one resource, but applies for a new resource, but the resource to be applied for is occupied by other threads

D). Cyclic waiting Conditions

Multiple Threads wait for other threads to release resources

public class DeadLockThread {    static final String a = "a";    static final String b = "b";        public static void main(String[] args) {        Thread a = new Thread(new DeadLockThread1());        Thread b = new Thread(new DeadLockThread2());        a.start();        b.start();    }}class DeadLockThread1 implements Runnable{    @Override    public void run() {        testb();    }        public void testa(){        synchronized (DeadLockThread.a) {            System.out.println(DeadLockThread.a);        }    }        public void testb(){        synchronized (DeadLockThread.b) {            System.out.println(DeadLockThread.b);                                    testa();        }    }    }class DeadLockThread2 implements Runnable{    @Override    public void run() {        testa();    }        public void testa(){        synchronized (DeadLockThread.a) {                        System.out.println(DeadLockThread.a);            testb();        }    }        public void testb(){        synchronized (DeadLockThread.b) {            System.out.println(DeadLockThread.b);        }    }    }

When the above four conditions are met at the same time, a deadlock will occur.

When a deadlock occurs, we can destroy the four conditions that fall down.

 

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.