JAVA线程之三----创建

java 线程回顾
创建线程的方式
其实就一种,就是通过Thread类的构造函数来创建,一是无参构造,另一种是通过Runnable对象作为参数来创建。所以他们都是执行的run()方法。
但是习惯称之为继承 thread类和实现Runnable接口来创建线程。


来看代码
package com.sample.runnable;


public class RecomendedThread {

public static void main(String[] args){
Thread t = new Thread(
new Runnable(){
           //here is implement of Runnable interface;
@Override
public void run() {
for(int i=0;i<10;i++){

System.out.println(Runnable.class+"  "+i);
}
}

}
)
/* {
         // here is override of Thread class.
public void run(){
for(int i=0;i<10;i++){

System.out.println(Thread.class+"  "+i);
}
}
}*/
;
t.start();
}
}






运行结果如下
interface java.lang.Runnable  0
interface java.lang.Runnable  1
interface java.lang.Runnable  2
interface java.lang.Runnable  3
interface java.lang.Runnable  4
interface java.lang.Runnable  5
interface java.lang.Runnable  6
interface java.lang.Runnable  7
interface java.lang.Runnable  8
interface java.lang.Runnable  9


将注释/**/ 去掉运行结构如下
class java.lang.Thread  0
class java.lang.Thread  1
class java.lang.Thread  2
class java.lang.Thread  3
class java.lang.Thread  4
class java.lang.Thread  5
class java.lang.Thread  6
class java.lang.Thread  7
class java.lang.Thread  8
class java.lang.Thread  9


所以充分说明 如果你既实现了Runnable 的run()方式,又重写了Thread 的run()方法,那么实现Runnable的run()方法将被忽略。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值