您的位置:首页 > 其它

传统线程(一)

2016-01-16 15:49 429 查看
package com.ygl;

public class TraditionThread {

public static void main(String[] args) {

//**************************************

Thread thread=new Thread(){

@Override

public void run() {

while(true)

try {

Thread.sleep(500);

System.out.println(Thread.currentThread().getName());

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

thread.start();

//**************************************

Thread thread2=new Thread(new Runnable(){

@Override

public void run() {

while(true)

try {

Thread.sleep(500);

System.out.println(Thread.currentThread().getName());

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

thread2.start();

//**************************************

new Thread(new Runnable(){

@Override

public void run() {

while(true)

try {

Thread.sleep(500);

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

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}){

//此处执行System.out.println("Thread"+Thread.currentThread().getName());

//而不会找System.out.println("Runnable"+Thread.currentThread().getName());因为被覆盖

public void run() {

while(true)

try {

Thread.sleep(500);

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

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

};

}.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: