日期:2014-05-20 浏览次数:20627 次
package thread;
public class Demo implements Runnable{
private String name;
public Demo(){
}
public Demo(String name){
this.name=name;
}
public void run(){
for(int i=0;i<20;i++){
System.out.println(name+" 运行 "+i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo1=new Demo("线程A");
Demo demo2=new Demo("线程B");
Thread thread1=new Thread(demo1);
Thread thread2=new Thread(demo2);
thread1.start();
thread2.start();
}
}
public class Demo implements Runnable{
private String name;
public Demo(){
}
public Demo(String name){
this.name=name;
}
public void run(){
for(int i=0;i<20;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+" 运行 "+i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo1=new Demo("线程A");
Demo demo2=new Demo("线程B");
Thread thread1=new Thread(demo1);
Thread thread2=new Thread(demo2);
thread1.start();
thread2.start();
}
}