We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
不加线程等待会出现一个线程执行完成,第二个线程才执行(偶尔出现) 在每个线程第一个对象获取锁之后加线程等待可以解决这个问题 public class ThreadDeadLock {
public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); Thread thread1 = new Thread(() -> { synchronized (o1){ System.out.println(Thread.currentThread() + " get o1"); // 线程等待 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2){ System.out.println(Thread.currentThread() + " get o2"); } } }); Thread thread2 = new Thread(() -> { synchronized (o2){ System.out.println(Thread.currentThread() + " get o2"); // 线程等待 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1){ System.out.println(Thread.currentThread() + " get o1"); } } }); thread1.start(); thread2.start(); }
}
The text was updated successfully, but these errors were encountered:
欢迎提交 PR~
Sorry, something went wrong.
我的理解是:由于o1对象引用了o2对象,o2对象又引用了o1对象,并且o1与o2都加了同步锁,相互不释放,因此进入了死锁。
No branches or pull requests
不加线程等待会出现一个线程执行完成,第二个线程才执行(偶尔出现)
在每个线程第一个对象获取锁之后加线程等待可以解决这个问题
public class ThreadDeadLock {
}
The text was updated successfully, but these errors were encountered: