存档

文章标签 ‘多线程’

java 多线程小试

2011年4月18日 没有评论   

[java]

public class ThreadTest extends Thread {

private static Object obje = new Object();

public void run(){
synchronized (obje) {
for(int i=0;i<100;i++){
try {
if(i%10==0&&i!=0){
System.out.println("haha");
obje.notify();
obje.wait();
}
Thread.sleep(100);
System.out.println(this.getName()+":"+i);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

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

[/java]

部分输出结果:
阅读全文…

分类: java 标签: ,