//继承Thread类
package zuoye;
//继承Thread类
public class City extends Thread{
private String name;
public City(String name) {
super();
this.name = name;
}
public void run() {
for(int i=0;i<10;i++)
{
System.out.println(this.name+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
//Runnable接口
package zuoye;
//Runnable接口
public class City2 implements Runnable {
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+" "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
//测试类
package zuoye;
public class Test {
public static void main(String[] args) {
//Thread类
City c1=new City("铁岭");
c1.start();
City c2=new City("纽约");
c2.start();
//Runnable
City2 t1=new City2();
Thread t2=new Thread(t1, "淄博");
t2.start();
Thread t3=new Thread(t1, "美国");
t3.start();
}
}