一、传统线程的回顾

package com.susan.thread;


/**

 * 

 * 传统线程 实现线程的两种方式: 

 * 1.重载Thread的run方法实现 

 * 2.通过new runable接口实现

 * 

 * @author dahai

 * 

 */

public class TraditionalThread {


public staticvoid main(String[] agrs) {


// 方式一.

Thread t = new Thread() {

@Override

publicvoid run() {

while (true) {


try {

Thread.sleep(1000);

} catch (InterruptedException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("1:" + Thread.currentThread().getName());

}

}

};

t.start();


// 方式二. 开发中 常用这方式 好处:体现面向对象的思想


Thread t1 = new Thread(new Runnable() {


@Override

publicvoid run() {

//TODO Auto-generated method stub

while (true) {


try {

Thread.sleep(1000);

} catch (InterruptedException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("110:" + Thread.currentThread().getName());


}

}


});

t1.start();


/**

*  笔试题

*  下面的输出的结果是: System.out.println("120:" + Thread.currentThread().getName());

*  why:new Thread()是thread的子类 ,那么就先去找自己的子类的run方法,如果子类没有才去找父类的run方法

*  

*  

*/

new Thread(new Runnable() {


@Override

publicvoid run() {

//TODO Auto-generated method stub

System.out.println("你好中国!!!!");

}

}) {

@Override

publicvoid run() {

while (true) {


try {

Thread.sleep(1000);

} catch (InterruptedException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("120:" + Thread.currentThread().getName());


}

}


}.start();

}


}


你可能感兴趣的:(java)