本帖最后由 jemn800923 于 2010-05-15 21:13:01 编辑

解决方案 »

  1.   

    http://dev.csdn.net/article/49/49650.shtm
      

  2.   

    public class ThreadTest {    private int count;
        private Object lock = new Object();    public ThreadTest(int count) {
            this.count = count;
        }    public static void main(String[] args) {
            ThreadTest test = new ThreadTest(5);
            new ThreadA(test).start();
            new ThreadA(test).start();
            new Thread(new ThreadB(test)).start();
            new Thread(new ThreadB(test)).start();
            test.print();
        }    public void print() {
            try {
                String name = Thread.currentThread().getName();
                for(int i = 0; i < count; i++) {
                    synchronized (lock) {
                        System.out.println((i + 1) + ". " + name);
                        lock.notify();                    
                        if(i < count - 1) {
                            lock.wait();
                        }
                        Thread.sleep(200);
                    }
                }
                System.out.println(name + " OVER");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }    private static class ThreadA extends Thread {        private ThreadTest test;        public ThreadA(ThreadTest test) {
                this.test = test;
            }        public void run() {
                test.print();
            }
        }    private static class ThreadB implements Runnable {        private ThreadTest test;        public ThreadB(ThreadTest test) {
                this.test = test;
            }        public void run() {
                test.print();
            }
        }
    }
      

  3.   

    public class MainThread implements Runnable{
    String name;
    public MainThread(String name){
    this.name=name;

    }
    public void run(){
    for(int i=0;i<5;i++){
    System.out.println(name);
    try{
    Thread.sleep(1000);
    }catch(InterruptedException e){

    }
    }
    }
    public static void main(String args[]){
    AnyThread1 t1=new AnyThread1("t1");
    AnyThread2 t2=new AnyThread2("t2");
    MainThread t3=new MainThread("t3");
    t1.start();
    new Thread(t2).start();
    new Thread(t3).start();

    }

    }
    class AnyThread1 extends Thread{
    public AnyThread1(String name){
    super(name);
    }
    public void run(){

    for(int i=0;i<5;i++){
    System.out.println(this.getName());
    try{
    Thread.sleep(1000);
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }
    }

    }class AnyThread2 implements Runnable{
    String name;
    public AnyThread2(String name){ this.name=name;
    }

    public void run(){
    for(int i=0;i<5;i++){
    System.out.println(name);
    try{
    Thread.sleep(1000);
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }
    }

    }
      

  4.   

    public class ThreadTest {    private int count;
        private Object lock = new Object();    public ThreadTest(int count) {
            this.count = count;
        }    public static void main(String[] args) {
            ThreadTest test = new ThreadTest(5);
            new ThreadA(test).start();
            new ThreadA(test).start();
            new Thread(new ThreadB(test)).start();
            new Thread(new ThreadB(test)).start();
            test.print();
        }    public void print() {
            try {
                String name = Thread.currentThread().getName();
                for(int i = 0; i < count; i++) {
                    synchronized (lock) {
                        System.out.println((i + 1) + ". " + name);
                        lock.notify();                    
                        if(i < count - 1) {
                            lock.wait();
                        }
                        Thread.sleep(200);
                    }
                }
                System.out.println(name + " OVER");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }    private static class ThreadA extends Thread {        private ThreadTest test;        public ThreadA(ThreadTest test) {
                this.test = test;
            }        public void run() {
                test.print();
            }
        }    private static class ThreadB implements Runnable {        private ThreadTest test;        public ThreadB(ThreadTest test) {
                this.test = test;
            }        public void run() {
                test.print();
            }
        }
    }
      

  5.   

    public class ThreadTest {//    private int count;
        private Object lock = new Object();    public ThreadTest(int count) {//只有继承了Thread的才能够用super();方法
            this.count = count;
        }    public static void main(String[] args) {
            ThreadTest test = new ThreadTest(5);//一个类弄出四个线程来
            new ThreadA(test).start();
            new ThreadA(test).start();
            new Thread(new ThreadB(test)).start();
            new Thread(new ThreadB(test)).start();//Threadpublic Thread(Runnable target)分配新的 Thread 对象。
    //这种构造方法与 Thread(null, target,gname) 具有相同的作用,
    //其中的 gname 是一个新生成的名称。自动生成的名称的形式为 “Thread-”+n,其中的 n 为整数。
            test.print();
        }    public void print() {
            try {
                String name = Thread.currentThread().getName();
                for(int i = 0; i < count; i++) {
                    synchronized (lock) {
                        System.out.println((i + 1) + ". " + name);
                        System.out.println(name);
                        lock.notify();                    
                        if(i < count - 1) {
                            lock.wait();
                        }
                        Thread.sleep(200);
                    }
                }
                System.out.println(name + " OVER");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }    private static class ThreadA extends Thread {        private ThreadTest test;        public ThreadA(ThreadTest test) {
                this.test = test;
            }        public void run() {
                test.print();
            }
        }    private static class ThreadB implements Runnable {        private ThreadTest test;        public ThreadB(ThreadTest test) {
                this.test = test;
            }        public void run() {
                test.print();
            }
        }
    }