- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Waiting w=new Waiting();
- new Thread (w) .start();
- new Thread (w) .start();
- }
- }
- class Waiting implements Runnable {
- boolean flag = false;
- public synchronized void run() {
- if (flag) {
- flag = false;
- System.out.print("1");
- try {
- this.wait();
- } catch (Exception e) {
- }
- System.out.print("2");
- } else {
- flag = true;
- System.out.print("3");
- try {
- Thread.sleep(2000);
- } catch (Exception e) {
- }
- System.out.print("4");
- notifyAll();
- }
- }
- }
复制代码 疑问:这段代码的输出结果是什么?这段代码会运行结束么?
|