概述
作为程序员经常在面试的时候遇到多线程的问题,我印象比较深刻的就是下面这道题:写两个线程,一个线程打印 1~52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。看这个题目已经说得很清楚了,要用两个线程交替打印出12A....Z,我相信如每个线程单独打印的话肯定没问题,但是要交替打印,就需要两个线程互相合作,就需要通信。
代码实现
方法一
package com.cfxmn.springboot.springbootDemo.test; // 使用多线程打印12A34B.......5152Z
public class MainTest2 {
public static void main(String[] args) {
Object obj = new Object(); // 共享资源,实现线程间的通信
Thread printNum = new Thread(new PrintNum(obj));
Thread printChar = new Thread(new PrintChar(obj));
printNum.start();
printChar.start();
}
} // 打印数字
class PrintNum implements Runnable { private Object object; public PrintNum (Object obj) {
this.object = obj;
} @Override
public void run() {
synchronized (object) {
for (int i=1;i<=52;i++) {
System.out.print(i);
if (i % 2 == 0) {
object.notifyAll(); // 先唤醒所有线程
try {
object.wait(); // 阻塞当前线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} } // 打印字母
class PrintChar implements Runnable { private Object object; public PrintChar (Object obj) {
this.object = obj;
} @Override
public void run() {
synchronized (object) {
for (int i=65;i<=90;i++) {
System.out.print((char)i);
object.notifyAll(); // 先唤醒所有线程
try {
object.wait(); // 阻塞当前线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} }
运行结果:
12A34B56C78D910E1112F1314G1516H1718I1920J2122K2324L2526M2728N2930O3132P3334Q3536R3738S3940T4142U4344V4546W4748X4950Y5152Z
上面我是使用了wait,notifyAll,synchronized实现的,是不是还有其他的实现方法呢?
方法二
package com.cfxmn.springboot.springbootDemo.test; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; //使用多线程打印12A34B.......5152Z
public class MainTest3 { private Lock lock = new ReentrantLock();
private Condition printNum = lock.newCondition();
private Condition printchar = lock.newCondition(); public static void main(String[] args) {
MainTest3 test = new MainTest3();
Thread printNum = new Thread(test.new PrintNumt());
Thread printChar = new Thread(test.new PrintChart());
printChar.start();
printNum.start(); } //打印数字
class PrintNumt implements Runnable { @Override
public void run() { for (int i = 1; i <= 52; i++) {
lock.lock();
try {
printchar.signalAll();
System.out.print(i);
if (i % 2 == 0) {
printNum.await();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
} } //打印字母
class PrintChart implements Runnable { @Override
public void run() { for (int i = 0; i < 26; i++) {
lock.lock();
try {
printNum.signalAll();
System.out.print((char)(i + 'A'));
printchar.await();
} catch (Exception e) {
e.printStackTrace(); } finally {
lock.unlock();
}
} } }
}
这边使用了lock,和condition来实现,它的两个方法signalAll,await相当于notifyAll,wait,效果是一样的。