Java - 尝试暂停一个线程

时间:2012-07-26 23:21:03

标签: java multithreading synchronization

  

可能重复:
  Java Wait and Notify: IllegalMonitorStateException

有什么问题
    private final Object lock; 
public synchronized void run() {
    while (numItersCompleted < maxNumIters) {
        while (guiState == GuiState.PAUSED) {
            try {
                lock.wait(); // problematic line

抛出:

java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)

1 个答案:

答案 0 :(得分:0)

锁定时没有synchronized

public void run() {
    while (numItersCompleted < maxNumIters) {
        while (guiState == GuiState.PAUSED) {
            try {
                synchronized (lock) {
                    lock.wait(); // problematic line
                }

我打算添加一个链接,但加里打赌我这个