[Java基础] 多线程

笔试中的题目, 考察wait()notifyAll(), 还不是很理解

public class Main {
    Integer i = 1;

    public Main() {

    }

    public static void main(String args[]) throws InterruptedException {
        final Main t = new Main();
        synchronized (t.i) {
            Thread thread = new Thread() {
                public void run() {
                    for (;;) {
                        synchronized (t.i) {
                            System.out.println("a:" + (++t.i));
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            t.i.notifyAll();
                        }
                    }
                }
            };
            thread.start();
            t.i.wait();
            System.out.println("b:" + (++t.i));
        }
    }
}

运行结果:

a:2
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
    at java.lang.Object.notifyAll(Native Method)
    at Main$1.run(Main.java:21)

[Java基础] 多线程


[Java基础] Top 10 questions of Java Strings

Top 10 questions of Java Strings


[笔记] 《Effective C++》

《Effective C++》笔记


[收藏] 通天塔导游 - 细数各种编程语言优缺点

通天塔导游 - 细数各种编程语言优缺点


[C++基础] 字符指针

char*


[C++基础] new 和 delete

new 和 delete


[C++基础] 虚函数

虚函数


[算法] Python 中的二分算法

Python 中的二分算法.


[基础] 浮点数

浮点数相关笔记