Java多线程中Reentrant类的带参构造器,创建一个公平锁,按道理说线程应该是先进先出的原则,为什么打印会是乱序。求解答:
package cn.zxyy.multiThread.chap4.ReentrantLock.Fair_noFair_test;
import java.util.concurrent.locks.ReentrantLock;
public class Service {
private ReentrantLock lock;
public Service(boolean isFair){
lock = new ReentrantLock(isFair);
}
public void serviceMethod(){
try{
lock.lock();
System.out.println("ThreadName = " +Thread.currentThread().getName()+"获得锁定");
}finally {
lock.unlock();
}
}
}
package cn.zxyy.multiThread.chap4.ReentrantLock.Fair_noFair_test;
public class RunFair {
public static void main(String[] args) {
final Service service = new Service(true);
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("♠线程"+Thread.currentThread().getName() +"运行了");
service.serviceMethod();
}
};
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread(runnable);
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
}
}
使用的是IntellijIdea2017会和这个工具的设置有关系吗?