Java中多執行緒交替列印
阿新 • • 發佈:2018-12-31
一、兩執行緒交替列印數字和字母
要求控制檯中輸出的內容為 A1B2C3D4
這應該是多執行緒面試題中推簡單的筆試題了吧;
1.Object. notify()和Object.wait()實現;
private void init (){ char[] c = new char[26]; int[] num = new int[26]; for (int i = 0; i < 26; i++) { c[i ] = (char) (i+65); num[i ] = i ; } PrintChar printChar = new PrintChar(c); printChar.start(); PrintNum printNum = new PrintNum(num); printNum.start(); } class PrintChar extends Thread { private char[] c; public PrintChar(char[] c) { this.c = c; } @Override public void run() { super.run(); for (char s : c) { synchronized (cLock) { cLock.notify(); try { cLock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(s); } } } } class PrintNum extends Thread { private int[] num; public PrintNum(int[] num) { this.num = num; } @Override public void run() { super.run(); for (int i : num) { synchronized (cLock) { cLock.notify(); try { cLock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i); } } } }
2. ReentrantLock和Condition 實現
private void init (){ Lock lock = new ReentrantLock(); Condition cCondition = lock.newCondition(); Condition nCondition = lock.newCondition(); char[] c = new char[26]; int[] num = new int[26]; for (int i = 0; i < 26; i++) { c[i ] = (char) (i+65); num[i ] = i ; } PrintChar printChar = new PrintChar(c, lock, cCondition, nCondition); printChar.start(); PrintNum printNum = new PrintNum(num, lock, cCondition, nCondition); printNum.start(); } class PrintChar extends Thread { private char[] c; private Condition cCondition, nCondition; private Lock lock; public PrintChar(char[] c, Lock lock, Condition cCondition, Condition nCondition) { this.c = c; this.nCondition = nCondition; this.cCondition = cCondition; this.lock = lock; } @Override public void run() { super.run(); for (char s : c) { System.out.println(s); lock.lock(); try { nCondition.signal(); cCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } try { lock.lock(); nCondition.signal(); }finally { lock.unlock(); } } } } class PrintNum extends Thread { private int[] num; private Condition cCondition, nCondition; private Lock lock; public PrintNum(int[] num, Lock lock, Condition cCondition, Condition nCondition) { this.num = num; this.nCondition = nCondition; this.cCondition = cCondition; this.lock = lock; } @Override public void run() { super.run(); for (int i : num) { System.out.println(i); try { lock.lock(); cCondition.signal(); nCondition.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } try { lock.lock(); nCondition.signal(); }finally { lock.unlock(); } }
3.LockSupport實現
public init() { char[] c = new char[26]; int[] num = new int[26]; for (int i = 0; i < 26; i++) { c[i ] = (char) (i+65); num[i] = i ; } PrintChar printChar = new PrintChar(c); PrintNum printNum = new PrintNum(num); printChar.setThread(printNum); printNum.setThread(printChar); printChar.start(); printNum.start(); } class PrintChar extends Thread { private char[] c; private Thread thread; public void setThread(Thread thread) { this.thread = thread; } public PrintChar(char[] c) { this.c = c; } @Override public void run() { super.run(); for (char s : c) { System.out.println(s); LockSupport.unpark(this.thread); LockSupport.park(); } LockSupport.unpark(this.thread); } } class PrintNum extends Thread { private int[] num; private Thread thread; public void setThread(Thread thread) { this.thread = thread; } public PrintNum(int[] num) { this.num = num; } @Override public void run() { super.run(); LockSupport.park(); for (int i : num) { System.out.println(i); LockSupport.unpark(this.thread); LockSupport.park(); } LockSupport.unpark(this.thread); } }
4.阻塞佇列實現
public init() {
char[] c = new char[26];
int[] num = new int[26];
for (int i = 0; i < 26; i++) {
c[i ] = (char) (i+65);
num[i] = i ;
}
BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);
PrintChar printChar = new PrintChar(c,queue);
PrintNum printNum = new PrintNum(num,queue);
printChar.start();
printNum.start();
}
class PrintChar extends Thread {
private char[] c;
private BlockingQueue<String> queue;
public PrintChar(char[] c,BlockingQueue<String> queue) {
this.c = c;
this.queue = queue;
}
@Override
public void run() {
super.run();
for (char s : c) {
try {
queue.put("--");
queue.put("--");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(s);
}
}
}
class PrintNum extends Thread {
private int[] num;
private BlockingQueue<String> queue;
public PrintNum(int[] num,BlockingQueue<String> queue) {
this.num = num;
this.queue=queue;
}
@Override
public void run() {
super.run();
for (int i : num) {
try {
queue.take();
queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}