java.lang.Thread(1)
Create Thread
java线程的两种实现方式
·继承Thread类
·实现Runnable接口
1、继承Thread类
定义语法:
class classname extends Thread{
属性;
方法;
//覆写Thread类中run()
public void run(){
}
}
例如:
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
启动线程:
PrimeThread p = new PrimeThread(143);
p.start();
详细示例1:
View Code
1 //用继承Thread创建线程
2 public class CreateThreadExtendsThread02 {
3 public static void main(String[] args) {
4 MyThread myA = new MyThread("线程A");
5 MyThread myB = new MyThread("线程B");
6 MyThread myC = new MyThread("线程C");
7 myA.start();
8 myB.start();
9 myC.start();
10 }
11 }
12
13 class MyThread extends Thread{
14 private String name;
15 public MyThread(String name){
16 this.name = name;
17 }
18 public void run(){
19 for(int i=1;i<=20;i++)
20 System.out.println(i+ " "+this.name);
21 }
22 }
详细示例2:
View Code
1 //用继承Thread创建线程
2 //线程A:打印A 100个
3 //线程B:打印B 100个
4 //线程100:打印1-100
5
6 public class CreateThreadExtendsThread {
7 public static void main(String[] args) {
8 PrintChar pc1 = new PrintChar('A',100);
9 PrintChar pc2 = new PrintChar('B',100);
10 PrintNumber pn = new PrintNumber(100);
11 //创建任务线程
12 Thread thread1 = new Thread(pc1);
13 Thread thread2 = new Thread(pc2);
14 Thread thread3 = new Thread(pn);
15 //启动线程
16 thread1.start();
17 thread2.start();
18 thread3.start();
19 }
20 }
21
22 class PrintCharacter extends Thread{
23 private char ch;
24 private int number;
25
26 public PrintCharacter(char ch,int number){
27 this.ch = ch;
28 this.number = number;
29 }
30
31 public void run(){
32 for(int i=1;i<=number;i++)
33 System.out.print(" "+ch);
34 }
35 }
36
37 class PrintNum extends Thread{
38 private int number;
39
40 public PrintNum(int number){
41 this.number = number;
42 }
43
44 public void run(){
45 for(int i=1;i<=number;i++)
46 System.out.print(" "+i);
47 }
48 }
2、实现Runnable接口
定义一个类,实现Runnable接口,并覆盖 run()方法,在这个方法里是你希望这个线程运行的代码。
创建一个这个新类的对象。
创建一个Thread类的对象,用刚才的Runnable对象作为构造函数参数。
调用Thread对象的start()方法来启动线程。
定义语法:
class classname implements Runnable{
属性;
方法;
//覆写Runnable类中run()
public void run(){
}
}
例如:
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
启动线程:
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
详细示例:
View Code
1 //用实现Runnable接口创建线程
2 //线程A:打印A 100个
3 //线程B:打印B 100个
4 //线程100:打印1-100
5 public class CreateThreadImplementRunnable {
6 public static void main(String[] args) {
7 //用实现Runnable接口
8 /*Runnable pc1 = new PrintChar('A',100);
9 Runnable pc2 = new PrintChar('B',100);
10 Runnable pn = new PrintNumber(100);
11 */
12 PrintChar pc1 = new PrintChar('A',100);
13 PrintChar pc2 = new PrintChar('B',100);
14 PrintNumber pn = new PrintNumber(100);
15
16 //创建任务线程
17 Thread thread1 = new Thread(pc1);
18 Thread thread2 = new Thread(pc2);
19 Thread thread3 = new Thread(pn);
20 //启动线程
21 thread1.start();
22 thread2.start();
23 thread3.start();
24 }
25 }
26
27 class PrintChar implements Runnable{
28 private char ch;
29 private int number;
30
31 public PrintChar(char ch,int number){
32 this.ch = ch;
33 this.number = number;
34 }
35
36 public void run(){
37 for(int i=1;i<=number;i++)
38 System.out.print(" "+ch);
39 }
40 }
41
42 class PrintNumber implements Runnable{
43 private int number;
44
45 public PrintNumber(int number){
46 this.number = number;
47 }
48
49 public void run(){
50 for(int i=1;i<=number;i++)
51 System.out.print(" "+i);
52 }
53 }