有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…  个人觉得:用到了sleep()不是好方法,打印出来了不一定就是正确的因为多线程的调度我们不可精细控制。ABC是线程的name,打印的时候用到Thread.getName();要怎么写呢??

解决方案 »

  1.   

    package csdn;/** 
     * 顺序 打印ABC 
     * @author Administrator 
     * 
     */
    public class ThreadPrint {    public static void main(String[] args) {
            A a = new A();        for (int i = 0; i < 10; i++) {
                MyThread mt1 = new MyThread(a);
                mt1.start();
            }
        }
    }class A {    private char[] c = {'A', 'B', 'C'};
        private int index;    public char[] getC() {
            return c;
        }    public synchronized void execute() {        if (index >= c.length) {
                index = 0;
            } else {
                System.out.println(c[index++]);
            }
        }
    }class MyThread extends Thread {    private A a;    public MyThread(A a) {
            this.a = a;
        }    public void run() {
            synchronized (a) {
                int index = a.getC().length;
                for (int i = 0; i <= index; i++) {
                    a.execute();
                }
                System.out.println("===============");
            }
        }
    }
      

  2.   


    请问锁住notify(),与wait()方法的对象必须是同一个对象吗?
      

  3.   


    package algorithm;public class Abc implements Runnable{

    //构造函数
    public Abc() {
    new Thread(new A()).start();
    new Thread(new B()).start();
    new Thread(new C()).start();
    }

    //自身线程
    @Override
    public void run() {
    while(true) {
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    synchronized(this) {
    notifyAll();
    }
    }
    }

    //sync
    public synchronized void print(Object obj) {
    System.out.println(obj.getClass().getName());
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } //线程A
    public class A implements Runnable { @Override
    public void run() {
    while (true) {
    print(this);
    }
    }

    }

    //线程B
    public class B implements Runnable {

    @Override
    public void run() {
    while (true) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    print(this);
    }
    }

    }

    //线程C
    public class C implements Runnable { @Override
    public void run() {
    while (true) {
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    print(this);
    }
    }

    }

    //main
    public static void main(String[] args) {
    new Thread(new Abc()).start();
    }}
    随便写的,解决的可能不是很优雅。这是无数次的,至于10次的自己改吧...
      

  4.   


    package Csdn;import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.TimeUnit; class AChar {

    public enum Status{ A,B,C};
    private Status status=Status.A;
    public void B(){status=Status.B;};
    public void C(){status=Status.C;};
    public Status getStatus(){return status;}
    }class CharQueue extends LinkedBlockingQueue<AChar>{

    }class  StatusA implements Runnable{
    private CharQueue queueA;
    private int count=10;
    StatusA(CharQueue queueA){
    this.queueA=queueA;
    } @Override
    public void run() {
       while(count>0){
       
        try {
       queueA.add(new AChar());
       count--;
    TimeUnit.MILLISECONDS.sleep(1000);
    //此处睡眠防止 A连续生产10个
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }

    }
    }
    class   StatusB implements Runnable{
    private CharQueue queueA;
    private CharQueue queueB;
    private int count=10;
    StatusB(CharQueue queueA,CharQueue queueB){
    this.queueA=queueA;
    this.queueB=queueB;
    } @Override
    public void run() {
       while(count>0){
      
    try { 
    AChar aChar;
    aChar = queueA.take();
       System.out.print(aChar.getStatus());
       aChar.B();           
       queueB.add(aChar);
       count--;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }    
       }

    }
    }
    class   StatusC implements Runnable{
    private CharQueue queueC;
    private CharQueue queueB;
    private int count=10;
    StatusC(CharQueue queueB,CharQueue queueC){
    this.queueC=queueC;
    this.queueB=queueB;
    } @Override
    public void run() {
       while(count>0){
      try {
       AChar aChar;
       aChar = queueB.take();
       System.out.print(aChar.getStatus());
       aChar.C();           
       queueC.add(aChar);
       count--;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }

    }
    }
    class   Finish implements Runnable{
    private CharQueue queueC;
    private int count=10;
    Finish(CharQueue queueC){
    this.queueC=queueC;
    } @Override
    public void run() {
       while(count>0){
      try {
       AChar aChar;
       aChar = queueC.take();
       System.out.print(aChar.getStatus());
       count--;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }

    }
    }


    public  class Csdn{
    public static void main(String [] args){
    CharQueue queueA=new CharQueue();
    CharQueue queueB=new CharQueue();
    CharQueue queueC=new CharQueue();
    ExecutorService exec=Executors.newCachedThreadPool();
    exec.execute(new StatusA(queueA));
    exec.execute(new StatusB(queueA,queueB));
    exec.execute(new StatusC(queueB,queueC));
    exec.execute(new Finish(queueC));


    }
    }

    用生产者消费者队列做的。。哈哈还算优雅把
      

  5.   




    package Csdn;import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.TimeUnit; class AChar {

    public enum Status{ A,B,C};
    private Status status=Status.A;
    public void B(){status=Status.B;};
    public void C(){status=Status.C;};
    public Status getStatus(){return status;}
    }class CharQueue extends LinkedBlockingQueue<AChar>{

    }class  StatusA implements Runnable{
    private CharQueue queueA;
    private int count=10;
    StatusA(CharQueue queueA){
    this.queueA=queueA;
    } @Override
    public void run() {
       while(count>0){
       
        try {
       queueA.add(new AChar());
       count--;
    TimeUnit.MILLISECONDS.sleep(1000);
    //此处睡眠防止 A连续生产10个
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }

    }
    }
    class   StatusB implements Runnable{
    private CharQueue queueA;
    private CharQueue queueB;
    private int count=10;
    StatusB(CharQueue queueA,CharQueue queueB){
    this.queueA=queueA;
    this.queueB=queueB;
    } @Override
    public void run() {
       while(count>0){
      
    try { 
    AChar aChar;
    aChar = queueA.take();
       System.out.print(aChar.getStatus());
       aChar.B();           
       queueB.add(aChar);
       count--;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }    
       }

    }
    }
    class   StatusC implements Runnable{
    private CharQueue queueC;
    private CharQueue queueB;
    private int count=10;
    StatusC(CharQueue queueB,CharQueue queueC){
    this.queueC=queueC;
    this.queueB=queueB;
    } @Override
    public void run() {
       while(count>0){
      try {
       AChar aChar;
       aChar = queueB.take();
       System.out.print(aChar.getStatus());
       aChar.C();           
       queueC.add(aChar);
       count--;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }

    }
    }
    class   Finish implements Runnable{
    private CharQueue queueC;
    private int count=10;
    Finish(CharQueue queueC){
    this.queueC=queueC;
    } @Override
    public void run() {
       while(count>0){
      try {
       AChar aChar;
       aChar = queueC.take();
       System.out.print(aChar.getStatus());
       count--;
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       }

    }
    }


    public  class Csdn{
    public static void main(String [] args){
    CharQueue queueA=new CharQueue();
    CharQueue queueB=new CharQueue();
    CharQueue queueC=new CharQueue();
    ExecutorService exec=Executors.newCachedThreadPool();
    exec.execute(new StatusA(queueA));
    exec.execute(new StatusB(queueA,queueB));
    exec.execute(new StatusC(queueB,queueC));
    exec.execute(new Finish(queueC));


    }
    }
      

  6.   


    这么多回的我就不写了 哈哈哈哈哈
    推荐一个强的 http://hi.baidu.com/dapplehou/blog/item/14e62834ebe1bc235ab5f504.html
      

  7.   

    哈 差不多 ~貌似 我用BlockingQueue写的 它那没有哦
      

  8.   

    这个貌似会a! 将三个线程都加上同步锁就OK 了!