三个线程轮流输出ABC各十次(另附两个线程轮流循环若干次)

【三个线程轮流输出ABC各十次(另附两个线程轮流循环若干次)】三个线程轮A,B,C,轮流输出ABC,顺序不可打乱,各十次。
我写了两种方法,第一种太慢了就写了第二种,没啥好说的直接上代码:

package thread.li; /** * Created by Lee Y on 2016/4/6. * 三个线程轮番输出ABC */ public class ThreadABC { public static void main(String[] args){ A a = new A(); B b = new B(); C c = new C(); a.setB(b); b.setC(c); c.setA(a); a.start(); b.start(); c.start(); }} class A extends Thread{ public void setB(B b) { this.b = b; }private B b; public static boolean flag = false; private int sum=0; private int num=0; @Override public void run() { while(true){ if(flag == false && B.flag == false){ System.out.println("A"); flag=true; sum++; } num++; if(sum == 10){ System.out.println(num); break; } } } }class B extends Thread{ public void setC(C c) { this.c = c; }private C c; public static boolean flag = false; private int sum=0; private int num=0; @Override public void run(){ while(true){ if(A.flag == true){ System.out.println("B"); flag = true; A.flag = false; sum++; } num++; if(sum == 10){ System.out.println(num); break; } } } } class C extends Thread{ public void setA(A a) { this.a = a; }private A a; private int sum=0; private int num=0; @Override public void run() { while(true){ if(B.flag == true && A.flag == false){ System.out.println("C"); B.flag = false; sum++; } num++; if(sum == 10){ System.out.println(num); break; } } } }

第一种方法使用死循环写的,效率异常低下,试了几次都要循环一百多万次才会完成十次输出,简直无法让人忍受=。=
package thread.li; /** * Created by Lee Y on 2016/4/9. */ public class ThreadABC_high { public static int flag = 1; public static final ThreadABC_high abc = new ThreadABC_high(); public static void main(String[] args) { AA a = new AA(); BB b = new BB(); CC c = new CC(); a.start(); b.start(); c.start(); } synchronizedpublic void print(int i){ while(i != flag){ try{ wait(); }catch (InterruptedException e){ e.printStackTrace(); } } int c = 64+i; System.out.println((char) c); flag = flag == 3 ? 1 : flag+1; notifyAll(); } } class AA extends Thread{@Override public void run() { for (int i=0; i<10; i++){ ThreadABC_high.abc.print(1); } } } class BB extends Thread{@Override public void run() { for (int i=0; i<10; i++){ ThreadABC_high.abc.print(2); } } }class CC extends Thread{@Override public void run() { for (int i=0; i<10; i++){ ThreadABC_high.abc.print(3); } } }

第二种方法用了同步块和wait,natifyall方法,这也应该是正常的写法吧,烦人之处在于频繁的方法调用。
另附:和上面线程非常类似的一道题,也是考线程控制的熟练度的。主线程循环十次,子线程循环五次,再回主线程循环十次,再回子线程循环五次,以此类推共循环10次。
超级简单上代码:
package thread.li; /** * Created by Lee Y on 2016/4/16. */ public class ThreadCircle { private static boolean flag = true; public static final ThreadCircle circle = new ThreadCircle(); public static void main(String[] args) { new ThreadA().start(); for(int i=0; i<10; i++){ ThreadCircle.circle.circle(true); } } synchronized public void circle(boolean i){ if (i == flag){ try{ wait(); }catch (InterruptedException e){ e.printStackTrace(); } } int m = flag ? 10:5; String s = flag ? "主线程正在循环: ":"子线程正在循环: "; for(int j=1; j<=m; j++){ System.out.println(s + j); } flag = flag ? false:true; notify(); } } class ThreadA extends Thread{ @Override public void run() { for (int i=0; i<10; i++){ ThreadCircle.circle.circle(false); } } }

    推荐阅读