Runnable接口与Thread类的区别

[url]http://hi.baidu.com/topsu/blog/item/c179c21876e180b24bedbc66.html[/url]
Runnable接口与Thread类的区别

如果让一个线程实现Runnable接口,那么当调用这个线程的对象开辟多个线程时,可以让这些线程调用同一个变量;若这个线程是由继承Thread类而来,则要通过内部类来实现上述功能,利用的就是内部类可任意访问外部变量这一特性。

例子程序:

public class ThreadTest {

public static void main(String[] args) {
MyThread mt = new MyThread();
new Thread(mt).start(); //通过实现Runnable的类的对象来开辟第一个线程
new Thread(mt).start(); //通过实现Runnable的类的对象来开辟第二个线程
new Thread(mt).start(); //通过实现Runnable的类的对象来开辟第三个线程
//由于这三个线程是通过同一个对象mt开辟的,所以run()里方法访问的是同一个index
}
}

class MyThread implements Runnable { //实现Runnable接口

int index = 0;

public void run() {
for(;index <= 200;)
System.out.println(Thread.currentThread().getName() + ":" + index++);
}
}


运行结果:

Thread-0:0
Thread-2:1
Thread-2:2
Thread-2:3
Thread-2:4
Thread-2:5
Thread-2:6
Thread-2:7
Thread-2:8
Thread-2:9
Thread-2:10
Thread-2:11
Thread-2:12
Thread-2:13
Thread-2:14
Thread-2:15
Thread-2:16
Thread-2:17
Thread-0:18
Thread-1:19
Thread-0:20
...


public class ThreadTest {

public static void main(String[] args) {

MyThread mt=new MyThread();
mt.getThread().start(); //通过返回内部类的对象来开辟第一个线程
mt.getThread().start(); //通过返回内部类的对象来开辟第二个线程
mt.getThread().start(); //通过返回内部类的对象来开辟第三个线程
//由于这三个线程是通过同一个匿名对象来开辟的,所以run()里方法访问的是同一个index
}
}

class MyThread {

int index = 0;

private class InnerClass extends Thread { //定义一个内部类,继承Thread

public void run() {
for(; index <= 200;)
System.out.println(getName() + ":" + index++);
}
}

Thread getThread() { //这个函数的作用是返回InnerClass的一个匿名对象
return new InnerClass();
}
}


这里有一个问题:如果内部类要访问一个外部变量或方法,那么这个变量或方法必须定义为final,但为什么这里的变量index不用定义为final就可以被内部类访问?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值