用线程交换打印#和&10
最后的结果应该是这样的#&#&#&#&的形式  我用sleep()方法会作
但是我想用synchronized notify() 来做

解决方案 »

  1.   


    public class Test 

    public static void main(String args[]) 
    {
    final T t = new T();
    Thread t1 = new Thread(new Runnable()
    {
    public void run()
    {
    while(true)
    {
    t.print1();
    }
    }
    });
    Thread t2 = new Thread(new Runnable()
    {
    public void run()
    {
    while(true)
    {
    t.print2();
    }
    }
    });
    t1.start( );
    t2.start( );
    }

    }class T
    {
    boolean b = false;
    synchronized void print1()
    {
    if(b)
    {
    try
    {
    wait();
    } catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    System.out.println("#");
    b = true;
    notify();

    }

    synchronized void print2() 
    {
    if(!b)
    {
    try
    {
    wait();
    } catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    System.out.println("&");
    b=false;
    notify();
    }
    }
      

  2.   

    public class T { public static void main(String[] args) {
    Thread p1 = new Printer1();
    Thread p2 = new Printer2();
    p1.start();
    p2.start();
    } private static final T.LastPrintChar lock= new T.LastPrintChar();
    private static final int Max_Print_Count=15;//交替打印15个字符
    private static boolean running = true;
    static class LastPrintChar{
    char lastChar;
    int printCount=0;
    }
    static class Printer1 extends Thread{
    public void run() {
    while(running){
    synchronized (lock) {
    if(lock.lastChar=='#'){
    try {
    lock.wait();
    } catch (InterruptedException e) {}
    }
    System.out.print('#');
    lock.lastChar='#';
    lock.printCount++;
    if(Max_Print_Count<=lock.printCount)running=false;
    lock.notify();
    }
    }
    }

    }
    static class Printer2 extends Thread{
    public void run() {
    while(running){
    synchronized (lock) {
    if(lock.lastChar=='&'){
    try {
    lock.wait();
    } catch (InterruptedException e) {}
    }
    System.out.print('&');
    lock.lastChar='&';
    lock.printCount++;
    if(Max_Print_Count<=lock.printCount)running=false;
    lock.notify();
    }
    }
    }

    }
    }
      

  3.   

    public class MyThread {
        private static boolean flag = false;    public static void main(String[] args) {
            MyThread myThread = new MyThread();
            new Thread(new ThreadA(myThread)).start();
            new Thread(new ThreadB(myThread)).start();
        }    public synchronized void printA() throws Exception {
            while (true) {
                if (flag)
                    wait();
                System.out.println("#");
                flag = true;
                notifyAll();
            }
        }    public synchronized void printB() throws Exception {
            while (true) {
                if (!flag)
                    wait();
                System.out.println("&");
                flag = false;
                notifyAll();
            }
        }
    }class ThreadA implements Runnable {
        private static MyThread myThread;    public ThreadA(MyThread myThread) {
             this.myThread = myThread;
        }
        public void run() {
            try {
                myThread.printA();
            } catch (Exception e) {
            }
        }
    }class ThreadB implements Runnable {
        private static MyThread myThread = new MyThread();    public ThreadB(MyThread myThread){
             this.myThread = myThread;
        }
        
        public void run() {
            try {
                myThread.printB();
            } catch (Exception e) {
            }
        }
    }