直接上代码:
import java.util.concurrent.locks.ReentrantLock;
public class MulThreadTest {
public static int a = 0;
ReentrantLock lock = new ReentrantLock(true);
public void addInt(){
lock.lock();
a++;
System.out.println(Thread.currentThread().getName()+"___________"+a +"_ " + lock.getHoldCount());
lock.unlock();
}
public static void main(String[] args) throws InterruptedException {
for(int i=0;i<3;i++){
new Thread(new Runnable() {
@Override
public void run() {
MulThreadTest test = new MulThreadTest();
for(int j=0;j<100;j++)
test.addInt();
}
},"thread"+i).start();
}
}
}
运行结果总会丢数字,难道是使用方法有问题?帮忙解答一下~