关于多线程,Concurrent包中的lock接口,和信号量很好的帮助我们解决多线程之间的同步,互斥,轮流执行等操作,且简单易懂,共享,相信掌握lock,Semaphore等接口可以解决大部分多线程问题,是很方便的一个类,可以很好的解决轮流打印ABC的问题等,这次我分享一个相似的问题,三个数组{1,3,5,7,9},{2,4,6,8,9},{‘a’,'b','c','d','e'},线程1打印1,线程2打印2,线程三打印a,再回来,线程一打印3,如此往下,下面是两种方式实现,synchronized配合nodifyall,wait,也可以实现,类似于lock,这次介绍concurrent中的接口。
第一种 Concurrent.locks
public class Lock { public static void main(String[] args) throws InterruptedException { System.out.println("开始"); CountDownLatch num = new CountDownLatch(3);//每一个线程结束减一,保证main线程最后结束 new Thread(new pr(0,num)).start(); new Thread(new pr(1,num)).start(); new Thread(new pr(2,num)).start(); num.await();//此方法会等待num等于0 System.out.println("\r\n结束"); } } class pr implements Runnable{ static ReentrantLock lock = new ReentrantLock(); //ReentrantLock是lock接口的一个实现类int[] a1 = {1, 3, 5, 7, 9};int[] a2 = {2, 4, 6, 8, 0};
char[] a3 = {'a', 'b', 'c', 'd', 'e'};
static int state = 0; //三个进程共享的,三个都可以修改的,state的相关操作可以判断哪一个线程启动
private int id;
CountDownLatch num = null;
public pr(int id,CountDownLatch o){ this.id = id; this.num = o; }
@Override
public void run() {
while (state<=14){
lock.lock(); //对lock加锁
if (state%3==id && state<=14){
if (id==0){
System.out.print(a1[state/3]+" ");
if (state==12) num.countDown();
}else if (id==1){
System.out.print(a2[state/3]+" ");
if (state==13) num.countDown();
}else if (id==2){
System.out.print(a3[state/3]+" ");
if (state ==14) num.countDown();
}
state++;
}
lock.unlock();//解锁
}
}}
运行效果:
开始
1 2 a 3 4 b 5 6 c 7 8 d 9 0 e
结束
第二种
信号量(类似于操作系统中的PV操作,信号量是资源数,当信号量<0时,则信号量的绝对值为等待的进程数目),这里想要达到同步效果,
则信号量为1,当一个线程取走这个信号量的时候,(把信号量看成一种许可证)它就可以运行,其他想要争取这个资源的线程需要等待。接下来的这个例子:A,B,C
初始信号量都为1,B,C的信号量先各自夺走一个(但是此时BC不会运行,因为还没有调用start方法),这样就保证A先运行,A获取信号,
执行一些代码后,B释放刚刚被夺走的B资源,A线程执行完毕后(中途不会有其他线程打断A,因为A执行时,BC的信号量为0,而且有信号量的BC
线程还没有start)
,A,C信号量为0,B为1,此时只有B线程可以获取其资源,继续开始,如此反复一遍import java.util.concurrent.CountDownLatch; import java.util.concurrent.Semaphore; public class Semaph { static CountDownLatch count; static int[] a1 = {1, 3, 5, 7, 9}; static int[] a2 = {2, 4, 6, 8, 0}; static char[] a3 = {'a', 'b', 'c', 'd', 'e'}; static Semaphore A = new Semaphore(1); static Semaphore B = new Semaphore(1); static Semaphore C = new Semaphore(1); public static void main(String[] args) throws InterruptedException { System.out.println("开始"); B.acquire(); C.acquire(); count = new CountDownLatch(3); new Thread(new printA()).start(); new Thread(new printB()).start(); new Thread(new printC()).start(); count.await(); System.out.println("\r\n结束"); } static class printA extends Thread{ @Override public void run() { for (int i = 0; i< 5;i++){ try { A.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print(a1[i]+" "); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } B.release(); } count.countDown(); } } static class printB extends Thread{ @Override public void run() { for (int i = 0; i< 5;i++){ try { B.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print(a2[i]+" "); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } C.release(); } count.countDown(); } } static class printC extends Thread{ @Override public void run() { for (int i = 0; i< 5;i++){ try { C.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print(a3[i]+" "); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } A.release(); } count.countDown(); } } } 运行结果: 开始 1 2 a 3 4 b 5 6 c 7 8 d 9 0 e 结束