< prev index next >

src/java.base/share/classes/java/lang/Thread.java

Print this page
8200123: Replace Thread.init with telescoping constructor
Reviewed-by: dholmes, mchung, plevart


 171     private static int threadInitNumber;
 172     private static synchronized int nextThreadNum() {
 173         return threadInitNumber++;
 174     }
 175 
 176     /* ThreadLocal values pertaining to this thread. This map is maintained
 177      * by the ThreadLocal class. */
 178     ThreadLocal.ThreadLocalMap threadLocals = null;
 179 
 180     /*
 181      * InheritableThreadLocal values pertaining to this thread. This map is
 182      * maintained by the InheritableThreadLocal class.
 183      */
 184     ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
 185 
 186     /*
 187      * The requested stack size for this thread, or 0 if the creator did
 188      * not specify a stack size.  It is up to the VM to do whatever it
 189      * likes with this number; some VMs will ignore it.
 190      */
 191     private long stackSize;
 192 
 193     /*
 194      * JVM-private state that persists after native thread termination.
 195      */
 196     private long nativeParkEventPointer;
 197 
 198     /*
 199      * Thread ID
 200      */
 201     private long tid;
 202 
 203     /* For generating thread ID */
 204     private static long threadSeqNumber;
 205 




 206     /*
 207      * Java thread status for tools, default indicates thread 'not yet started'
 208      */
 209     private volatile int threadStatus;
 210 
 211     private static synchronized long nextThreadID() {
 212         return ++threadSeqNumber;
 213     }
 214 
 215     /**
 216      * The argument supplied to the current call to
 217      * java.util.concurrent.locks.LockSupport.park.
 218      * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
 219      * Accessed using java.util.concurrent.locks.LockSupport.getBlocker
 220      */
 221     volatile Object parkBlocker;
 222 
 223     /* The object in which this thread is blocked in an interruptible I/O
 224      * operation, if any.  The blocker's interrupt method should be invoked
 225      * after setting this thread's interrupt status.
 226      */
 227     private volatile Interruptible blocker;
 228     private final Object blockerLock = new Object();
 229 
 230     /* Set the blocker field; invoked via jdk.internal.misc.SharedSecrets
 231      * from java.nio code
 232      */
 233     static void blockedOn(Interruptible b) {
 234         Thread me = Thread.currentThread();


 360      *         }
 361      *
 362      *         void readAndProcessEvent() {
 363      *             // Read event from some source and process it
 364      *              . . .
 365      *         }
 366      *     }
 367      * }</pre>
 368      * <p>
 369      * The code above would remain correct even if the {@code onSpinWait}
 370      * method was not called at all. However on some architectures the Java
 371      * Virtual Machine may issue the processor instructions to address such
 372      * code patterns in a more beneficial way.
 373      *
 374      * @since 9
 375      */
 376     @HotSpotIntrinsicCandidate
 377     public static void onSpinWait() {}
 378 
 379     /**
 380      * Initializes a Thread with the current AccessControlContext.
 381      * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean)
 382      */
 383     private void init(ThreadGroup g, Runnable target, String name,
 384                       long stackSize) {
 385         init(g, target, name, stackSize, null, true);
 386     }
 387 
 388     /**
 389      * Initializes a Thread.
 390      *
 391      * @param g the Thread group
 392      * @param target the object whose run() method gets called
 393      * @param name the name of the new Thread
 394      * @param stackSize the desired stack size for the new thread, or
 395      *        zero to indicate that this parameter is to be ignored.
 396      * @param acc the AccessControlContext to inherit, or
 397      *            AccessController.getContext() if null
 398      * @param inheritThreadLocals if {@code true}, inherit initial values for
 399      *            inheritable thread-locals from the constructing thread
 400      */
 401     private void init(ThreadGroup g, Runnable target, String name,
 402                       long stackSize, AccessControlContext acc,
 403                       boolean inheritThreadLocals) {
 404         if (name == null) {
 405             throw new NullPointerException("name cannot be null");
 406         }
 407 
 408         this.name = name;
 409 
 410         Thread parent = currentThread();
 411         SecurityManager security = System.getSecurityManager();
 412         if (g == null) {
 413             /* Determine if it's an applet or not */
 414 
 415             /* If there is a security manager, ask the security manager
 416                what to do. */
 417             if (security != null) {
 418                 g = security.getThreadGroup();
 419             }
 420 
 421             /* If the security doesn't have a strong opinion of the matter
 422                use the parent thread group. */
 423             if (g == null) {
 424                 g = parent.getThreadGroup();
 425             }
 426         }
 427 
 428         /* checkAccess regardless of whether or not threadgroup is
 429            explicitly passed in. */
 430         g.checkAccess();
 431 
 432         /*
 433          * Do we have the required permissions?
 434          */
 435         if (security != null) {
 436             if (isCCLOverridden(getClass())) {
 437                 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
 438             }
 439         }
 440 
 441         g.addUnstarted();
 442 
 443         this.group = g;
 444         this.daemon = parent.isDaemon();
 445         this.priority = parent.getPriority();
 446         if (security == null || isCCLOverridden(parent.getClass()))
 447             this.contextClassLoader = parent.getContextClassLoader();
 448         else
 449             this.contextClassLoader = parent.contextClassLoader;
 450         this.inheritedAccessControlContext =
 451                 acc != null ? acc : AccessController.getContext();
 452         this.target = target;
 453         setPriority(priority);
 454         if (inheritThreadLocals && parent.inheritableThreadLocals != null)
 455             this.inheritableThreadLocals =
 456                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
 457         /* Stash the specified stack size in case the VM cares */
 458         this.stackSize = stackSize;
 459 
 460         /* Set thread ID */
 461         tid = nextThreadID();
 462     }
 463 
 464     /**
 465      * Throws CloneNotSupportedException as a Thread can not be meaningfully
 466      * cloned. Construct a new Thread instead.
 467      *
 468      * @throws  CloneNotSupportedException
 469      *          always
 470      */
 471     @Override
 472     protected Object clone() throws CloneNotSupportedException {
 473         throw new CloneNotSupportedException();
 474     }
 475 
 476     /**
 477      * Allocates a new {@code Thread} object. This constructor has the same
 478      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 479      * {@code (null, null, gname)}, where {@code gname} is a newly generated
 480      * name. Automatically generated names are of the form
 481      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 482      */
 483     public Thread() {
 484         init(null, null, "Thread-" + nextThreadNum(), 0);
 485     }
 486 
 487     /**
 488      * Allocates a new {@code Thread} object. This constructor has the same
 489      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 490      * {@code (null, target, gname)}, where {@code gname} is a newly generated
 491      * name. Automatically generated names are of the form
 492      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 493      *
 494      * @param  target
 495      *         the object whose {@code run} method is invoked when this thread
 496      *         is started. If {@code null}, this classes {@code run} method does
 497      *         nothing.
 498      */
 499     public Thread(Runnable target) {
 500         init(null, target, "Thread-" + nextThreadNum(), 0);
 501     }
 502 
 503     /**
 504      * Creates a new Thread that inherits the given AccessControlContext
 505      * but thread-local variables are not inherited.
 506      * This is not a public constructor.
 507      */
 508     Thread(Runnable target, AccessControlContext acc) {
 509         init(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
 510     }
 511 
 512     /**
 513      * Allocates a new {@code Thread} object. This constructor has the same
 514      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 515      * {@code (group, target, gname)} ,where {@code gname} is a newly generated
 516      * name. Automatically generated names are of the form
 517      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 518      *
 519      * @param  group
 520      *         the thread group. If {@code null} and there is a security
 521      *         manager, the group is determined by {@linkplain
 522      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 523      *         If there is not a security manager or {@code
 524      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 525      *         is set to the current thread's thread group.
 526      *
 527      * @param  target
 528      *         the object whose {@code run} method is invoked when this thread
 529      *         is started. If {@code null}, this thread's run method is invoked.
 530      *
 531      * @throws  SecurityException
 532      *          if the current thread cannot create a thread in the specified
 533      *          thread group
 534      */
 535     public Thread(ThreadGroup group, Runnable target) {
 536         init(group, target, "Thread-" + nextThreadNum(), 0);
 537     }
 538 
 539     /**
 540      * Allocates a new {@code Thread} object. This constructor has the same
 541      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 542      * {@code (null, null, name)}.
 543      *
 544      * @param   name
 545      *          the name of the new thread
 546      */
 547     public Thread(String name) {
 548         init(null, null, name, 0);
 549     }
 550 
 551     /**
 552      * Allocates a new {@code Thread} object. This constructor has the same
 553      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 554      * {@code (group, null, name)}.
 555      *
 556      * @param  group
 557      *         the thread group. If {@code null} and there is a security
 558      *         manager, the group is determined by {@linkplain
 559      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 560      *         If there is not a security manager or {@code
 561      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 562      *         is set to the current thread's thread group.
 563      *
 564      * @param  name
 565      *         the name of the new thread
 566      *
 567      * @throws  SecurityException
 568      *          if the current thread cannot create a thread in the specified
 569      *          thread group
 570      */
 571     public Thread(ThreadGroup group, String name) {
 572         init(group, null, name, 0);
 573     }
 574 
 575     /**
 576      * Allocates a new {@code Thread} object. This constructor has the same
 577      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 578      * {@code (null, target, name)}.
 579      *
 580      * @param  target
 581      *         the object whose {@code run} method is invoked when this thread
 582      *         is started. If {@code null}, this thread's run method is invoked.
 583      *
 584      * @param  name
 585      *         the name of the new thread
 586      */
 587     public Thread(Runnable target, String name) {
 588         init(null, target, name, 0);
 589     }
 590 
 591     /**
 592      * Allocates a new {@code Thread} object so that it has {@code target}
 593      * as its run object, has the specified {@code name} as its name,
 594      * and belongs to the thread group referred to by {@code group}.
 595      *
 596      * <p>If there is a security manager, its
 597      * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess}
 598      * method is invoked with the ThreadGroup as its argument.
 599      *
 600      * <p>In addition, its {@code checkPermission} method is invoked with
 601      * the {@code RuntimePermission("enableContextClassLoaderOverride")}
 602      * permission when invoked directly or indirectly by the constructor
 603      * of a subclass which overrides the {@code getContextClassLoader}
 604      * or {@code setContextClassLoader} methods.
 605      *
 606      * <p>The priority of the newly created thread is set equal to the
 607      * priority of the thread creating it, that is, the currently running
 608      * thread. The method {@linkplain #setPriority setPriority} may be


 616      * @param  group
 617      *         the thread group. If {@code null} and there is a security
 618      *         manager, the group is determined by {@linkplain
 619      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 620      *         If there is not a security manager or {@code
 621      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 622      *         is set to the current thread's thread group.
 623      *
 624      * @param  target
 625      *         the object whose {@code run} method is invoked when this thread
 626      *         is started. If {@code null}, this thread's run method is invoked.
 627      *
 628      * @param  name
 629      *         the name of the new thread
 630      *
 631      * @throws  SecurityException
 632      *          if the current thread cannot create a thread in the specified
 633      *          thread group or cannot override the context class loader methods.
 634      */
 635     public Thread(ThreadGroup group, Runnable target, String name) {
 636         init(group, target, name, 0);
 637     }
 638 
 639     /**
 640      * Allocates a new {@code Thread} object so that it has {@code target}
 641      * as its run object, has the specified {@code name} as its name,
 642      * and belongs to the thread group referred to by {@code group}, and has
 643      * the specified <i>stack size</i>.
 644      *
 645      * <p>This constructor is identical to {@link
 646      * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact
 647      * that it allows the thread stack size to be specified.  The stack size
 648      * is the approximate number of bytes of address space that the virtual
 649      * machine is to allocate for this thread's stack.  <b>The effect of the
 650      * {@code stackSize} parameter, if any, is highly platform dependent.</b>
 651      *
 652      * <p>On some platforms, specifying a higher value for the
 653      * {@code stackSize} parameter may allow a thread to achieve greater
 654      * recursion depth before throwing a {@link StackOverflowError}.
 655      * Similarly, specifying a lower value may allow a greater number of
 656      * threads to exist concurrently without throwing an {@link


 695      *
 696      * @param  target
 697      *         the object whose {@code run} method is invoked when this thread
 698      *         is started. If {@code null}, this thread's run method is invoked.
 699      *
 700      * @param  name
 701      *         the name of the new thread
 702      *
 703      * @param  stackSize
 704      *         the desired stack size for the new thread, or zero to indicate
 705      *         that this parameter is to be ignored.
 706      *
 707      * @throws  SecurityException
 708      *          if the current thread cannot create a thread in the specified
 709      *          thread group
 710      *
 711      * @since 1.4
 712      */
 713     public Thread(ThreadGroup group, Runnable target, String name,
 714                   long stackSize) {
 715         init(group, target, name, stackSize);
 716     }
 717 
 718     /**
 719      * Allocates a new {@code Thread} object so that it has {@code target}
 720      * as its run object, has the specified {@code name} as its name,
 721      * belongs to the thread group referred to by {@code group}, has
 722      * the specified {@code stackSize}, and inherits initial values for
 723      * {@linkplain InheritableThreadLocal inheritable thread-local} variables
 724      * if {@code inheritThreadLocals} is {@code true}.
 725      *
 726      * <p> This constructor is identical to {@link
 727      * #Thread(ThreadGroup,Runnable,String,long)} with the added ability to
 728      * suppress, or not, the inheriting of initial values for inheritable
 729      * thread-local variables from the constructing thread. This allows for
 730      * finer grain control over inheritable thread-locals. Care must be taken
 731      * when passing a value of {@code false} for {@code inheritThreadLocals},
 732      * as it may lead to unexpected behavior if the new thread executes code
 733      * that expects a specific thread-local value to be inherited.
 734      *
 735      * <p> Specifying a value of {@code true} for the {@code inheritThreadLocals}


 751      * @param  name
 752      *         the name of the new thread
 753      *
 754      * @param  stackSize
 755      *         the desired stack size for the new thread, or zero to indicate
 756      *         that this parameter is to be ignored
 757      *
 758      * @param  inheritThreadLocals
 759      *         if {@code true}, inherit initial values for inheritable
 760      *         thread-locals from the constructing thread, otherwise no initial
 761      *         values are inherited
 762      *
 763      * @throws  SecurityException
 764      *          if the current thread cannot create a thread in the specified
 765      *          thread group
 766      *
 767      * @since 9
 768      */
 769     public Thread(ThreadGroup group, Runnable target, String name,
 770                   long stackSize, boolean inheritThreadLocals) {
 771         init(group, target, name, stackSize, null, inheritThreadLocals);
 772     }
 773 
 774     /**
 775      * Causes this thread to begin execution; the Java Virtual Machine
 776      * calls the {@code run} method of this thread.
 777      * <p>
 778      * The result is that two threads are running concurrently: the
 779      * current thread (which returns from the call to the
 780      * {@code start} method) and the other thread (which executes its
 781      * {@code run} method).
 782      * <p>
 783      * It is never legal to start a thread more than once.
 784      * In particular, a thread may not be restarted once it has completed
 785      * execution.
 786      *
 787      * @throws     IllegalThreadStateException  if the thread was already started.
 788      * @see        #run()
 789      * @see        #stop()
 790      */
 791     public synchronized void start() {




 171     private static int threadInitNumber;
 172     private static synchronized int nextThreadNum() {
 173         return threadInitNumber++;
 174     }
 175 
 176     /* ThreadLocal values pertaining to this thread. This map is maintained
 177      * by the ThreadLocal class. */
 178     ThreadLocal.ThreadLocalMap threadLocals = null;
 179 
 180     /*
 181      * InheritableThreadLocal values pertaining to this thread. This map is
 182      * maintained by the InheritableThreadLocal class.
 183      */
 184     ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
 185 
 186     /*
 187      * The requested stack size for this thread, or 0 if the creator did
 188      * not specify a stack size.  It is up to the VM to do whatever it
 189      * likes with this number; some VMs will ignore it.
 190      */
 191     private final long stackSize;
 192 
 193     /*
 194      * JVM-private state that persists after native thread termination.
 195      */
 196     private long nativeParkEventPointer;
 197 
 198     /*
 199      * Thread ID
 200      */
 201     private final long tid;
 202 
 203     /* For generating thread ID */
 204     private static long threadSeqNumber;
 205 
 206     private static synchronized long nextThreadID() {
 207         return ++threadSeqNumber;
 208     }
 209 
 210     /*
 211      * Java thread status for tools, default indicates thread 'not yet started'
 212      */
 213     private volatile int threadStatus;
 214 




 215     /**
 216      * The argument supplied to the current call to
 217      * java.util.concurrent.locks.LockSupport.park.
 218      * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
 219      * Accessed using java.util.concurrent.locks.LockSupport.getBlocker
 220      */
 221     volatile Object parkBlocker;
 222 
 223     /* The object in which this thread is blocked in an interruptible I/O
 224      * operation, if any.  The blocker's interrupt method should be invoked
 225      * after setting this thread's interrupt status.
 226      */
 227     private volatile Interruptible blocker;
 228     private final Object blockerLock = new Object();
 229 
 230     /* Set the blocker field; invoked via jdk.internal.misc.SharedSecrets
 231      * from java.nio code
 232      */
 233     static void blockedOn(Interruptible b) {
 234         Thread me = Thread.currentThread();


 360      *         }
 361      *
 362      *         void readAndProcessEvent() {
 363      *             // Read event from some source and process it
 364      *              . . .
 365      *         }
 366      *     }
 367      * }</pre>
 368      * <p>
 369      * The code above would remain correct even if the {@code onSpinWait}
 370      * method was not called at all. However on some architectures the Java
 371      * Virtual Machine may issue the processor instructions to address such
 372      * code patterns in a more beneficial way.
 373      *
 374      * @since 9
 375      */
 376     @HotSpotIntrinsicCandidate
 377     public static void onSpinWait() {}
 378 
 379     /**









 380      * Initializes a Thread.
 381      *
 382      * @param g the Thread group
 383      * @param target the object whose run() method gets called
 384      * @param name the name of the new Thread
 385      * @param stackSize the desired stack size for the new thread, or
 386      *        zero to indicate that this parameter is to be ignored.
 387      * @param acc the AccessControlContext to inherit, or
 388      *            AccessController.getContext() if null
 389      * @param inheritThreadLocals if {@code true}, inherit initial values for
 390      *            inheritable thread-locals from the constructing thread
 391      */
 392     private Thread(ThreadGroup g, Runnable target, String name,
 393                    long stackSize, AccessControlContext acc,
 394                    boolean inheritThreadLocals) {
 395         if (name == null) {
 396             throw new NullPointerException("name cannot be null");
 397         }
 398 
 399         this.name = name;
 400 
 401         Thread parent = currentThread();
 402         SecurityManager security = System.getSecurityManager();
 403         if (g == null) {
 404             /* Determine if it's an applet or not */
 405 
 406             /* If there is a security manager, ask the security manager
 407                what to do. */
 408             if (security != null) {
 409                 g = security.getThreadGroup();
 410             }
 411 
 412             /* If the security manager doesn't have a strong opinion
 413                on the matter, use the parent thread group. */
 414             if (g == null) {
 415                 g = parent.getThreadGroup();
 416             }
 417         }
 418 
 419         /* checkAccess regardless of whether or not threadgroup is
 420            explicitly passed in. */
 421         g.checkAccess();
 422 
 423         /*
 424          * Do we have the required permissions?
 425          */
 426         if (security != null) {
 427             if (isCCLOverridden(getClass())) {
 428                 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
 429             }
 430         }
 431 
 432         g.addUnstarted();
 433 
 434         this.group = g;
 435         this.daemon = parent.isDaemon();
 436         this.priority = parent.getPriority();
 437         if (security == null || isCCLOverridden(parent.getClass()))
 438             this.contextClassLoader = parent.getContextClassLoader();
 439         else
 440             this.contextClassLoader = parent.contextClassLoader;
 441         this.inheritedAccessControlContext =
 442                 acc != null ? acc : AccessController.getContext();
 443         this.target = target;
 444         setPriority(priority);
 445         if (inheritThreadLocals && parent.inheritableThreadLocals != null)
 446             this.inheritableThreadLocals =
 447                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
 448         /* Stash the specified stack size in case the VM cares */
 449         this.stackSize = stackSize;
 450 
 451         /* Set thread ID */
 452         this.tid = nextThreadID();
 453     }
 454 
 455     /**
 456      * Throws CloneNotSupportedException as a Thread can not be meaningfully
 457      * cloned. Construct a new Thread instead.
 458      *
 459      * @throws  CloneNotSupportedException
 460      *          always
 461      */
 462     @Override
 463     protected Object clone() throws CloneNotSupportedException {
 464         throw new CloneNotSupportedException();
 465     }
 466 
 467     /**
 468      * Allocates a new {@code Thread} object. This constructor has the same
 469      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 470      * {@code (null, null, gname)}, where {@code gname} is a newly generated
 471      * name. Automatically generated names are of the form
 472      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 473      */
 474     public Thread() {
 475         this(null, null, "Thread-" + nextThreadNum(), 0);
 476     }
 477 
 478     /**
 479      * Allocates a new {@code Thread} object. This constructor has the same
 480      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 481      * {@code (null, target, gname)}, where {@code gname} is a newly generated
 482      * name. Automatically generated names are of the form
 483      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 484      *
 485      * @param  target
 486      *         the object whose {@code run} method is invoked when this thread
 487      *         is started. If {@code null}, this classes {@code run} method does
 488      *         nothing.
 489      */
 490     public Thread(Runnable target) {
 491         this(null, target, "Thread-" + nextThreadNum(), 0);
 492     }
 493 
 494     /**
 495      * Creates a new Thread that inherits the given AccessControlContext
 496      * but thread-local variables are not inherited.
 497      * This is not a public constructor.
 498      */
 499     Thread(Runnable target, AccessControlContext acc) {
 500         this(null, target, "Thread-" + nextThreadNum(), 0, acc, false);
 501     }
 502 
 503     /**
 504      * Allocates a new {@code Thread} object. This constructor has the same
 505      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 506      * {@code (group, target, gname)} ,where {@code gname} is a newly generated
 507      * name. Automatically generated names are of the form
 508      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 509      *
 510      * @param  group
 511      *         the thread group. If {@code null} and there is a security
 512      *         manager, the group is determined by {@linkplain
 513      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 514      *         If there is not a security manager or {@code
 515      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 516      *         is set to the current thread's thread group.
 517      *
 518      * @param  target
 519      *         the object whose {@code run} method is invoked when this thread
 520      *         is started. If {@code null}, this thread's run method is invoked.
 521      *
 522      * @throws  SecurityException
 523      *          if the current thread cannot create a thread in the specified
 524      *          thread group
 525      */
 526     public Thread(ThreadGroup group, Runnable target) {
 527         this(group, target, "Thread-" + nextThreadNum(), 0);
 528     }
 529 
 530     /**
 531      * Allocates a new {@code Thread} object. This constructor has the same
 532      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 533      * {@code (null, null, name)}.
 534      *
 535      * @param   name
 536      *          the name of the new thread
 537      */
 538     public Thread(String name) {
 539         this(null, null, name, 0);
 540     }
 541 
 542     /**
 543      * Allocates a new {@code Thread} object. This constructor has the same
 544      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 545      * {@code (group, null, name)}.
 546      *
 547      * @param  group
 548      *         the thread group. If {@code null} and there is a security
 549      *         manager, the group is determined by {@linkplain
 550      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 551      *         If there is not a security manager or {@code
 552      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 553      *         is set to the current thread's thread group.
 554      *
 555      * @param  name
 556      *         the name of the new thread
 557      *
 558      * @throws  SecurityException
 559      *          if the current thread cannot create a thread in the specified
 560      *          thread group
 561      */
 562     public Thread(ThreadGroup group, String name) {
 563         this(group, null, name, 0);
 564     }
 565 
 566     /**
 567      * Allocates a new {@code Thread} object. This constructor has the same
 568      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 569      * {@code (null, target, name)}.
 570      *
 571      * @param  target
 572      *         the object whose {@code run} method is invoked when this thread
 573      *         is started. If {@code null}, this thread's run method is invoked.
 574      *
 575      * @param  name
 576      *         the name of the new thread
 577      */
 578     public Thread(Runnable target, String name) {
 579         this(null, target, name, 0);
 580     }
 581 
 582     /**
 583      * Allocates a new {@code Thread} object so that it has {@code target}
 584      * as its run object, has the specified {@code name} as its name,
 585      * and belongs to the thread group referred to by {@code group}.
 586      *
 587      * <p>If there is a security manager, its
 588      * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess}
 589      * method is invoked with the ThreadGroup as its argument.
 590      *
 591      * <p>In addition, its {@code checkPermission} method is invoked with
 592      * the {@code RuntimePermission("enableContextClassLoaderOverride")}
 593      * permission when invoked directly or indirectly by the constructor
 594      * of a subclass which overrides the {@code getContextClassLoader}
 595      * or {@code setContextClassLoader} methods.
 596      *
 597      * <p>The priority of the newly created thread is set equal to the
 598      * priority of the thread creating it, that is, the currently running
 599      * thread. The method {@linkplain #setPriority setPriority} may be


 607      * @param  group
 608      *         the thread group. If {@code null} and there is a security
 609      *         manager, the group is determined by {@linkplain
 610      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 611      *         If there is not a security manager or {@code
 612      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 613      *         is set to the current thread's thread group.
 614      *
 615      * @param  target
 616      *         the object whose {@code run} method is invoked when this thread
 617      *         is started. If {@code null}, this thread's run method is invoked.
 618      *
 619      * @param  name
 620      *         the name of the new thread
 621      *
 622      * @throws  SecurityException
 623      *          if the current thread cannot create a thread in the specified
 624      *          thread group or cannot override the context class loader methods.
 625      */
 626     public Thread(ThreadGroup group, Runnable target, String name) {
 627         this(group, target, name, 0);
 628     }
 629 
 630     /**
 631      * Allocates a new {@code Thread} object so that it has {@code target}
 632      * as its run object, has the specified {@code name} as its name,
 633      * and belongs to the thread group referred to by {@code group}, and has
 634      * the specified <i>stack size</i>.
 635      *
 636      * <p>This constructor is identical to {@link
 637      * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact
 638      * that it allows the thread stack size to be specified.  The stack size
 639      * is the approximate number of bytes of address space that the virtual
 640      * machine is to allocate for this thread's stack.  <b>The effect of the
 641      * {@code stackSize} parameter, if any, is highly platform dependent.</b>
 642      *
 643      * <p>On some platforms, specifying a higher value for the
 644      * {@code stackSize} parameter may allow a thread to achieve greater
 645      * recursion depth before throwing a {@link StackOverflowError}.
 646      * Similarly, specifying a lower value may allow a greater number of
 647      * threads to exist concurrently without throwing an {@link


 686      *
 687      * @param  target
 688      *         the object whose {@code run} method is invoked when this thread
 689      *         is started. If {@code null}, this thread's run method is invoked.
 690      *
 691      * @param  name
 692      *         the name of the new thread
 693      *
 694      * @param  stackSize
 695      *         the desired stack size for the new thread, or zero to indicate
 696      *         that this parameter is to be ignored.
 697      *
 698      * @throws  SecurityException
 699      *          if the current thread cannot create a thread in the specified
 700      *          thread group
 701      *
 702      * @since 1.4
 703      */
 704     public Thread(ThreadGroup group, Runnable target, String name,
 705                   long stackSize) {
 706         this(group, target, name, stackSize, null, true);
 707     }
 708 
 709     /**
 710      * Allocates a new {@code Thread} object so that it has {@code target}
 711      * as its run object, has the specified {@code name} as its name,
 712      * belongs to the thread group referred to by {@code group}, has
 713      * the specified {@code stackSize}, and inherits initial values for
 714      * {@linkplain InheritableThreadLocal inheritable thread-local} variables
 715      * if {@code inheritThreadLocals} is {@code true}.
 716      *
 717      * <p> This constructor is identical to {@link
 718      * #Thread(ThreadGroup,Runnable,String,long)} with the added ability to
 719      * suppress, or not, the inheriting of initial values for inheritable
 720      * thread-local variables from the constructing thread. This allows for
 721      * finer grain control over inheritable thread-locals. Care must be taken
 722      * when passing a value of {@code false} for {@code inheritThreadLocals},
 723      * as it may lead to unexpected behavior if the new thread executes code
 724      * that expects a specific thread-local value to be inherited.
 725      *
 726      * <p> Specifying a value of {@code true} for the {@code inheritThreadLocals}


 742      * @param  name
 743      *         the name of the new thread
 744      *
 745      * @param  stackSize
 746      *         the desired stack size for the new thread, or zero to indicate
 747      *         that this parameter is to be ignored
 748      *
 749      * @param  inheritThreadLocals
 750      *         if {@code true}, inherit initial values for inheritable
 751      *         thread-locals from the constructing thread, otherwise no initial
 752      *         values are inherited
 753      *
 754      * @throws  SecurityException
 755      *          if the current thread cannot create a thread in the specified
 756      *          thread group
 757      *
 758      * @since 9
 759      */
 760     public Thread(ThreadGroup group, Runnable target, String name,
 761                   long stackSize, boolean inheritThreadLocals) {
 762         this(group, target, name, stackSize, null, inheritThreadLocals);
 763     }
 764 
 765     /**
 766      * Causes this thread to begin execution; the Java Virtual Machine
 767      * calls the {@code run} method of this thread.
 768      * <p>
 769      * The result is that two threads are running concurrently: the
 770      * current thread (which returns from the call to the
 771      * {@code start} method) and the other thread (which executes its
 772      * {@code run} method).
 773      * <p>
 774      * It is never legal to start a thread more than once.
 775      * In particular, a thread may not be restarted once it has completed
 776      * execution.
 777      *
 778      * @throws     IllegalThreadStateException  if the thread was already started.
 779      * @see        #run()
 780      * @see        #stop()
 781      */
 782     public synchronized void start() {


< prev index next >