Older Dick Silk Self-study note--java 0 basics to rookie--034

Source: Internet
Author: User

Lock lock, deadlock, thread group, threads pool, timer, exercises

1, Lock Lock (interface)

Lock ();

Unlock ();

Reentrantlock is the implementation class for lock.

2. Deadlock

The disadvantage of synchronization: low efficiency, easy to create deadlocks.

Deadlock: Two or more threads, two or more, occur in the process of contention for resources, a phenomenon of mutual waiting.

1  Public classMyLock {2      Public Static FinalObject obja=NewObject ();3      Public Static FinalObject objb=NewObject ();4 }5 6  Public classDielockextendsthread{7 8     Private BooleanFlag;9 Ten      PublicDielock (Booleanflag) { One          This. flag=Flag; A     } -  - @override the      Public voidrun () { -         if(flag) { -             synchronized(mylock.obja) { -System.out.println ("If A"); +                 synchronized(MYLOCK.OBJB) { -System.out.println ("If B"); +      A                 } at             } -}Else{ -             synchronized(MYLOCK.OBJB) { -System.out.println ("Else B"); - synchronzied (mylock.obja) { -System.out.println ("Else A"); in                 } -             } to         } +     } - } the  *  Public classfin{ $      Public Static voidMain (string[] args) {Panax NotoginsengDielock dielock1=NewDielock (true); -Dielock dielock2=NewDielock (false); the Dielock1.start (); + Dielock2.start (); A     } the } +  - //dead Lock

3. Thread Group

The Threadgroup class allows you to classify a batch of threads.

Getthreadgroup ();

4. Thread pool

Executros class, starting a new thread is too expensive to use the thread pool to increase the efficiency of threading (recycling is exhausted).

Newcachedthreadpool ();

Newfixedthreadpool ();

Newsinglethreadexecutor ();

The return value of the above three methods is the Executorservice object, which represents a thread pool that can execute a Runnable object or the threads represented by callable objects.

Submit ();

Shutdown ();

5. Timer

Timer class: Schedule ();

TimerTask class: Run (); , Cancel ();

6. Exercises

(1) What are the different ways to implement multithreading?

Three kinds: Inherit the thread class, implement Runnable interface, implement callable interface (combined with thread pool usage).

(2) What are the different ways to achieve synchronization?

Two kinds: Synchronous code block, synchronous method.

(3) What is the difference between starting a thread that is run () or start ()?

Start ().

Run (): Encapsulates code that is executed by the thread, called directly, and is equivalent to a call to a normal method.

Start (): Starts the thread and the run () method is called automatically by the JVM.

(4) What is the difference between sleep () and wait ()?

Sleep (): The time must be specified, and no lock is released.

Wait (): You can specify a time, or you can not specify a time, or release a lock.

(5) Why Wait (), notify (), Notifyall () are defined in the object class?

Because the invocation of these methods relies on the lock object, the lock of the synchronous code block is arbitrary, so it is defined in the object class that can represent any object.

Older Dick Silk Self-study note--java 0 basics to rookie--034

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.