lock
package com.yf.reentrantLock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DemoLock {
public static void main(String[] args) {
int proLen=1;
int conLen=1;
Lock lock=new ReentrantLock();
Thread produtors[]=new Thread[proLen];
Thread consumers[]=new Thread[conLen];
for (int i = 0; i < produtors.length; i++) {
produtors[i]=new Thread(()->{
sleep(3000);
lock.lock();
try {
System.out.println("生产者线程号:"+Thread.currentThread()+"在生产");
}catch (Exception e){
System.out.println("加锁失败,出现异常");
}finally {
lock.unlock();
}
});
}
int m=9;
for (int i = 0; i < consumers.length; i++) {
consumers[i]=new Thread(()->{
lock.lock();
System.out.println("消费者线程号"+Thread.currentThread()+"在消费");
while (true){
if(m==5){
break;
}
}
lock.unlock();
});
}
for (Thread produtor : produtors) {
produtor.start();
}
for (Thread consumer : consumers) {
consumer.start();
}
}
public static void sleep(long millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
trylock
package com.yf.reentrantLock;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DemoTryLock {
public static void main(String[] args) {
int proLen = 1;
int conLen = 1;
Lock lock = new ReentrantLock();
Thread produtors[] = new Thread[proLen];
Thread consumers[] = new Thread[conLen];
for (int i = 0; i < produtors.length; i++) {
produtors[i] = new Thread(() -> {
sleep(3000);
if (lock.tryLock()) {
try {
System.out.println("生产者线程号:" + Thread.currentThread() + "在生产");
} catch (Exception e) {
System.out.println("加锁失败,出现异常");
} finally {
lock.unlock();
}
} else {
System.out.println("未获取这把锁,生产者线程停止生产,当前线程也执行完成,非阻塞");
}
});
}
AtomicInteger m = new AtomicInteger(9);
for (int i = 0; i < consumers.length; i++) {
consumers[i] = new Thread(() -> {
try {
lock.lock();
System.out.println("消费者线程号" + Thread.currentThread() + "在消费");
while (true) {
if (m.get() == 2000000000) {
break;
}
m.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
}
for (Thread produtor : produtors) {
produtor.start();
}
for (Thread consumer : consumers) {
consumer.start();
}
}
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
lockInterruptibly 抢占其他线程的锁
LockTnterruptily 是否能抢夺正在运行其他线程的锁?否
package com.yf.reentrantLock;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DemoLockTnterruptily {
public static void main(String[] args) {
int proLen = 1;
int conLen = 1;
Lock lock = new ReentrantLock();
Thread produtors[] = new Thread[proLen];
Thread consumers[] = new Thread[conLen];
for (int i = 0; i < produtors.length; i++) {
produtors[i] = new Thread(() -> {
sleep(3000);
try {
lock.lockInterruptibly();
System.out.println("生产者线程号:" + Thread.currentThread() + "在生产");
} catch (Exception e) {
System.out.println("加锁失败,出现异常");
} finally {
lock.unlock();
}
});
}
AtomicInteger m = new AtomicInteger(9);
for (int i = 0; i < consumers.length; i++) {
consumers[i] = new Thread(() -> {
try {
lock.lock();
System.out.println("消费者线程号" + Thread.currentThread() + "在消费");
while (true) {
if (m.get() == 2000000000) {
break;
}
m.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
}
for (Thread produtor : produtors) {
produtor.start();
}
for (Thread consumer : consumers) {
consumer.start();
}
}
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
LockTnterruptily 是否能抢夺正在阻塞其他线程的锁?是
package com.yf.reentrantLock;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DemoLockTnterruptily2 {
public static void main(String[] args) {
int proLen = 1;
int conLen = 1;
Lock lock = new ReentrantLock();
Thread produtors[] = new Thread[proLen];
Thread consumers[] = new Thread[conLen];
for (int i = 0; i < produtors.length; i++) {
produtors[i] = new Thread(() -> {
sleep(3000);
try {
lock.lockInterruptibly();
System.out.println("生产者线程号:" + Thread.currentThread() + "在生产");
} catch (Exception e) {
System.out.println("加锁失败,出现异常");
} finally {
lock.unlock();
}
});
}
for (int i = 0; i < consumers.length; i++) {
consumers[i] = new Thread(() -> {
try {
lock.lock();
System.out.println("消费者线程号" + Thread.currentThread() + "在消费");
lock.wait();
System.out.println("消费者线程号" + Thread.currentThread() + "消费结束");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
}
for (Thread produtor : produtors) {
produtor.start();
}
for (Thread consumer : consumers) {
consumer.start();
}
}
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}