Java Learning Lesson 25th (Multi-threaded (iv))-multithreading issues involved in a single-case design pattern

Source: Internet
Author: User

A single example design mode under multi-thread


Solving the problem of lazy-type safety and efficiency by using the form of double inference

A hungry man-/*class single{private static final single t = new single ();p rivate single () {}//private constructor, Ensure that other class objects cannot be directly new to the object instance public static single getinstance () {return t;}} *///Lazy class Single{private static single T = Null;private single () {} public static single getinstance ()   //synchronization function is not required , synchronizing the entire function resulting in reduced efficiency {if (t==null)//resolving efficiency problems {synchronized (Single.class)//Resolving security issues {if (t==null) T = new single ();}} return t;}}

so the development is still the application of a hungry man style, but in the interview of lazy-type, because of its high technical content


Ii. examples of deadlock demonstrations

Deadlocks are caused by waiting for a lock between threads.

Deadlocks: Common is, synchronous nesting

At the interview. Will let the writing deadlock program, just write out. It means the deadlock is understood.

Class Deadlock implements Runnable{private Boolean Flag;public Deadlock (Boolean flag) {//TODO auto-generated constructor Stubthis.flag = Flag;} public void Run () {if (flag) {while (true) synchronized (lock). A_lock) {System.out.println ("If...alock"); synchronized (LOCK. B_lock) {System.out.println ("If...block");}}} else {while (true) synchronized (lock). B_lock) {System.out.println ("Else...block"); synchronized (LOCK. A_lock) {System.out.println ("Else...alock");}}}} Class Lock{public static Final Object a_lock = new Object ();p ublic static Final Object b_lock = new Object ();} Class Main{public static void Main (string[] args) {Deadlock T1 = new Deadlock (true);D eadlock t2 = new Deadlock (false); Thread J1 = new Thread (t1); Thread J2 = new Thread (t2); J1.start (); J2.start ();}}

If...alock
Else...block

Locked, J1 thread take a lock, J2 thread take B lock, both are waiting, but do not release the lock, forming a deadlock, causing the program to die, in the development of the probability of deadlock is very low.

Because somewhere within the code, the CPU will definitely switch and there is a thread, so the probability of a deadlock is very low. But once it happens, the program is completely stuck.


Summary of thread synchronization issues and locks:

1, synchronized is to protect multiple threads at the same time access to the same resource when the internal members of the resource corruption.

2, Thread synchronization methods (non-identical) are implemented by locks, and each object instance has and has only one lock this, which is associated with a particular object. Once a thread acquires the lock, other threads accessing the object cannot access the object's other synchronization methods. A thread can have more than one lock 3, for a static synchronization method. Locks are for this class, and the lock object is the byte-code object (class) of the class.

locks for static and non-static methods do not interfere with each other.

A thread acquires locks that are acquired when a synchronization method is visited on another object in a synchronous method. 4, for synchronization. To be awake at all times on which object to synchronize. This is the key. 5, writing thread-safe classes, It is important to always be aware of the logic and security of competing access resources to multiple threads to make the right inference, to analyze atomic operations, and to ensure that other threads during atomic operations are unable to access competing resources. 6 when multiple threads are waiting for an object lock. A thread that does not get to the lock will have a blockage (deadlock).

Java Learning Lesson 25th (Multi-threaded (iv))-multithreading issues in a singleton design pattern

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.