线程1:

public class TestThread1 ...{
public static void main(String args[]) ...{
Runner1 r = new Runner1();//第一个线程
//线程r第一次运行run()
r.start();//start()只是表示线程开始,线程什么时候运行,我们不知道。
//线程r直接调用run(),第二次运行run()
r.run();
Thread t = new Thread(r);//第二个线程
//线程t第三次运行run()
t.start();

for(int i=0; i<3; i++) ...{
System.out.println("Main Thread:------" + i);
}
}
}
//class Runner1 implements Runnable {
class Runner1 extends Thread ...{//Runner1继承Thread类,因此Runner1也是一个线程类。
public void run() ...{
for(int i=0; i<3; i++) ...{
System.out.println("Runner1 :" + i);
}
}
}
线程2:

public class TestThread2 ...{
public static void main(String args[]) ...{
Runner2 r = new Runner2();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}

class Runner2 implements Runnable ...{//Runner2实现了Runnable接口
public void run() ...{
for(int i=0; i<3; i++) ...{
System.out.println("No. " + i);
}
}
}
线程3:

public class TestThread3...{
public static void main(String args[]) ...{
Runner3 r = new Runner3();
Thread t = new Thread(r);
t.start();
}
}

class Runner3 implements Runnable ...{
public void run() ...{
for(int i=0; i<30; i++) ...{ 
if(i%10==0 && i!=0) ...{
try...{
Thread.sleep(2000); //停2000毫秒后再执行
}catch(InterruptedException e)...{}
}
System.out.println("No. " + i);
}
}
}

10万+

被折叠的 条评论
为什么被折叠?



