解决方案 »
- 求下面这段文字来自的书或者专辑技术文章
- BLOB 转成 string
- 如何捕捉JTextpane上的特定内容
- 郁闷呀,一天到晚学JAVA,三个月了,感觉还是什么都做不出来?
- 关于等差数列和的问题
- java连接sqlserver问题java.sql.SQLException: No suitable driver
- JTable可能实现网格合并的功能吗?
- 大哥们,救救急,怎么回事???
- 为什么我在JBuilder中写好的.Java的程序,编译通过,但用c:\java source没法执行,用dos下 javac编译还有错
- 如何在程序里选择切换输入法?
- RandomAccessFile的问题
- eclipse运行错误,求大神帮忙。
public class Volatiletest extends Thread {
private static AtomicInteger atomic = new AtomicInteger(0);
public void run() {
atomic.getAndIncrement();
}
public static void main(String[] args) throws InterruptedException {
Thread threads[] = new Thread[100];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Volatiletest();
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
Thread.sleep(1000);
System.out.println(atomic.get());
}
}
public class Volatiletest extends Thread {
private static AtomicInteger atomic = new AtomicInteger(0);
private static int n = 0; public void run() {
n++;
atomic.getAndIncrement();
if(n==100){
System.out.println("第100次执行");
}
} public static void main(String[] args) throws InterruptedException {
Thread threads[] = new Thread[100];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Volatiletest();
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
System.out.println(atomic.get());
}
},加个打印应该就能看出来了
sleep()多长时间?不是好的问题的解决办法。
你要等待这100个线程都运行结束,则你要在 threads[i].start(); 之后 加上如下代码: try {
threads[i].join();
} catch (InterruptedException e) {}