run执行
class T1 implements Runnable {
public void run() {
try {
for(int i=0;i<5;i++){
System.out.println(i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class T2 implements Runnable {
public void run() {
try {
for(int i=5;i<10;i++){
System.out.println(i);
Thread.sleep(100); //模拟耗时任务
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class test {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.run();
t2.run();
}
}
执行结果可看出,完全是先执行完t1.run()再去执行t2.run()
Start执行
public class test {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.start();
t2.start();
}
}
执行结果:
start方法是用于启动线程的,可以实现并发执行