要求用線程順序打印A1B2C3…Z26
wait notify實現
package com.mashibing.juc.c_026_00_interview.A1B2C3;
public class T01_00_Question {
public static void main(String[] args) {
Object lock = new Object();
//要求用線程順序打印A1B2C3....Z26
new Thread(() -> {
synchronized (lock) {
char s = 'A';
try {
for (int i = 0; i < 26; i++) {
System.out.print(s++);
lock.notifyAll();
lock.wait();
}
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
synchronized (lock) {
int s = 1;
try {
for (int i = 0; i < 26; i++) {
System.out.print(s++);
lock.notifyAll();
lock.wait();
}
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
LockSupport park unpark實現
package com.mashibing.juc.c_026_00_interview.A1B2C3;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
public class T01_00_Question {
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
Object lock = new Object();
//要求用線程順序打印A1B2C3....Z26
t1 = new Thread(() -> {
char s = 'A';
for (int i = 0; i < 26; i++) {
System.out.print(s++);
LockSupport.unpark(t2);
LockSupport.park();
}
LockSupport.unpark(t2);
}, "t1");
t1.start();
t2 = new Thread(() -> {
int s = 1;
LockSupport.park();
for (int i = 0; i < 26; i++) {
System.out.print(s++);
LockSupport.unpark(t1);
LockSupport.park();
}
LockSupport.unpark(t1);
}, "t2");
t2.start();
}
}
CAS操作
package com.mashibing.juc.c_026_00_interview.A1B2C3;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
public class T01_00_Question {
enum ReadtoRun {T1, T2}
volatile static ReadtoRun r = ReadtoRun.T1;
public static void main(String[] args) {
new Thread(() -> {
char s = 'A';
for (int i = 0; i < 26; i++) {
while (r != ReadtoRun.T1) {
}
System.out.print(s);
s++;
r = ReadtoRun.T2;
}
}, "t1").start();
new Thread(() -> {
int s = 1;
for (int i = 0; i < 26; i++) {
while (r != ReadtoRun.T2) {
}
System.out.print(s);
s++;
r = ReadtoRun.T1;
}
}, "t1").start();
}
}
使用阻塞隊列實現
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class T01_00_Question {
static BlockingQueue<String> q1 = new ArrayBlockingQueue(1);
static BlockingQueue<String> q2 = new ArrayBlockingQueue(1);
public static void main(String[] args) {
try {
q1.put("ok");
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
char s = 'A';
for (int i = 0; i < 26; i++) {
try {
q1.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(s++);
try {
q2.put("ok");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
int s = 0;
for (int i = 0; i < 26; i++) {
try {
q2.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(s++);
try {
q1.put("ok");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
使用ReentranLock和Condition實現
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class T01_00_Question {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
new Thread(() -> {
char s = 'A';
try {
lock.lock();
for (int i = 0; i < 26; i++) {
System.out.print(s++);
condition2.signal();
try {
condition1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
condition2.signal();
}
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
int s = 1;
try {
lock.lock();
for (int i = 0; i < 26; i++) {
System.out.print(s++);
condition1.signal();
try {
condition2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
condition1.signal();
}
} finally {
lock.unlock();
}
}).start();
}
}
使用PipeString
效率低,試着玩玩
package com.mashibing.juc.c_026_00_interview.A1B2C3;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class T01_00_Question {
public static void main(String[] args) throws Exception {
PipedInputStream input1 = new PipedInputStream();
PipedInputStream input2 = new PipedInputStream();
PipedOutputStream output1 = new PipedOutputStream();
PipedOutputStream output2 = new PipedOutputStream();
input1.connect(output2);
input2.connect(output1);
String msg = "Your Turn";
new Thread(() -> {
byte[] buffer = new byte[9];
char s = 'A';
for (int i = 0; i < 2; i++) {
try {
input1.read(buffer);
if (new String(buffer).equals(msg)) {
System.out.print(s++);
}
output1.write(msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
byte[] buffer = new byte[9];
int s = 1;
for (int i = 0; i < 2; i++) {
try {
System.out.print(s++);
output2.write(msg.getBytes());
input2.read(buffer);
if (new String(buffer).equals(msg)) {
continue;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
使用預佔阻塞隊列實現LinkedTransferQueue
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
public class T01_00_Question {
public static void main(String[] args) throws Exception {
TransferQueue<Object> queue = new LinkedTransferQueue<Object>();
String msg = "Your Turn";
new Thread(() -> {
char s = 'A';
for (int i = 0; i < 26; i++) {
try {
queue.transfer(s++);
System.out.print(queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
int s = 1;
for (int i = 0; i < 26; i++) {
try {
System.out.print(queue.take());
queue.transfer(s++);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}