问题:有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC(我没接触过线程,但问题的解答方式又很急)

解决方案 »

  1.   

    public class PrintABC { public static void main(String[] args) {
    Token token = new Token();
    Thread thA = new Thread(new PrintThread('A', token));
    Thread thB = new Thread(new PrintThread('B', token));
    Thread thC = new Thread(new PrintThread('C', token));
    thA.start();
    thB.start();
    thC.start();
    }}class Token {
    private char _pos = 'A'; public void rotate() {
    _pos++;
    if (_pos >= 'D') {
    _pos = 'A';
    }
    } public char curPos() {
    return _pos;
    }
    }class PrintThread implements Runnable {
    public static final int _MAX = 10;
    private char _name;
    private Token _token; public PrintThread(char name, Token token) {
    _name = name;
    _token = token;
    } public char getName() {
    return _name;
    } public void run() {
    for (int i = 0; i < _MAX; ++i) {
    synchronized (_token) {
    while (_token.curPos() != getName()) {
    try {
    _token.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println(getName());
    _token.rotate();
    _token.notifyAll();
    }
    }
    }
    }
    貌似能行:-)
      

  2.   


    之前做过这题 测试通过
    http://topic.csdn.net/u/20100801/12/e5265ef6-eaed-4cf7-a524-1e5592fa5967.html
      

  3.   


    public class Print {

    public void init(){
    printThread A = new printThread();
    A.start();
    printThread B = new printThread();
    B.start();
    printThread C = new printThread();
    C.start();
    }

    public static void main(String[] args){
    new Print().init();


    }

    class printThread extends Thread{
     
    public void run(){
    for(int i = 0 ; i<10 ; i++){
    System.out.print("ABCABC");
    }
    System.out.println();
    }
    } }