【发布时间】:2020-08-16 20:56:19
【问题描述】:
public class ClassTest extends Thread{
public static Object lock = new Object();
public static LinkedList<Integer> stack;
public SortedSet<Integer> set= new TreeSet<>();
@Override
public void run(){
synchronized(lock){
// try{
// this.wait();
// }
// catch(Exception e){
// e.printStackTrace();
// }
while(!stack.isEmpty()){
set.add(stack.pop());
this.yield();
// this.notifyAll();
}
}
}
当我开始()5个线程时,为什么只有第一个弹出所有元素而其他人不弹出任何人? 我尝试使用 wait() 和 notify() 方法,但没有帮助..
【问题讨论】:
标签: java multithreading synchronization thread-safety