完成用一个线程负责打印1-26的数字,另一个线程负责打印A-Z 要求打印的结果为:1 2 A 3 4 B 5 6 C 7 8 D 9 10 1 12 ...  521 E F G H I J K ...  X Y Z

解决方案 »

  1.   

    没看懂,不过随便用个东西就能搞定,建议用ReentrantLock
      

  2.   

    你可以用sleep 方法实现。。sleep 方法就是控制线程。让线程睡眠。。
      

  3.   

    package com.anxin.utils;public class ThreadTest { private static boolean bShouldMain = false;
    public static void main(String[] args) {
    new Thread(new Runnable() {
    public void run() {
    synchronized (ThreadTest.class) {
    int m = 1;
    while (m <= 26) {
    if (bShouldMain) {
    try {
    ThreadTest.class.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.print(m++);
    System.out.print(m++);
    bShouldMain = true;;
    ThreadTest.class.notify(); }
    } }
    }).start();
    synchronized (ThreadTest.class) {
    char k = 'A';
    while (k <= 'Z') {
    if (!bShouldMain) {
    try {
    ThreadTest.class.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.print(k++);
    System.out.print(k++);
    bShouldMain = false;
    ThreadTest.class.notify();
    }
    } }}
      

  4.   

    这是我自己做的   用的jion()方法  但在控制1 2 A 3 4 B 5 6 C 7 8 D输出 出了问题,不是有规律的间隔,请帮我改进一下  谢了!
    package com.ambow.day10;public class TextThread  {

    public static void main(String[] args) {

     TreadTake th = new TreadTake();
     
     ThreadTake2 t = new ThreadTake2(th);

    th.start();
    t.start();
    }
    }
    class TreadTake extends Thread{


    public   void run(){
    for(int i=1;i<=26;i++){
    System.out.println(2*i-1);
    System.out.println(2*i);
    try {
    Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }class ThreadTake2 extends Thread{

    private Thread t;
    public  ThreadTake2(Thread t){
    this.t=t;
    }

    public void run(){
     for(char i='A';i<='Z';i++){
     System.out.println(i);
     if(i=='D'){
     try {
    t.join();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     }

     try {
    Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     }
    }
    }
      

  5.   

    回7楼
    在输出结果中前段必须是1 2 A 3 4 B 5 6 C 7 8 D这样的输出   后面的就是输出余下的数字后再输出字母(这个jion()方法一实现     我现在想做的就是怎么控制前面一段按这样de "1 2 A 3 4 B 5 6 C 7 8 D"输出