为爱奔跑


无论在家出家。必须上敬下和。忍人所不能忍。行人所不能行。代人之劳。成人之美。静坐长思己过。闲谈不论人非。行住坐卧。穿衣吃饭。从幕至朝。一名佛号。不令间断。或小声念。或默念。除念佛外。不起别念。若或妄念一起。当下就要教他消灭。当生惭愧心及忏悔心。从有修持。总觉我工夫很浅。不自矜夸。只管自家。不管人家。只看好样子。不看坏样子。看一切人都是菩萨。唯我一人是凡夫!

题目一:写两个线程,一个线程打印1~52,另一个线程打印字母A~Z。打印顺序为12A34B56C........5152Z。要求用线程间的通信。package test;
import java.lang.Thread;
class Printer{
    private int index = 1;
    public synchronized void print(int n){
        while(index%3==0){
            try{
                wait();
/*在其他线程调用此对象的notify方法钱,导致当前线程等待*/
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        System.out.print(index);
        index++;
        notifyAll();
    }
    public synchronized void print(char c){
        while(index%3!=0){
            try{
                wait();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        System.out.print(c);
        System.out.print(index);
        index++;
        notifyAll();
    }
}
class NumberPrinter extends Thread{
    private Printer p;
    public NumberPrinter(Printer p){
        this.p=p;
    }
    public void run(){
        for(int i=1;i<=52;i++)
            p.print(i);
    }
}
class CharPrinter extends Thread{
    private Printer p;
    public CharPrinter(Printer p){
        this.p=p;
    }
    public void run(){
        for(char c='A';c<='Z';c++)
            p.print(c);
    }
}
public class MyThread {
    public static void main(String args[]){
        Printer p = new Printer();
        Thread t1 = new NumberPrinter(p);
        Thread t2 = new CharPrinter(p);
        t1.start();
        t2.start();
    }
}

posted on 2014-12-11 22:04  RunforLove  阅读(631)  评论(0编辑  收藏  举报