Thread pool and Threadlocal

Source: Internet
Author: User

Thread pool and Threadlocal

Thread pool: The thread pool is designed to enable threads to be used in loops, threads are kept in the thread pool, and threads are used to constructor thread objects when a task needs to use threads. Thread constructor threads can be reused, so the resources can be used better. When the number of tasks is more appropriate to use the thread pool, because it is not always possible to open the thread indefinitely, in case the number of tasks is thousands of, you have to open thousands of threads, so the resources are more wasted. If you use a thread pool, you can reuse threads in the thread pools without having to open threads all the time, and all threads can be reused.

How the thread pool is opened:

Thread pool is opened by executors class, there are several types of thread pool, there are fixed number of threads, there are not fixed quantity, there is timing function, and there is single thread.

1. Create a thread pool with a fixed number of threads:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710001.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

Operation Result:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710002.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

2. Create a thread pool that does not have a fixed number of threads:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710003.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

Operation Result:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710004.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

3. Create a thread pool with timed functions:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710005.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

Operation Result:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710006.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

4. Create a single-thread pool:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710007.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

Operation Result:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710008.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

Threadlocal class:

Before you say Threadlocal class, look at a problem. If there are several methods of a, B, C, D, E, these methods do not have parameters except the A method, but want to pass a value from a method to the E method? Think about it. It seems that using a static property as an intermediate medium can achieve the effect of passing.

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710009.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

Although there seems to be no problem at all, the problem is that if multiple threads are called at the same time, there will be a problem with the value being overwritten and there will be confusion on the data.

code example: 650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710010.jpg "alt=" image "Style=" Border-style: None;background-color:rgb (255,255,255); "/> 650) this.width=650; src=" http://os78f4hny.bkt.clouddn.com/ 20170710011.jpg "alt=" image "Style=" Border-style:none;background-color:rgb (255,255,255); "/>

Operation Result:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710012.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>

It can be seen from the running results of the code that the data is covered. At this point, it may be said that each time you build Test1 objects to make calls, you can avoid this problem. Yes, it is true that every time you build an object, you can avoid this problem, but what if it is static, not that there will only be one situation in the development process, so if this is the static situation, the use of this method will not be solved, and even there will be problems.

While the above approach is OK for instance, there is another way to solve this problem using the Hashtable collection, where the Hashtable collection is a set of key-value pairs, and a key corresponds to a value. Because of this feature, we can use the name of the thread as the key value, and then store the value of the thread in the Hashtable collection. This way, the value in the method can be taken to the corresponding value of the line threads, so the value of other threads will not be overwritten, using this method can be used to solve the static situation.

To get to the point, the use of Hashtable collection or a little bit of trouble, so there is a more convenient solution is to use the Threadlocal class, use the Threadlocal class's set to set the value in, and then use the Get method to get the value on it, do not need to set the key value, It is more convenient than Hashtable.

code example:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710013.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255), "/> 650" this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710014. JPG "alt=" image "Style=" Border-style:none;background-color:rgb (255,255,255); "/>

Operation Result:

650) this.width=650; "src=" http://os78f4hny.bkt.clouddn.com/20170710015.jpg "alt=" image "style=" border-style:none; Background-color:rgb (255,255,255); "/>


Thread pool and Threadlocal

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.