public ReentrantLock() {
sync = new NonfairSync();
}
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
public class Test {
private static class Counter {
private ReentrantLock mReentrantLock = new ReentrantLock();
public void count() {
mReentrantLock.lock();
try {
for (int i = 0; i < 6; i++) {
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
} finally {
// 必须在 finally 释放锁
mReentrantLock.unlock();
}
}
}
private static class MyThread extends Thread {
private Counter mCounter;
public MyThread(Counter counter) {
mCounter = counter;
}
@Override
public void run() {
super.run();
mCounter.count();
}
}
public static void main(String[] var0) {
Counter counter = new Counter();
// 注:myThread1 和 myThread2 是调用同一个对象 counter
MyThread myThread1 = new MyThread(counter);
MyThread myThread2 = new MyThread(counter);
myThread1.start();
myThread2.start();
}
}
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1, i = 0 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-1, i = 4 Thread-1, i = 5
public class Test {
private static class Counter {
private ReentrantLock mReentrantLock = new ReentrantLock();
public void count() {
for (int i = 0; i < 6; i++) {
mReentrantLock.lock();
// 模拟耗时,突出线程是否阻塞
try{
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + ", i = " + i);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 必须在 finally 释放锁
mReentrantLock.unlock();
}
}
}
public void doOtherThing(){
for (int i = 0; i < 6; i++) {
// 模拟耗时,突出线程是否阻塞
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i);
}
}
}
public static void main(String[] var0) {
final Counter counter = new Counter();
new Thread(new Runnable() {
@Override
public void run() {
counter.count();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
counter.doOtherThing();
}
}).start();
}
}
Thread-0, i = 0 Thread-1 doOtherThing, i = 0 Thread-0, i = 1 Thread-1 doOtherThing, i = 1 Thread-0, i = 2 Thread-1 doOtherThing, i = 2 Thread-0, i = 3 Thread-1 doOtherThing, i = 3 Thread-0, i = 4 Thread-1 doOtherThing, i = 4 Thread-0, i = 5 Thread-1 doOtherThing, i = 5
public class Test {
private static class Counter {
private ReentrantLock mReentrantLock = new ReentrantLock();
public void count() {
for (int i = 0; i < 6; i++) {
mReentrantLock.lock();
// 模拟耗时,突出线程是否阻塞
try{
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + ", i = " + i);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 必须在 finally 释放锁
mReentrantLock.unlock();
}
}
}
public void doOtherThing(){
mReentrantLock.lock();
try{
for (int i = 0; i < 6; i++) {
// 模拟耗时,突出线程是否阻塞
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i);
}
}finally {
mReentrantLock.unlock();
}
}
}
public static void main(String[] var0) {
final Counter counter = new Counter();
new Thread(new Runnable() {
@Override
public void run() {
counter.count();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
counter.doOtherThing();
}
}).start();
}
}
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1 doOtherThing, i = 0 Thread-1 doOtherThing, i = 1 Thread-1 doOtherThing, i = 2 Thread-1 doOtherThing, i = 3 Thread-1 doOtherThing, i = 4 Thread-1 doOtherThing, i = 5
public class Test {
static final int TIMEOUT = 300;
private static class Counter {
private ReentrantLock mReentrantLock = new ReentrantLock();
public void count() {
try{
//lock() 不可中断
mReentrantLock.lock();
// 模拟耗时,突出线程是否阻塞
for (int i = 0; i < 6; i++) {
long startTime = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - startTime > 100)
break;
}
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
} finally {
// 必须在 finally 释放锁
mReentrantLock.unlock();
}
}
public void doOtherThing(){
try{
//lockInterruptibly() 可中断,若线程没有中断,则获取锁
mReentrantLock.lockInterruptibly();
for (int i = 0; i < 6; i++) {
// 模拟耗时,突出线程是否阻塞
long startTime = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - startTime > 100)
break;
}
System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i);
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " 中断 ");
}finally {
// 若当前线程持有锁,则释放
if(mReentrantLock.isHeldByCurrentThread()){
mReentrantLock.unlock();
}
}
}
}
public static void main(String[] var0) {
final Counter counter = new Counter();
new Thread(new Runnable() {
@Override
public void run() {
counter.count();
}
}).start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
counter.doOtherThing();
}
});
thread2.start();
long start = System.currentTimeMillis();
while (true){
if (System.currentTimeMillis() - start > TIMEOUT) {
// 若线程还在运行,尝试中断
if(thread2.isAlive()){
System.out.println(" 不等了,尝试中断 ");
thread2.interrupt();
}
break;
}
}
}
}
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 不等了,尝试中断 Thread-1 中断 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1 doOtherThing, i = 0 Thread-1 doOtherThing, i = 1 Thread-1 doOtherThing, i = 2 Thread-1 doOtherThing, i = 3 Thread-1 doOtherThing, i = 4 Thread-1 doOtherThing, i = 5
public class Test {
static final int TIMEOUT = 3000;
private static class Counter {
private ReentrantLock mReentrantLock = new ReentrantLock();
public void count() {
try{
//lock() 不可中断
mReentrantLock.lock();
// 模拟耗时,突出线程是否阻塞
for (int i = 0; i < 6; i++) {
long startTime = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - startTime > 100)
break;
}
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
} finally {
// 必须在 finally 释放锁
mReentrantLock.unlock();
}
}
public void doOtherThing(){
try{
//tryLock(long timeout, TimeUnit unit) 尝试获得锁
boolean isLock = mReentrantLock.tryLock(300, TimeUnit.MILLISECONDS);
System.out.println(Thread.currentThread().getName() + " isLock:" + isLock);
if(isLock){
for (int i = 0; i < 6; i++) {
// 模拟耗时,突出线程是否阻塞
long startTime = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - startTime > 100)
break;
}
System.out.println(Thread.currentThread().getName() + " doOtherThing, i = " + i);
}
}else{
System.out.println(Thread.currentThread().getName() + " timeout");
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " 中断 ");
}finally {
// 若当前线程持有锁,则释放
if(mReentrantLock.isHeldByCurrentThread()){
mReentrantLock.unlock();
}
}
}
}
public static void main(String[] var0) {
final Counter counter = new Counter();
new Thread(new Runnable() {
@Override
public void run() {
counter.count();
}
}).start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
counter.doOtherThing();
}
});
thread2.start();
}
}
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-1 isLock:false Thread-1 timeout Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有