这个程序存在线程并发的问题,对不起,我刚才把循环次数写得太小了。现在改得大些,就出现的并发的问题:
public class ThreadDemo {
class RunnableImpl implements Runnable {
int count = 0;
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
count++;
System.out.println(Thread.currentThread().getName() + ":"
+ count);
}
}
}
public static void main(String[] args) {
ThreadDemo demo = new ThreadDemo();
RunnableImpl impl = demo.new RunnableImpl();
Thread t1 = new Thread(impl, "线程1");
Thread t2 = new Thread(impl, "线程2");
t1.start();
t2.start();
}
}
|
|