为了更好地理解Java中的线程,我编写了以下代码
public class SimpleRunnableTest {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
Thread t1 = new Thread(new TT1());
t1.start();
Thread t2 = new Thread(new TT2());
t2.start();
t2.join();
t1.join();
long end = System.currentTimeMillis();
System.out.println("end-start="+(end-start));
}
}
class TT1 implements Runnable {
public void run(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class TT2 implements Runnable {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} …Run Code Online (Sandbox Code Playgroud)