标签:
1、实现多线程:继承Thread类;
2、实现Runnable接口,并且把该类当作参数传入Thread类或其子类的构造函数中;
例1:
1 class ThreadA{ 2 public void run(){ 3 for(int i = 0;i < 100:i++){ 4 System.out.println("ThreadA-->" + i); 5 } 6 } 7 } 8 9 class Test{ 10 public static void main(String[] args){ 11 for(int i = 0;i < 100;i++){ 12 System.out.println("Main-->" + i); 13 } 14 ThreadA ta = new ThreadA(); 15 ta.start(); 16 } 17 }
例2:
class RunnableImpl implements Runnable{ public void run(){ for(int i = 0;i < 100;i++){ System.out.println("Runnable-->" + i); } } } class Test{ public static void main(String[] args){
for(int i = 0;i < 100;i++){
System.out.println("main-->" + i);
}
RunnableImpl ri = new RunnableImpl();
Thread th = new Thread(ri);
th.start();
}
}
标签:
原文地址:http://www.cnblogs.com/zengneng/p/5597011.html