?
public class PrintABC {
??? public static Boolean isThreadA = true;
??? public static Boolean isThreadB = false;
??? public static Boolean isThreadC = false;
??? public static void main(String[] args) {
??? ??? final PrintABC abc = new PrintABC();
??? ??? new Thread(new Runnable() {
??? ??? ??? public void run() {
??? ??? ??? ??? for (int i = 0; i < 10; i++) {
??? ??? ??? ??? ??? synchronized (abc) {
??? ??? ??? ??? ??? ??? while(!isThreadA) {
??? ??? ??? ??? ??? ??? ??? try {
??? ??? ??? ??? ??? ??? ??? ??? abc.wait();
??? ??? ??? ??? ??? ??? ??? } catch (InterruptedException e) {
??? ??? ??? ??? ??? ??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? ??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? ??? System.out.print("A");
??? ??? ??? ??? ??? ??? ??? isThreadA = false;
??? ??? ??? ??? ??? ??? ??? isThreadB = true;
??? ??? ??? ??? ??? ??? ??? isThreadC = false;
??? ??? ??? ??? ??? ??? ??? abc.notifyAll();
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }).start();
??? ??? new Thread(new Runnable() {
??? ??? ??? public void run() {
??? ??? ??? ??? for (int i = 0; i < 10; i++) {
??? ??? ??? ??? ??? synchronized (abc) {
??? ??? ??? ??? ??? ??? while(!isThreadB) {
??? ??? ??? ??? ??? ??? ??? try {
??? ??? ??? ??? ??? ??? ??? ??? abc.wait();
??? ??? ??? ??? ??? ??? ??? } catch (InterruptedException e) {
??? ??? ??? ??? ??? ??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? ??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? ??? System.out.print("B");
??? ??? ??? ??? ??? ??? ??? isThreadA = false;
??? ??? ??? ??? ??? ??? ??? isThreadB = false;
??? ??? ??? ??? ??? ??? ??? isThreadC = true;
??? ??? ??? ??? ??? ??? ??? abc.notifyAll();
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }).start();
??? ???
??? ??? new Thread(new Runnable() {
??? ??? ??? public void run() {
??? ??? ??? ??? for (int i = 0; i < 10; i++) {
??? ??? ??? ??? ??? synchronized (abc) {
??? ??? ??? ??? ??? ??? while(!isThreadC) {
??? ??? ??? ??? ??? ??? ??? try {
??? ??? ??? ??? ??? ??? ??? ??? abc.wait();
??? ??? ??? ??? ??? ??? ??? } catch (InterruptedException e) {
??? ??? ??? ??? ??? ??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? ??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? ??? System.out.print("C");
??? ??? ??? ??? ??? ??? ??? isThreadA = true;
??? ??? ??? ??? ??? ??? ??? isThreadB = false;
??? ??? ??? ??? ??? ??? ??? isThreadC = false;
??? ??? ??? ??? ??? ??? ??? abc.notifyAll();
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }).start();
??? }
}
输出结果如下?:ABCABCABCABCABC
思路:解题思路大概是这样的,开启三个线程,每个线程一次打印一个字母,并且按照一定 的顺序打印,当打印A的时候,其他线程处于阻塞状态,打印完A以后,将线程解锁,让打印B的那个线程开启,其他线程处于阻塞状态,同理打印C的时候,阻塞 其他线程,这三个线程顺序循环,就达到顺序多次打印ABC的目的了。
这道题看似思路简单,其实主要需要用到wait()方法和notify()方法,还有关键字synchronized,只有充分理解了这些,才能解出这道题。下面我有必要讲解一下这两个方法,还有关键字synchronized。
?
在 JAVA中,是没有类似于PV操作、进程互斥等相关的方法的。JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的 synchronized()方法类似于操作系统概念中的互斥内存块,在JAVA中的Object类型中,都是带有一个内存锁的,在有线程获取该内存锁 后,其它线程无法访问该内存,从而实现JAVA中简单的同步、互斥操作。明白这个原理,就能理解为什么synchronized(this)与 synchronized(static XXX)的区别了,synchronized就是针对内存区块申请内存锁,this关键字代表类的一个对象,所以其内存锁是针对相同对象的互斥操作,而 static成员属于类专有,其内存空间为该类所有成员共有,这就导致synchronized()对static成员加锁,相当于对类加锁,也就是在该 类的所有成员间实现互斥,在同一时间只有一个线程可访问该类的实例。如果只是简单的想要实现在JAVA中的线程互斥,明白这些基本就已经够了。但如果需要 在线程间相互唤醒的话就需要借助Object.wait(), Object.nofity()了。
??? Obj.wait(),与Obj.notify()必须要与synchronized(Obj)一起使用,也就是wait,与notify是针对已经获取 了Obj锁进行操作,从语法角度来说就是Obj.wait(),Obj.notify必须在synchronized(Obj){...}语句块内。从功 能上来说wait就是说线程在获取对象锁后,主动释放对象锁,同时本线程休眠。直到有其它线程调用对象的notify()唤醒该线程,才能继续获取对象 锁,并继续执行。相应的notify()就是对对象锁的唤醒操作。但有一点需要注意的是notify()调用后,并不是马上就释放对象锁的,而是在相应的 synchronized(){}语句块执行结束,自动释放锁后,JVM会在wait()对象锁的线程中随机选取一线程,赋予其对象锁,唤醒线程,继续执 行。这样就提供了在线程间同步、唤醒的操作。Thread.sleep()与Object.wait()二者都可以暂停当前线程,释放CPU控制权,主要 的区别在于Object.wait()在释放CPU同时,释放了对象锁的控制。理解了这些解决这道面试题应该就不成问题了