【摘要】 (4) 面试题-三个线程打印三个数组批次打印
a.LockSupport来打印
package com.lcz.thread;import java.util.concurrent.locks.LockSupport;
public class Test13 {
// 三个线程
static Thread t1 = null,t2 = null,t3=nu…
(4) 面试题-三个线程打印三个数组批次打印
a.LockSupport来打印
package com.lcz.thread;
import java.util.concurrent.locks.LockSupport;
public class Test13 {
// 三个线程
static Thread t1 = null,t2 = null,t3=null;
public static void main(String[] args) { char[] a1 = "123".toCharArray();
char[] a2 = "abc".toCharArray();
char[] a3 = "ABC".toCharArray();
// 开始
t1 = new Thread(()->{ for(char c:a1) { LockSupport.park(); System.out.println(Thread.currentThread().getName()+":"+c); LockSupport.unpark(t2); } },"t1"); t2 = new Thread(()->{ for(char c:a2) { LockSupport.park(); System.out.println(Thread.currentThread().getName()+":"+c); LockSupport.unpark(t3); } },"t2"); t3 = new Thread(()->{ for(char c:a3) { LockSupport.park(); System.out.println(Thread.currentThread().getName()+":"+c); LockSupport.unpark(t1); } },"t3"); t1.start();
t2.start();
t3.start(); LockSupport.unpark(t1);
}
}
b.reetrantlock+condition来打印
package com.lcz.thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
public class Test13 {
// 三个线程
static Thread t1 = null,t2 = null,t3=null;
public static void main(String[] args) { char[] a1 = "123".toCharArray();
char[] a2 = "abc".toCharArray();
char[] a3 = "ABC".toCharArray(); ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition(); // 开始
t1 = new Thread(()->{ try { lock.lock(); for(char c:a1) { System.out.println(Thread.currentThread().getName()+":"+c); condition.signal(); condition.await(); } condition.signal(); } catch (Exception e) { e.printStackTrace(); }finally { lock.unlock(); }
},"t1"); t2 = new Thread(()->{ try { lock.lock(); for(char c:a2) { System.out.println(Thread.currentThread().getName()+":"+c); condition.signal(); condition.await(); } condition.signal(); } catch (Exception e) { e.printStackTrace(); }finally { lock.unlock(); }
},"t2"); t3 = new Thread(()->{ try { lock.lock(); for(char c:a3) { System.out.println(Thread.currentThread().getName()+":"+c); condition.signal(); condition.await(); } condition.signal(); } catch (Exception e) { e.printStackTrace(); }finally { lock.unlock(); }
},"t3"); t1.start();
t2.start();
t3.start(); }
}
c.乐观锁来实现(Enum来实现)
package com.lcz.thread;
// 枚举类
enum MyThreadRun{
t1,t2,t3
}
public class Test14 {
static volatile MyThreadRun r = MyThreadRun.t1;
public static void main(String[] args) {
char[] a1 = "123".toCharArray();
char[] a2 = "abc".toCharArray();
char[] a3 = "ABC".toCharArray(); Thread t1 = new Thread(()->{ //打印 for(char c:a1) { while(r!=MyThreadRun.t1) { } System.out.print(Thread.currentThread().getName()+":"+c); r = MyThreadRun.t2; }
},"t1"); Thread t2 = new Thread(()->{ //打印 for(char c:a2) { while(r!=MyThreadRun.t2) { } System.out.print(Thread.currentThread().getName()+":"+c); r = MyThreadRun.t3; }
},"t2"); Thread t3 = new Thread(()->{ //打印 for(char c:a3) { while(r!=MyThreadRun.t3) { } System.out.print(Thread.currentThread().getName()+":"+c); r = MyThreadRun.t1; }
},"t3"); t1.start();
t2.start();
t3.start(); }
}
文章来源: blog.csdn.net,作者:mind_programmonkey,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Mind_programmonkey/article/details/116739463
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END