您的位置:首页 > 其它

创建线程方式

2015-08-25 22:38 190 查看
创建线程的2中方式:重写Thread的Run方法、将Runnable作为参数传递给Thread

public class TranditionalThread {

/**

* @param args

*/

public static void main(String[] args) {

//创建线程方式1

Thread thread = new Thread(){

@Override

public void run() {

while(true){

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

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

}

}

};

thread.start();

//创建线程方式2

Thread thread2 = new Thread(new Runnable(){

@Override

public void run() {

while(true){

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

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

}

}

});

thread2.start();

}

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