Thread Pool, java Thread Pool

Source: Internet
Author: User

Thread Pool, java Thread Pool

Thread Pool
Solved the efficiency problem of multi-production and multi-consumption.
The objects in the JDK1.5 java. util. concurrent. locks package are used.
Lock interface: it has more operations than synchronized.
Lock (): Get the lock.
Unlock (): Release the lock.

The lock operation for Synchronous Code blocks or synchronous functions is implicit.
The JDK1.5 Lock interface encapsulates the Lock into an object separately according to the object-oriented idea.
And provides the lock display operation.

The Lock interface is an alternative to synchronization.
Replace synchronization in the thread with the Lock interface.

An error occurred while replacing the task. Because wait does not have the synchronization region and does not have the synchronization lock.
The synchronization has been upgraded. The Lock is not an arbitrary object, but an object of the Lock type.
So the monitor method bound to any object has also been upgraded. Is there a dedicated monitor method bound to the Lock type Lock?
Check the api. The Condition interface replaces the monitor method in the Object.

Previously, the monitor method was encapsulated into every object.
The monitor method is now encapsulated into the Condition object.
Method Name: await signal signalAll


How can the Condition object of the monitor be bound to Lock?

--- You can use the newCondition () method of the Lock interface.

However, the basis of the problem is the same as that of the local Party. Efficiency is still low!

// Describe the resource. Class Res {private String name; private int count = 1; // create a Lock. Private Lock lock = new ReentrantLock (); // creates a monitor object bound to Lock. Create two instances. // Producer monitor. Private Condition producer_con = lock. newCondition (); // consumer monitor private Condition consumer_con = lock. newCondition (); // define the tag. Private boolean flag; // you can assign values to a product. Public void set (String name) // {// obtain the lock. Lock. lock (); try {while (flag) // mark as true and run wait. Wait. Is false. Production. Try {producer_con.await ();} catch (InterruptedException e) {}// t0 (and so on) t1 (and so on) this. name = name + "--" + count; // bread 1, bread 2, bread 3 count ++; // 2 3 4System. out. println (Thread. currentThread (). getName () + "... producer ..... "+ this. name); // t0 bread 1, t0 bread 2 t1, and bread 3 // after generation, the flag is changed to true. Flag = true; // after production is complete, a consumer should be awakened to consume the data. Consumer_con.signal ();} finally {// release the lock. Lock. unlock () ;}/// provides a method to obtain the product. Public void get () // {lock. lock (); try {while (! Flag) try {consumer_con.await ();} catch (InterruptedException e) {}// t2 (etc.) t3 (etc.) System. out. println (Thread. currentThread (). getName () + "....... consumer ..... "+ this. name); // t2 bread 1. // change the flag to false. Flag = false; // after consumption, wake up the producer. Producer_con.signal () ;}finally {lock. unlock () ;}}// producer. Class Producer implements Runnable {private Res r; Producer (Res r) {this. r = r;} public void run () {while (true) r. set ("Bread") ;}}// Consumer class Consumer implements Runnable {private Res r; Consumer (Res r) {this. r = r;} public void run () {while (true) r. get () ;}} class NewProducerConsumerDemo {public static void main (String [] args) {// 1, create a resource. Res r = new Res (); // 2, create two tasks. Producer pro = new Producer (r); Consumer con = new Consumer (r); // 3. Create a thread. Thread t0 = new Thread (pro); Thread t1 = new Thread (pro); Thread t2 = new Thread (con); Thread t3 = new Thread (con); t0.start (); t1.start (); t2.start (); t3.start ();}}

 

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.