说明:本博客仅为个人学习笔记,无任何商业用途。
题目
建立两个线程循环输出两个数组中的内容。如A1B2C3D4E5…
LockSupport实现
public class LockSupportDemo {
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
Runnable r1 = ()->{
for (char c : chars){
System.out.print(c);
LockSupport.unpark(t2);
LockSupport.park();
}
};
Runnable r2 = ()->{
for (int c : arr){
LockSupport.park();
System.out.print(c);
LockSupport.unpark(t1);
}
};
t1 = new Thread(r1,"t1");
t2 = new Thread(r2,"t2");
t1.start();
t2.start();
}
}
synchronize实现
public class SynchronizedArr {
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
final Object lock = new Object();
new Thread(() -> {
synchronized (lock) {
for (int c : arr) {
System.out.print(c);
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//这个需要吗,为什么?
lock.notify();
}
}, "t2").start();
new Thread(() -> {
synchronized (lock) {
for (char c : chars) {
System.out.print(c);
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "t1").start();
}
}
public class SynchronizedArrState {
final static Object lock = new Object();
private static int state = 0;
final static char [] chars = {'A','B','C','D','E'};
final static int [] arr = {1,2,3,4,5};
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
Runnable r1 = ()->{
synchronized (lock){
for(char c : chars){
while (state %2 != 0){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(c);
state++;
lock.notify();
}
//这个需要吗,为什么?
lock.notify();
}
};
Runnable r2 = ()->{
synchronized (lock){
for(int c : arr){
while (state %2 == 0){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(c);
state++;
lock.notify();
}
}
};
t1 = new Thread(r1,"t1");
t2 = new Thread(r2,"t2");
t2.start();
t1.start();
}
}
Lock实现
public class OneCondition {
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(()->{
try{
lock.lock();
for (char c : chars) {
System.out.print(c);
condition.signal();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
condition.signal();
}finally {
lock.unlock();
}
},"t1").start();
new Thread(()->{
try{
lock.lock();
for (int c : arr) {
System.out.print(c);
condition.signal();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}finally {
lock.unlock();
}
},"t2").start();
}
}
public class TwoCondition {
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
ReentrantLock lock = new ReentrantLock();
Condition ch = lock.newCondition();
Condition in = lock.newCondition();
new Thread(()->{
try{
lock.lock();
for (char c : chars) {
System.out.print(c);
in.signal(); //唤醒数字线程
try {
ch.await(); //阻塞当前字符线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//必须需要 应为 t2 还在阻塞状态
in.signal();
}finally {
lock.unlock();
}
},"t1").start();
new Thread(()->{
try{
lock.lock();
for (int c : arr) {
System.out.print(c);
ch.signal(); //唤醒字符线程
try {
in.await(); //阻塞当前数字线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}finally {
lock.unlock();
}
},"t2").start();
}
}
semaphore实现
public class SemaphoreDemo {
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
Semaphore a1 = new Semaphore(1);
//注意这里
Semaphore a2 = new Semaphore(0);
new Thread(()->{
try {
for (char c : chars){
a1.acquire(1);
System.out.print(c);
a2.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{
try {
for (int c : arr){
a2.acquire(1);
System.out.print(c);
a1.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t2").start();
}
}
CycliBarrier实现
public class CycliBarrierDemo {
public static void main(String[] args) {
CyclicBarrier c1 = new CyclicBarrier(2);
CyclicBarrier c2 = new CyclicBarrier(2);
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
new Thread(()->{
try {
for (char c : chars){
//注意这里区别
System.out.print(c);
c1.await();
c2.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{
try {
for (int c : arr){
c1.await();
System.out.print(c);
c2.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
},"t2").start();
}
}
枚举+自旋
public class EnumSpin {
enum Mlock{thread1,thread2}
private static volatile Mlock flag = Mlock.thread1;
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
new Thread(()->{
for (char c : chars){
while(flag != Mlock.thread1){
//进行自旋
}
System.out.print(c);
flag = Mlock.thread2;
}
},"t1").start();
new Thread(()->{
for (int c : arr){
while(flag != Mlock.thread2){
//进行自旋
}
System.out.print(c);
flag = Mlock.thread1;
}
},"t2").start();
}
}
管道流
public class PipedStreamDemo {
public static void main(String[] args) throws IOException {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
PipedInputStream input1 = new PipedInputStream();
PipedInputStream input2 = new PipedInputStream();
PipedOutputStream output1 = new PipedOutputStream();
PipedOutputStream output2 = new PipedOutputStream();
input1.connect(output2);
input2.connect(output1);
String turn = "you!";
new Thread(()->{
byte[] buffer = new byte[4];
try {
for(char c : chars){
input1.read(buffer);
if(new String(buffer).equals(turn)){
System.out.print(c);
}
output1.write(turn.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{
byte[] buffer = new byte[4];
try {
for(int c : arr){
System.out.print(c);
output2.write(turn.getBytes());
input2.read(buffer);
if(new String(buffer).equals(turn)){
continue;
}
}
} catch (IOException e) {
e.printStackTrace();
}
},"t1").start();
}
}
TransferQueue
public class TransferQueueDemo {
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
char[] arr = {'1', '2', '3', '4', '5'};
TransferQueue<Character> queue = new LinkedTransferQueue<>();
new Thread(()->{
try {
for(char c : chars){
System.out.print(queue.take());
queue.transfer(c);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{
try {
for(char c : arr){
queue.transfer(c);
System.out.print(queue.take());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t2").start();
}
}
BlockingQueue实现
take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止
put(anObject):把anObject加到BlockingQueue里,如果BlockQueue没有空间,则调用此方法的线程被阻断直到BlockingQueue里面有空间再继续.
public class BlockingQueueDemo {
static BlockingQueue<String> q1 = new ArrayBlockingQueue<>(1);
static BlockingQueue<String> q2 = new ArrayBlockingQueue<>(1);
public static void main(String[] args) {
char[] chars = {'A', 'B', 'C', 'D', 'E'};
int[] arr = {1, 2, 3, 4, 5};
new Thread(()->{
for (char c : chars){
System.out.print(c);
try{
q1.put("ok");
q2.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t1").start();
new Thread(()->{
for (int c : arr){
try{
q1.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(c);
try{
q2.put("ok");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"t2").start();
}
}
思考
1、这些方式那几种对线程的启动顺序没有要求?
2、synchronized中的两种方式差异是什么,为什么?
3、CycliBarrier实现中线程1可以改成下面的顺序吗?
___ 1)c1.await();
___ 2)System.out.print©;
____ 3)c2.await();
4、 枚举自旋中Mlock flag 需要volatile修饰吗?为什么
请评论区留言吧。