import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test{
private ArrayList<Integer> list = new ArrayList<>();
private int num = 5;
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Test test = new Test();
new Thread(){
public void run(){
try {
System.out.println("线程0开始执行");
test.isStart(this,lock);
System.out.println("线程0结束执行");
}catch (Exception e){
e.printStackTrace();
}
};
}.start();
new Thread(){
public void run(){
try {
System.out.println("线程1开始执行");
test.isStart(this,lock);
System.out.println("线程1结束执行");
/* this.sleep(3000);*/
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
}
/*
* lock非阻塞
* */
public void lockStart(Thread thread,Lock lock){
lock.lock();
try {
while (num > 0){
num=num-1;
System.out.println(thread.getName()+"花费1元");
}
}catch (Exception e){
e.printStackTrace();
}finally {
// 可以保证发生异常 锁可以得到释放 避免死锁的发生
lock.unlock();
}
}
/*
* 阻塞
* */
public void synStart(Thread thread){
synchronized (this){
while (num > 0){
num=num-1;
System.out.println(thread.getName()+"花费1元");
}
}
}
/*
* 返回true 和 false
* */
public void isStart(Thread thread,Lock lock) {
try {
if (lock.tryLock()) {
while (num > 0) {
num = num - 1;
System.out.println(thread.getName() + "花费1元");
}
}else {
System.out.println("没有拿到"+lock.tryLock());
return;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/*
* lockInterruptibly()响应中断的使用方法:
* */
public void interruptibly() {
}
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class test02 {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
new Thread() {
@Override
public void run() {
String tName = Thread.currentThread().getName();
if (lock.tryLock()) {
System.out.println(tName + "获取到锁!");
} else {
System.out.println(tName + "获取不到锁!");
return;
}
try {
for (int i = 0; i < 5; i++) {
System.out.println(tName + ":" + i);
}
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(tName + "出错了!!!");
} finally {
System.out.println(tName + "释放锁!!");
lock.unlock();
}
}
}.start();
new Thread() {
@Override
public void run() {
String tName = Thread.currentThread().getName();
if (lock.tryLock()) {
System.out.println(tName + "获取到锁!");
} else {
System.out.println(tName + "获取不到锁!");
return;
}
try {
for (int i = 0; i < 5; i++) {
System.out.println(tName + ":" + i);
}
} catch (Exception e) {
System.out.println(tName + "出错了!!!");
} finally {
System.out.println(tName + "释放锁!!");
lock.unlock();
}
}
}.start();
}
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test3 {
class Mythread extends Thread{
private Test3 test3;
public Mythread(Test3 test3){
this.test3 = test3;
}
@Override
public void run(){
try {
test3.insert(Thread.currentThread());
}catch (Exception e){
System.out.println(Thread.currentThread().getName()+"被中断");
}
}
}
private Lock lock = new ReentrantLock();
public void insert(Thread thread)throws InterruptedException{
lock.lockInterruptibly();
try {
System.out.println(thread.getName()+"得到了锁");
long starTime = System.currentTimeMillis();
for(int i =0;i<5;i++){
if( System.currentTimeMillis() - starTime >= Integer.MAX_VALUE ){
break;
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println(Thread.currentThread().getName()+"执行finaly");
lock.unlock();
System.out.println(thread.getName()+"释放了锁");
}
}
public static void main(String[] args) {
Test3 test3 = new Test3();
Thread thread1 = new Mythread(test3);
Mythread thread2 = new Mythread(test3);
thread1.start();
thread2.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.interrupt();
}
}