Difference between revisions of "Good basic Thread notes from Oracle"
From MyWiki
(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...") |
|||
Line 15: | Line 15: | ||
</source> | </source> | ||
− | + | The following code would then create a thread and start it running: | |
<source lang="java"> | <source lang="java"> | ||
− | + | ||
PrimeThread p = new PrimeThread(143); | PrimeThread p = new PrimeThread(143); | ||
p.start(); | p.start(); | ||
</source> | </source> |
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 . . . } }
The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143); p.start();