package zhangge;
public class TestThread extends Thread{
static int n = 0;
public static synchronized void out()throws Exception
{
System.out.println(n++);
Thread.sleep(1000); }
public void run(){
try {
for (;n<=20;)
{
out();
}
}
catch (Exception e)
{ // TODO: handle exception }
}
}
}
package zhangge;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1 = new TestThread();
Thread t2 = new TestThread();
Thread t3 = new TestThread();
Thread t4 = new TestThread();
t1.start();
t2.start();
t3.start();
t4.start();
}
}