ananxuan
2021-02-09 12:03
采纳率: 100%
浏览 56
已结题

调用wait方法报错,有前辈能指导下吗

public class Test{
    public static Integer i=0;
    public static void main(String[] args) {

        Object o1=new Object();
        ReentrantLock lock=new ReentrantLock();

        Thread t1=new Thread(new Runnable() {

            @Override
            public  void run() {
                lock.lock();
                i++;
                try {
                    this.wait(2000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(i);
                lock.unlock();
            }

        });

        Thread t2=new Thread(new Runnable() {
            @Override
            public  void run() {
                lock.lock();
                i++;
                System.out.println(i);
                lock.unlock();
            }
        });
        t1.start();

        try {
            Thread.sleep(10l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }
}

报错如下

Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at com.suning.ctmodp.Controller.Test$1.run(Test.java:19)
    at java.lang.Thread.run(Thread.java:748)

 

调用wait方法报错了,我觉得调用wait得时候t1是拿到自己得对象所得不知道为啥报这个,其实我一开始写的是lock.wait(),我就想看下wait是不是释放锁了,之前用sleep结果正确没有释放锁,难道lock之后不能调用wait方法吗

  • 好问题 提建议
  • 收藏

4条回答 默认 最新

  • allway2 2021-02-09 12:47
    已采纳
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test{
        public static Integer i=0;
        public static void main(String[] args) {
    
            Object o1=new Object();
            ReentrantLock lock=new ReentrantLock();
    
            Thread t1=new Thread(new Runnable() {
    
                @Override
                public  void run() {
                	synchronized (this) {
                    lock.lock();
                    i++;
                    try {
                        this.wait(2000l);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    System.out.println(i);
                    lock.unlock();
                }
    
            }
        });
    
            Thread t2=new Thread(new Runnable() {
                @Override
                public  void run() {
                    lock.lock();
                    i++;
                    System.out.println(i);
                    lock.unlock();
                }
            });
            t1.start();
    
            try {
                Thread.sleep(10l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t2.start();
        }
    }
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • allway2 2021-02-09 12:47

    wait和synchronized 配合使用

    在调用Wait()之前,notify()和notifyAll()方法线程必须在对象的监视器上拥有锁,这意味着必须从同步块或同步方法中调用wait(),notify()和notifyAll()方法,否则将在抛出IllegalMonitorStateException运行。

    评论
    解决 无用
    打赏 举报
  • allway2 2021-02-09 12:53
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test {
    	public static Integer i = 0;
    
    	public static void main(String[] args) {
    
    		Object o1 = new Object();
    		ReentrantLock lock = new ReentrantLock();
    
    		Thread t1 = new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    
    				lock.lock();
    				i++;
    				try {
    					Thread.sleep(2000);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    
    				System.out.println(i);
    				lock.unlock();
    			}
    
    		});
    
    		Thread t2 = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				lock.lock();
    				i++;
    				System.out.println(i);
    				lock.unlock();
    			}
    		});
    		t1.start();
    
    		try {
    			Thread.sleep(10l);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		t2.start();
    	}
    }
    评论
    解决 无用
    打赏 举报
  • allway2 2021-02-09 12:54

    或者使用Thread.sleep(2000);看程序这里可能原本想使用Thread.sleep(2000);

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题