8.9java学习笔记——多线程

多线程

一、多线程的三种创建方式

 

1、通过继承Thread

************************************************

public class duoxiancheng extends Thread{

public void run(){

//创建一个无线循环(但是加入了时间间隔)

while(true){

/**

 * 

 * 

 * 具体内容代码

 * 

 * 

 */

//需要加入异常处理机制

try {

Thread.sleep(10);

catch (Exception e) {

e.printStackTrace();

}

}

}

}

************************************************

duoxiancheng th = new duoxiancheng();

th.start();

 

2、通过实现Runnable接口

************************************************

public class duoxiancheng implements Runnable{

public void run(){

//创建一个无线循环(但是加入了时间间隔)

while(true){

/**

 * 

 * 

 * 具体内容代码

 * 

 * 

 */

//需要加入异常处理机制

try {

Thread.sleep(10);

catch (Exception e) {

e.printStackTrace();

}

}

}

}

************************************************

duoxiancheng rt = new duoxiancheng();

Thread thread = new Thread(rt);

thread.start();

 

3、通过匿名内部类

【略】

猜你喜欢

转载自yuyongjia.iteye.com/blog/1626454
今日推荐