Difference between revisions of "Good basic Thread notes from Oracle"

From MyWiki
Jump to: navigation, search
(Created page with "There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An...")
(No difference)

Revision as of 23:49, 11 July 2015

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:

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