The two ways of creating Thread
2010-03-03 11:50
316 查看
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of
The following code would then create a thread and start it running:
The other way to create a thread is to declare a class that implements the
The following code would then create a thread and start it running:
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.
Thread. This subclass should override the
runmethod 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();
The other way to create a thread is to declare a class that implements the
Runnableinterface. That class then implements the
runmethod. An instance of the class can then be allocated, passed as an argument when creating
Thread, 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 . . . } }
The following code would then create a thread and start it running:
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.
相关文章推荐
- two ways of multiThread
- Two ways to create a new thread of execution
- The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make s
- Thread-Safety with the Semaphores Class(Synchronization of .net)
- two ways to hold the states of a job in ironic
- projecteuler网站第四题解法:Find the largest palindrome made from the product of two 3-digit numbers
- The Power of Two Random Choices - Summary
- error C2665:'AfxBeginThread':none of the 2 overloads can convert parameter 1 from type 'unsigned int
- The 2 ways of building for Windows
- Building Two Versions of the Same Android App
- (转)Building Two Versions of the Same Android App
- geeksforgeeks Find the maximum path sum between two leaves of a binary tree
- Two Sides of the Same Coin --二分图的最大匹配
- Find the top k sums of two sorted arrays
- Problem 10 of Find the sum of all the primes below two million.
- URAL - 1721 Two Sides of the Same Coin
- Two ways to terminate blocking socket thread gracefully
- Tests of the Equality of Two Means
- MapReduce and MPP: Two sides of the Big Data coin?
- Better Ways to Deal With the Tail Slag Two