Concurrency: Thread类及Runnable接口的

2019-05-22  本文已影响0人  CalmHeart

Every thread has a priority. Threads with higher priority are
executed in preference to threads with lower priority. Each thread
may or may not also be marked as a daemon. When code running in
some thread creates a new <code>Thread</code> object, the new
thread has its priority initially set equal to the priority of the
creating thread, and is a daemon thread if and only if the
creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single
non-daemon thread (which typically calls the method named
<code>main</code> of some designated class). The Java Virtual
Machine continues to execute threads until either of the following
occurs:

     class PrimeThread extends Thread {
           long minPrime;
           PrimeThread(long minPrime) {
           this.minPrime = minPrime;
          }
 
          public void run() {
              // compute primes larger than minPrime
              &nbsp;.&nbsp;.&nbsp;.
          }
      }
      PrimeThread p = new PrimeThread(143);
      p.start();

The other way to create a thread is to declare a class that
implements the <code>Runnable</code> interface. That class then
implements the <code>run</code> method. An instance of the class can
then be allocated, passed as an argument when creating
<code>Thread</code>, and started. The same example in this other
style looks like the following:

   class PrimeRun implements Runnable {
         long minPrime;
         PrimeRun(long minPrime) {
             this.minPrime = minPrime;
         }

        public void run() {
            // compute primes larger than minPrime
            &nbsp;.&nbsp;.&nbsp;.
      }
    }
    PrimeRun p = new PrimeRun(143);
    new Thread(p).start();

Every thread has a name for identification purposes. More than
one thread may have the same name. If a name is not specified when
a thread is created, a new name is generated for it.


Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.


@FunctionalInterface 显示指定为函数式接口
public interface Runnable

In addition, <code>Runnable</code> provides the means for a class to be active while not subclassing <code>Thread</code>. A class that implements <code>Runnable</code> can run without subclassing <code>Thread</code> by instantiating a <code>Thread</code> instance and passing itself in as the target.


When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
The general contract of the method run is that it may take any action whatsoever.

上一篇下一篇

猜你喜欢

热点阅读