Java Multithreading (five), multithreading other Knowledge Brief introduction

Source: Internet
Author: User

Java Multithreading (five), multithreading other knowledge Brief introductionCategory: Javase comprehensive knowledge points 2012-09-19 18:12 1413 people reading reviews (1) favorite reports

One, Thread group

[Java]View Plaincopyprint?
  1. /**
  2. * A thread group represents A set of threads. In addition, a thread
  3. * Group can also include other thread groups. The Thread groups form
  4. * A tree in which every thread group except the initial thread group
  5. * Has a parent.
  6. * <p>
  7. * A thread is allowed to access information on its own thread
  8. * Group, but not to access information on its thread group ' s
  9. * Parent Thread group or any other thread groups.
  10. *
  11. * @author unascribed
  12. * @version 1.66, 03/13/08
  13. * @since JDK1.0
  14. */
/** * A thread group represents A set of threads. In addition, a thread  * Group can also include the other thread groups. The thread groups form  * A tree in which every thread group except the initial thread group  * has a parent.  * <p> * A thread is allowed to access information about it own thread  * Group, but not to access information AB Out its thread group's  * Parent thread group or any other thread groups.  * * @author  unascribed * @version 1.66, 03/13/08 * @since   JDK1.0 * *
A thread group represents a series of threads. Also, a thread group can include other thread groups. In addition to the initial thread group, each thread group has a parent thread group, similar to the structure of the tree.

A thread can access information about its group of threads, and it cannot access its parent thread group and other thread groups.

From this paragraph you can probably understand the concept of thread groups, and all threads and thread groups form the structure of a tree, as follows:

Looking at the thread's API, you can see that creating a thread can specify its thread group and do not specify a thread group. If you specify the thread group to which it belongs, the thread group is the child thread group of the thread group to which the thread that created it belongs. If you do not specify a thread group, it is the default, and the thread is the same thread group as the thread that created it.

Take a simple example from the diagram above:

If the main thread creates a Thread1 thread and does not specify the thread group that the THREAD1 is in, then the THREAD1 is the default and the main thread belongs to the same thread group, the system thread group.

If the main thread creates a Thread3 thread and does not specify that the thread group that the THREAD3 is in is thread Group 1, then thread Group 1 belongs to the system thread group, and the main thread is peer in the tree structure.

Once a thread has joined the specified thread group, the thread will always belong to that thread group until the thread dies, and the thread can not change the thread group it belongs to when it is running. Because the thread group that you specify is the one on which the thread is created, you can no longer modify the thread group it is in.

Here is a summary of Threadgroup methods

Method Summary
 int activeCount()
Returns the estimated number of active threads in this thread group.
 int activeGroupCount()
Returns the estimated number of active thread groups in this thread group.
 boolean allowThreadSuspension(boolean b)
is obsolete. The definition of this call depends on suspend() it being deprecated. Further, the behavior of this call is never specified.
 void checkAccess()
Determines whether the currently running thread has permission to modify this thread group.
 void destroy()
Destroys this thread group and all its child groups.
 int enumerate(Thread[] list)
Copies all active threads in this thread group and its subgroups into the specified array.
 int enumerate(Thread[] list, boolean recurse)
Copies all the active threads in this thread group into the specified array.
 int enumerate(ThreadGroup[] list)
Copies a reference to all active subgroups in this thread group into the specified array.
 int enumerate(ThreadGroup[] list, boolean recurse)
Copies a reference to all active subgroups in this thread group into the specified array.
 int getMaxPriority()
Returns the highest priority for this thread group.
 String getName()
Returns the name of this thread group.
 ThreadGroup getParent()
Returns the parent thread group for this thread group.
 void interrupt()
Interrupts all threads in this thread group.
 boolean isDaemon()
Tests whether this thread group is a background program thread group.
 boolean isDestroyed()
Tests whether this thread group has been destroyed.
 void list()
Prints information about this thread group to standard output.
 boolean parentOf(ThreadGroup g)
Tests whether this thread group is a thread group parameter or one of its ancestor thread groups.
 void resume()
is obsolete. This method is only used for federated Thread.Suspend and threadgroup.suspend , because they are inherently prone to deadlocks, so both are deprecated. For more information, see Thread.suspend() .
 void setDaemon(boolean daemon)
Change the background program state for this thread group.
 void setMaxPriority(int pri)
Sets the highest priority for the thread group.
 void stop()
is obsolete. This method has inherent insecurity. For more information, see Thread.stop() .
 void suspend()
is obsolete. This method easily leads to deadlocks. For more information, see Thread.suspend() .
 String toString()
Returns a string representation of this thread group.
 void uncaughtException(Thread t,Throwable e)
Thread.UncaughtExceptionHandlerThis method is called by Java Virtual Machine when a thread in this thread group is stopped because of an uncaught exception and the thread is not installed specific.

two, thread groups and unhandled exceptions

Starting with JDK1.5, Java enforces thread exception handling, and if an unhandled exception is thrown during thread execution, the JVM will automatically look for a corresponding Thread.uncaughtexceptionhandler object before ending the thread, and if the processor object is found, the object's Uncaughtexception (Thread t,throwable e) method to handle the exception.

Thread.uncaughtexceptionhandler is an internal public static interface for the thread class, and there is only one method within that interface:

void Uncaughtexception (thread t,throwable t), in which t represents the thread on which the exception occurred, and E represents the exception that the thread throws.

The thread class provides two methods to set up an exception handler:

public static void Setdefaultuncaughtexceptionhandler (Uncaughtexceptionhandler eh)

The default exception handler is set for all thread instances of the thread class

public void Setuncaughtexceptionhandler (Uncaughtexceptionhandler eh)

Sets the exception handler for the instance of the specified thread

The Threadgroup class implements the Thread.uncaughtexceptionhandler interface, so the thread group that each thread belongs to will be the default exception handler. When a thread throws an unhandled exception, the JVM first looks for the exception handler that corresponds to the exception (the exception handler set by the Setuncaughtexceptionhandler method), and if the exception handler is found, the exception handler is called to handle the exception, otherwise The JVM will call the Uncaughtexception method of the thread group object to which the thread belongs to handle the exception, and the process of handling the exception by the thread group is as follows:

1), if the thread group has a parent thread group, call the parent thread group's Uncaughtexception method to handle the exception

2), otherwise, if the thread class to which the thread instance belongs has a default exception handler (the exception handler set by the Setdefaultuncaughtexceptionhandler method), then the exception handler is called to handle the exception

3), otherwise, prints the information of the exception debug stack to the System.err error output stream and ends the thread.

Look at the following example:

[Java]View Plaincopyprint?
  1. Class MyHandler implements thread.uncaughtexceptionhandler{
  2. @Override
  3. public void Uncaughtexception (Thread t, Throwable e) {
  4. System.out.println ("An exception has occurred");
  5. E.printstacktrace ();
  6. }
  7. }
  8. Public class test{
  9. public static void Main (string[] args) {
  10. Thread.CurrentThread (). Setuncaughtexceptionhandler (new MyHandler ());
  11. int a=1/0;
  12. }
  13. }
Class MyHandler implements thread.uncaughtexceptionhandler{@Overridepublic void Uncaughtexception (Thread t, throwable e) {System.out.println ("An exception has occurred"); E.printstacktrace ();}} public class Test{public static void Main (string[] args) {thread.currentthread (). Setuncaughtexceptionhandler (New MyHandler ()); int a=1/0;}}

The exception handler was set in the main thread and the exception was finally caught.

Iii. Callable and future

Reference: http://lavasoft.blog.51cto.com/62575/222082

Four, volatile keywords

Reference: http://lavasoft.blog.51cto.com/62575/222076

Five, display the synchronization lock

Reference: http://lavasoft.blog.51cto.com/62575/222084

Java Multithreading (five), multithreading other Knowledge Brief introduction (GO)

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.