Java Essentials Java Multi thread group and thread pool

Source: Internet
Author: User

In the previous article, we talked about the basic concepts and usage of threads, and we will continue to talk about some of the threads and thread pools.

Thread Group: Java.lang.Threadgroup

1. Introduction to Thread Groups

A thread group represents a collection of threads. In addition, thread groups can contain other thread groups. Thread groups form a tree in which each thread group has a parent thread group, in addition to the initial thread group. allows a thread to access information about its own thread group, but does not allow it to access information about its thread group's parent thread group or any other thread group.

2. How to construct thread groups

Threadgroup (String name)           constructs a new thread group. Threadgroup (threadgroup parent, String name)           creates a new thread group.

The second construction method above can specify the parent thread group of the newly created thread group and specify the name of the current thread group.

3. Common methods for thread groups

Common methods of thread groups are consistent with common methods of threading, but the thread group execution method affects the target of thread group or thread group specific each thread, such as the Setdaemon method, which in thread means that the scene is set as the daemon thread, and the only daemon thread left is the JVM exits automatically. In a thread group, the group is automatically destroyed when the last field in the group is destroyed. The interrupt method, however, means that all threads in the thread group are interrupted.

4. How to set which group the thread belongs to

  The method of constructing thread groups is provided in the method of constructing threads, as follows:

Thread (threadgroup Group, Runnable target)           assigns a new thread object. Thread (threadgroup group, Runnable Target, String name)           long  stackSize)           assigns a new thread object so that the Target, as its running object, takes the specified name as a member of the group of threads referenced by group and has the specified stack size. The thread (threadgroup Group, String name)           assigns a new thread object. 

 

Thread pool: Executorservice

 1. How to create a thread pool object

 Public Static Executorservice newfixedthreadpool (int nthreads)

This method is a static method under executors, executors defined in Executor,executorservice,scheduled Executorservice,threadfactory and callable plants and practical methods.

 2. How to submit thread execution

<T> future<t> Submit(callable<T> task )           a task that submits a return value is used to execute, returning a future that represents the pending result of the task. the future <?> submit    (Runnable Task)           submits a Runnable task for execution and returns a future that represents the task. 

3. How to end the thread pool

void     shutdown ()           initiates a sequential shutdown, performing previously submitted tasks, but not accepting new tasks. List<Runnable>    shutdownnow ()           tries to stop all active tasks that are being performed, pauses processing of the awaited task, and returns a list of tasks waiting to be executed. 

4. What is the future and what is the role

future   is an interface that represents the result of an asynchronous calculation. It provides a way to check whether the calculation is complete, wait for the calculation to complete, and get the result of the calculation . Only get method to get the result, if necessary, to block this method before the calculation is complete. Cancellation is performed by cancelfuturefuture<?> form type, and return null

We know that runnable does not provide a return method for threading results, so we are looking for a new tool class or interface, which is callable.

The callable interface is similar to Runnable, and both are designed for classes whose instances might be executed by another thread. However , Runnable does not return a result and cannot throw a checked exception.

5. Application of callable--asynchronous summation case

Importjava.util.concurrent.*; Public classThreadTest { Public Static voidMain (String args[])throwsexecutionexception, interruptedexception {//can execute Runnable object or thread represented by callable objectExecutorservice pool = Executors.newfixedthreadpool (2); Future<Integer> f1 = Pool.submit (NewMycallable (100)); Future<Integer> F2 = Pool.submit (NewMycallable (200)); //V get ()Integer I1 =F1.get (); Integer I2=F2.get ();        System.out.println (I1);        System.out.println (I2); //EndPool.shutdown (); }}classMycallableImplementsCallable<integer> {    Private intNumber ;  PublicMycallable (intNumber ) {         This. Number =Number ; } @Override PublicInteger Call ()throwsException {intsum = 0;  for(intx = 1; x <= number; X + +) {sum+=x; }        returnsum; }}

  

Java Essentials Java Multi thread group and thread pool

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.