マルチスレッドの中のロックのいくつかの使い方を簡単に説明します。
一、Reentrant Lock
package com.ietree.basicskill.mutilthread.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by Administrator on 2017/5/17.
*/
public class UseReentrantLock {
private Lock lock = new ReentrantLock();
public void method1(){
try {
lock.lock();
System.out.println(" :" + Thread.currentThread().getName() + " method1..");
Thread.sleep(1000);
System.out.println(" :" + Thread.currentThread().getName() + " method1..");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void method2(){
try {
lock.lock();
System.out.println(" :" + Thread.currentThread().getName() + " method2..");
Thread.sleep(2000);
System.out.println(" :" + Thread.currentThread().getName() + " method2..");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final UseReentrantLock ur = new UseReentrantLock();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
ur.method1();
ur.method2();
}
}, "t1");
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println(ur.lock.getQueueLength());
}
}
二、Reentrant ReadWriteLock
package com.ietree.basicskill.mutilthread.lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Created by Administrator on 2017/5/17.
*/
public class UseReentrantReadWriteLock {
private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
private ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();
private ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();
public void read(){
try {
readLock.lock();
System.out.println(" :" + Thread.currentThread().getName() + " ...");
Thread.sleep(3000);
System.out.println(" :" + Thread.currentThread().getName() + " ...");
} catch (Exception e) {
e.printStackTrace();
} finally {
readLock.unlock();
}
}
public void write(){
try {
writeLock.lock();
System.out.println(" :" + Thread.currentThread().getName() + " ...");
Thread.sleep(3000);
System.out.println(" :" + Thread.currentThread().getName() + " ...");
} catch (Exception e) {
e.printStackTrace();
} finally {
writeLock.unlock();
}
}
public static void main(String[] args) {
final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
urrw.read();
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
urrw.read();
}
}, "t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
urrw.write();
}
}, "t3");
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
urrw.write();
}
}, "t4");
// t1.start();
// t2.start();
// t1.start(); // R
// t3.start(); // W
t3.start();
t4.start();
}
}
三、コンディショニング
package com.ietree.basicskill.mutilthread.lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by Administrator on 2017/5/17.
*/
public class UseCondition {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void method1(){
try {
lock.lock();
System.out.println(" :" + Thread.currentThread().getName() + " ..");
Thread.sleep(3000);
System.out.println(" :" + Thread.currentThread().getName() + " ..");
condition.await(); // Object wait
System.out.println(" :" + Thread.currentThread().getName() +" ...");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void method2(){
try {
lock.lock();
System.out.println(" :" + Thread.currentThread().getName() + " ..");
Thread.sleep(3000);
System.out.println(" :" + Thread.currentThread().getName() + " ..");
condition.signal(); //Object notify
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final UseCondition uc = new UseCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
uc.method1();
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
uc.method2();
}
}, "t2");
t1.start();
t2.start();
}
}
四、ManyCodition
package com.ietree.basicskill.mutilthread.lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by Administrator on 2017/5/17.
*/
public class UseManyCondition {
private ReentrantLock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
public void m1(){
try {
lock.lock();
System.out.println(" :" +Thread.currentThread().getName() + " m1 ..");
c1.await();
System.out.println(" :" +Thread.currentThread().getName() + " m1 ..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m2(){
try {
lock.lock();
System.out.println(" :" +Thread.currentThread().getName() + " m2 ..");
c1.await();
System.out.println(" :" +Thread.currentThread().getName() + " m2 ..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m3(){
try {
lock.lock();
System.out.println(" :" +Thread.currentThread().getName() + " m3 ..");
c2.await();
System.out.println(" :" +Thread.currentThread().getName() + " m3 ..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m4(){
try {
lock.lock();
System.out.println(" :" +Thread.currentThread().getName() + " ..");
c1.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m5(){
try {
lock.lock();
System.out.println(" :" +Thread.currentThread().getName() + " ..");
c2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final UseManyCondition umc = new UseManyCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
umc.m1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
umc.m2();
}
},"t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
umc.m3();
}
},"t3");
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
umc.m4();
}
},"t4");
Thread t5 = new Thread(new Runnable() {
@Override
public void run() {
umc.m5();
}
},"t5");
t1.start(); // c1
t2.start(); // c1
t3.start(); // c2
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t4.start(); // c1
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t5.start(); // c2
}
}
以上のように、多くのスレッドの中のロックのいくつかの使い方をまとめました。つまり、小编が皆さんに共有している内容です。参考にしていただければと思います。よろしくお愿いします。