解决方案 »
- 如何设置JFrame在桌面上的初始显示位置
- 哪位大侠不吝赐教:用户的ADSL猫和电信局端之间传送的是PPP帧还是以太网帧?
- 在阅读JDK时发现的问题
- javaAPI与javaDOC有什么区别,有明白的人吗?
- 关于直接用在ie中打开excel文件的问题>>>>>>>>
- 如何让JTree初始化的时候全部展开?
- 大家帮帮我:关于在TableModel中的取得指定数据所在行的位置即index,该怎么做?
- 大家帮帮我,我的程序为什么不出界面(编译通过,执行时一闪即逝)
- 路径问题,求教
- java 泛型问题,通配符赋值,怎么解决?
- 【GUI】怎样通过输入数字来控制时钟,面板和时钟程序已经写好。
- 谁看看这段代码有什么问题 action=null 文件名为 userregister.jsp
TTSynchronized tt = new TTSynchronized();
Thread t1 = new Thread(tt);
Thread t2 = new Thread(tt);
t1.setName("Thread-1");
t2.setName("Thread-0");
t1.start();
t2.start(); }}
class TTSynchronized implements Runnable{
static int i=0;
public void run() {
synchronized(this){
for(i=0;i<30;i++){
if(i%3==0){
try {
this.notify();
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}
final static Object lock = new Object(); static int counter = 0; public static void main(String[] args) throws InterruptedException { Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
synchronized (lock) {
int number = ++counter;
if (number > 30)
return;
System.out.println(Thread.currentThread().getName() + " " + number);
}
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}; Thread thread0 = new Thread(runnable, "Thread-0");
Thread thread1 = new Thread(runnable, "Thread-1"); thread0.start();
thread1.start();
}
}