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

Print this page




 327     public static void sleep(long millis, int nanos)
 328     throws InterruptedException {
 329         if (millis < 0) {
 330             throw new IllegalArgumentException("timeout value is negative");
 331         }
 332 
 333         if (nanos < 0 || nanos > 999999) {
 334             throw new IllegalArgumentException(
 335                                 "nanosecond timeout value out of range");
 336         }
 337 
 338         if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
 339             millis++;
 340         }
 341 
 342         sleep(millis);
 343     }
 344 
 345     /**
 346      * Initializes a Thread with the current AccessControlContext.
 347      * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext)
 348      */
 349     private void init(ThreadGroup g, Runnable target, String name,
 350                       long stackSize) {
 351         init(g, target, name, stackSize, null);
 352     }
 353 
 354     /**
 355      * Initializes a Thread.
 356      *
 357      * @param g the Thread group
 358      * @param target the object whose run() method gets called
 359      * @param name the name of the new Thread
 360      * @param stackSize the desired stack size for the new thread, or
 361      *        zero to indicate that this parameter is to be ignored.
 362      * @param acc the AccessControlContext to inherit, or
 363      *            AccessController.getContext() if null


 364      */
 365     private void init(ThreadGroup g, Runnable target, String name,
 366                       long stackSize, AccessControlContext acc) {

 367         if (name == null) {
 368             throw new NullPointerException("name cannot be null");
 369         }
 370 
 371         this.name = name;
 372 
 373         Thread parent = currentThread();
 374         SecurityManager security = System.getSecurityManager();
 375         if (g == null) {
 376             /* Determine if it's an applet or not */
 377 
 378             /* If there is a security manager, ask the security manager
 379                what to do. */
 380             if (security != null) {
 381                 g = security.getThreadGroup();
 382             }
 383 
 384             /* If the security doesn't have a strong opinion of the matter
 385                use the parent thread group. */
 386             if (g == null) {


 397          */
 398         if (security != null) {
 399             if (isCCLOverridden(getClass())) {
 400                 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
 401             }
 402         }
 403 
 404         g.addUnstarted();
 405 
 406         this.group = g;
 407         this.daemon = parent.isDaemon();
 408         this.priority = parent.getPriority();
 409         if (security == null || isCCLOverridden(parent.getClass()))
 410             this.contextClassLoader = parent.getContextClassLoader();
 411         else
 412             this.contextClassLoader = parent.contextClassLoader;
 413         this.inheritedAccessControlContext =
 414                 acc != null ? acc : AccessController.getContext();
 415         this.target = target;
 416         setPriority(priority);
 417         if (parent.inheritableThreadLocals != null)
 418             this.inheritableThreadLocals =
 419                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
 420         /* Stash the specified stack size in case the VM cares */
 421         this.stackSize = stackSize;
 422 
 423         /* Set thread ID */
 424         tid = nextThreadID();
 425     }
 426 
 427     /**
 428      * Throws CloneNotSupportedException as a Thread can not be meaningfully
 429      * cloned. Construct a new Thread instead.
 430      *
 431      * @throws  CloneNotSupportedException
 432      *          always
 433      */
 434     @Override
 435     protected Object clone() throws CloneNotSupportedException {
 436         throw new CloneNotSupportedException();
 437     }


 451      * Allocates a new {@code Thread} object. This constructor has the same
 452      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 453      * {@code (null, target, gname)}, where {@code gname} is a newly generated
 454      * name. Automatically generated names are of the form
 455      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 456      *
 457      * @param  target
 458      *         the object whose {@code run} method is invoked when this thread
 459      *         is started. If {@code null}, this classes {@code run} method does
 460      *         nothing.
 461      */
 462     public Thread(Runnable target) {
 463         init(null, target, "Thread-" + nextThreadNum(), 0);
 464     }
 465 
 466     /**
 467      * Creates a new Thread that inherits the given AccessControlContext.
 468      * This is not a public constructor.
 469      */
 470     Thread(Runnable target, AccessControlContext acc) {
 471         init(null, target, "Thread-" + nextThreadNum(), 0, acc);
 472     }
 473 
 474     /**
 475      * Allocates a new {@code Thread} object. This constructor has the same
 476      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 477      * {@code (group, target, gname)} ,where {@code gname} is a newly generated
 478      * name. Automatically generated names are of the form
 479      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 480      *
 481      * @param  group
 482      *         the thread group. If {@code null} and there is a security
 483      *         manager, the group is determined by {@linkplain
 484      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 485      *         If there is not a security manager or {@code
 486      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 487      *         is set to the current thread's thread group.
 488      *
 489      * @param  target
 490      *         the object whose {@code run} method is invoked when this thread
 491      *         is started. If {@code null}, this thread's run method is invoked.


 661      *
 662      * @param  name
 663      *         the name of the new thread
 664      *
 665      * @param  stackSize
 666      *         the desired stack size for the new thread, or zero to indicate
 667      *         that this parameter is to be ignored.
 668      *
 669      * @throws  SecurityException
 670      *          if the current thread cannot create a thread in the specified
 671      *          thread group
 672      *
 673      * @since 1.4
 674      */
 675     public Thread(ThreadGroup group, Runnable target, String name,
 676                   long stackSize) {
 677         init(group, target, name, stackSize);
 678     }
 679 
 680     /**
























































 681      * Causes this thread to begin execution; the Java Virtual Machine
 682      * calls the <code>run</code> method of this thread.
 683      * <p>
 684      * The result is that two threads are running concurrently: the
 685      * current thread (which returns from the call to the
 686      * <code>start</code> method) and the other thread (which executes its
 687      * <code>run</code> method).
 688      * <p>
 689      * It is never legal to start a thread more than once.
 690      * In particular, a thread may not be restarted once it has completed
 691      * execution.
 692      *
 693      * @exception  IllegalThreadStateException  if the thread was already
 694      *               started.
 695      * @see        #run()
 696      * @see        #stop()
 697      */
 698     public synchronized void start() {
 699         /**
 700          * This method is not invoked for the main method thread or "system"




 327     public static void sleep(long millis, int nanos)
 328     throws InterruptedException {
 329         if (millis < 0) {
 330             throw new IllegalArgumentException("timeout value is negative");
 331         }
 332 
 333         if (nanos < 0 || nanos > 999999) {
 334             throw new IllegalArgumentException(
 335                                 "nanosecond timeout value out of range");
 336         }
 337 
 338         if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
 339             millis++;
 340         }
 341 
 342         sleep(millis);
 343     }
 344 
 345     /**
 346      * Initializes a Thread with the current AccessControlContext.
 347      * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean)
 348      */
 349     private void init(ThreadGroup g, Runnable target, String name,
 350                       long stackSize) {
 351         init(g, target, name, stackSize, null, true);
 352     }
 353 
 354     /**
 355      * Initializes a Thread.
 356      *
 357      * @param g the Thread group
 358      * @param target the object whose run() method gets called
 359      * @param name the name of the new Thread
 360      * @param stackSize the desired stack size for the new thread, or
 361      *        zero to indicate that this parameter is to be ignored.
 362      * @param acc the AccessControlContext to inherit, or
 363      *            AccessController.getContext() if null
 364      * @param inheritThreadLocals if {@code true}, inherit initial values for
 365      *            inheritable thread-locals from the constructing thread
 366      */
 367     private void init(ThreadGroup g, Runnable target, String name,
 368                       long stackSize, AccessControlContext acc,
 369                       boolean inheritThreadLocals) {
 370         if (name == null) {
 371             throw new NullPointerException("name cannot be null");
 372         }
 373 
 374         this.name = name;
 375 
 376         Thread parent = currentThread();
 377         SecurityManager security = System.getSecurityManager();
 378         if (g == null) {
 379             /* Determine if it's an applet or not */
 380 
 381             /* If there is a security manager, ask the security manager
 382                what to do. */
 383             if (security != null) {
 384                 g = security.getThreadGroup();
 385             }
 386 
 387             /* If the security doesn't have a strong opinion of the matter
 388                use the parent thread group. */
 389             if (g == null) {


 400          */
 401         if (security != null) {
 402             if (isCCLOverridden(getClass())) {
 403                 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
 404             }
 405         }
 406 
 407         g.addUnstarted();
 408 
 409         this.group = g;
 410         this.daemon = parent.isDaemon();
 411         this.priority = parent.getPriority();
 412         if (security == null || isCCLOverridden(parent.getClass()))
 413             this.contextClassLoader = parent.getContextClassLoader();
 414         else
 415             this.contextClassLoader = parent.contextClassLoader;
 416         this.inheritedAccessControlContext =
 417                 acc != null ? acc : AccessController.getContext();
 418         this.target = target;
 419         setPriority(priority);
 420         if (inheritThreadLocals && parent.inheritableThreadLocals != null)
 421             this.inheritableThreadLocals =
 422                 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
 423         /* Stash the specified stack size in case the VM cares */
 424         this.stackSize = stackSize;
 425 
 426         /* Set thread ID */
 427         tid = nextThreadID();
 428     }
 429 
 430     /**
 431      * Throws CloneNotSupportedException as a Thread can not be meaningfully
 432      * cloned. Construct a new Thread instead.
 433      *
 434      * @throws  CloneNotSupportedException
 435      *          always
 436      */
 437     @Override
 438     protected Object clone() throws CloneNotSupportedException {
 439         throw new CloneNotSupportedException();
 440     }


 454      * Allocates a new {@code Thread} object. This constructor has the same
 455      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 456      * {@code (null, target, gname)}, where {@code gname} is a newly generated
 457      * name. Automatically generated names are of the form
 458      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 459      *
 460      * @param  target
 461      *         the object whose {@code run} method is invoked when this thread
 462      *         is started. If {@code null}, this classes {@code run} method does
 463      *         nothing.
 464      */
 465     public Thread(Runnable target) {
 466         init(null, target, "Thread-" + nextThreadNum(), 0);
 467     }
 468 
 469     /**
 470      * Creates a new Thread that inherits the given AccessControlContext.
 471      * This is not a public constructor.
 472      */
 473     Thread(Runnable target, AccessControlContext acc) {
 474         init(null, target, "Thread-" + nextThreadNum(), 0, acc, true);
 475     }
 476 
 477     /**
 478      * Allocates a new {@code Thread} object. This constructor has the same
 479      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 480      * {@code (group, target, gname)} ,where {@code gname} is a newly generated
 481      * name. Automatically generated names are of the form
 482      * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 483      *
 484      * @param  group
 485      *         the thread group. If {@code null} and there is a security
 486      *         manager, the group is determined by {@linkplain
 487      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 488      *         If there is not a security manager or {@code
 489      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 490      *         is set to the current thread's thread group.
 491      *
 492      * @param  target
 493      *         the object whose {@code run} method is invoked when this thread
 494      *         is started. If {@code null}, this thread's run method is invoked.


 664      *
 665      * @param  name
 666      *         the name of the new thread
 667      *
 668      * @param  stackSize
 669      *         the desired stack size for the new thread, or zero to indicate
 670      *         that this parameter is to be ignored.
 671      *
 672      * @throws  SecurityException
 673      *          if the current thread cannot create a thread in the specified
 674      *          thread group
 675      *
 676      * @since 1.4
 677      */
 678     public Thread(ThreadGroup group, Runnable target, String name,
 679                   long stackSize) {
 680         init(group, target, name, stackSize);
 681     }
 682 
 683     /**
 684      * Allocates a new {@code Thread} object so that it has {@code target}
 685      * as its run object, has the specified {@code name} as its name,
 686      * belongs to the thread group referred to by {@code group}, has
 687      * the specified {@code stackSize}, and inherits initial values for
 688      * {@linkplain InheritableThreadLocal inheritable thread-local} variables
 689      * if {@code inheritThreadLocals} is {@code true}.
 690      *
 691      * <p> This constructor is identical to {@link
 692      * #Thread(ThreadGroup,Runnable,String,long)} with the added ability to
 693      * suppress, or not, the inheriting of initial values for inheritable
 694      * thread-local variables from the constructing thread. This allows for
 695      * finer grain control over inheritable thread-locals. Care must be taken
 696      * when passing a value of {@code false} for {@code inheritThreadLocals},
 697      * as it may lead to unexpected behavior if the new thread executes code
 698      * that expects a specific thread-local value to be inherited.
 699      *
 700      * <p> Specifying a value of {@code true} for the {@code inheritThreadLocals}
 701      * parameter will cause this constructor to behave exactly like the
 702      * {@code Thread(ThreadGroup, Runnable, String, long)} constructor.
 703      *
 704      * @param  group
 705      *         the thread group. If {@code null} and there is a security
 706      *         manager, the group is determined by {@linkplain
 707      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
 708      *         If there is not a security manager or {@code
 709      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
 710      *         is set to the current thread's thread group.
 711      *
 712      * @param  target
 713      *         the object whose {@code run} method is invoked when this thread
 714      *         is started. If {@code null}, this thread's run method is invoked.
 715      *
 716      * @param  name
 717      *         the name of the new thread
 718      *
 719      * @param  stackSize
 720      *         the desired stack size for the new thread, or zero to indicate
 721      *         that this parameter is to be ignored
 722      *
 723      * @param  inheritThreadLocals
 724      *         if {@code true}, inherit initial values for inheritable
 725      *         thread-locals from the constructing thread, otherwise no initial
 726      *         values are inherited
 727      *
 728      * @throws  SecurityException
 729      *          if the current thread cannot create a thread in the specified
 730      *          thread group
 731      *
 732      * @since 9
 733      */
 734     public Thread(ThreadGroup group, Runnable target, String name,
 735                   long stackSize, boolean inheritThreadLocals) {
 736         init(group, target, name, stackSize, null, inheritThreadLocals);
 737     }
 738 
 739     /**
 740      * Causes this thread to begin execution; the Java Virtual Machine
 741      * calls the <code>run</code> method of this thread.
 742      * <p>
 743      * The result is that two threads are running concurrently: the
 744      * current thread (which returns from the call to the
 745      * <code>start</code> method) and the other thread (which executes its
 746      * <code>run</code> method).
 747      * <p>
 748      * It is never legal to start a thread more than once.
 749      * In particular, a thread may not be restarted once it has completed
 750      * execution.
 751      *
 752      * @exception  IllegalThreadStateException  if the thread was already
 753      *               started.
 754      * @see        #run()
 755      * @see        #stop()
 756      */
 757     public synchronized void start() {
 758         /**
 759          * This method is not invoked for the main method thread or "system"