public class Singleton {
public static void main(String[] args) throws InterruptedException {
for (int i=0; i<10; i++){
new Thread(new QueueThread()).start();
Thread.sleep(10);
}
}
static class QueueThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is running...");
synchronized (QueueThread.class){
System.out.println(Thread.currentThread().getName() + " has got lock...");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
Thread-0 is running...
Thread-0 has got lock...
Thread-1 is running...
Thread-2 is running...
Thread-3 is running...
Thread-4 is running...
Java synchronized阻塞的同步队列现象分析
最新推荐文章于 2025-03-02 21:58:34 发布