1. 程式人生 > >java多執行緒交替列印數字和字母

java多執行緒交替列印數字和字母

題目:

1.開啟兩個執行緒,一個執行緒列印A~Z,另一個執行緒列印1~52的資料。

2.實現交替列印,輸出結果為12A34B………..5152Z.

3.請用多執行緒方式實現。

使用阻塞佇列實現

class NumberPrinter extends Thread{
    private BlockingQueue<String> bp;
    public NumberPrinter(BlockingQueue<String> bp) {
        super();
        this.bp = bp;
    }
    @Override
    public
void run() { super.run(); for(int i=1;i<53;i++){ try { System.out.print(i); bp.put((i)+""+(i+1)); Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } } class LetterPrinter extends Thread{ private
BlockingQueue<String> bp; public LetterPrinter(BlockingQueue<String> bp) { super(); this.bp = bp; } @Override public void run() { super.run(); char ch ='A'; for (int i = 0; i < 26; i++) { try { bp.take(); bp.take(); System.out.print((char
) (ch + i)); Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class T16{ private final Lock lock = new ReentrantLock(); private final Condition con = lock.newCondition(); public static void main(String[] args){ BlockingQueue<String> bp = new ArrayBlockingQueue<>(2); LetterPrinter t1= new LetterPrinter(bp); NumberPrinter t2 = new NumberPrinter(bp); t1.start(); t2.start(); } }

使用原子變數

public class T16_2 {
    public static AtomicInteger countAtomicInteger=new AtomicInteger(0);

    /**
     * @param args
     */
    public static void main(String[] args) {
        // 列印字母
        Thread t1 = new Thread(){

            @Override
            public void run() {
                int i=0;
                char ch = 'A';
                while(true){
                    if(i>=26){return;}
                    if(countAtomicInteger.get()==2 && i<26){
                        System.out.print((char)(ch+i));
                        countAtomicInteger.set(0);
                        i++;
                    }
                }
            }
        };
        // 列印數字
        Thread t2 = new Thread(){

            @Override
            public void run() {
                int i=1;
                while(true){
                    if(i>52)return;
                    if(countAtomicInteger.get()<2 && i<=52){
                        countAtomicInteger.incrementAndGet();
                        System.out.print(i);
                        i++;
                    }
                }
            }
        };
        t1.start();
        t2.start();
    }

}

使用Condition

public class T16_3 {

    static boolean letterFlag=true;
    static boolean numFlag=true;
    static Lock lock = new ReentrantLock();
    static Condition letterCondition = lock.newCondition();
    static Condition numCondition = lock.newCondition();
    static boolean flag=true;
    /**
     * @param args
     */
    public static void main(String[] args) {

        new Thread(new Runnable(){
            @Override
            public void run() {
                char ch = 'A';
                int i=0;
                while(i<26){
                    lock.lock();
                    try{
                        if(flag){
                            letterCondition.await();
                        }
                        System.out.print((char)(ch+i));
                        i++;
                        flag=true;
                        numCondition.signal();
                    }catch(Exception e){
                        e.printStackTrace();
                    }finally{
                        lock.unlock();
                    }
                }
            }
        }).start();

        //列印數字的執行緒
        new Thread(new Runnable(){
            @Override
            public void run() {
                int i=1;
                while(i<53){
                    lock.lock();
                    try{
                        if(!flag){
                            numCondition.await();
                        }
                        System.out.print(i+""+(i+1));
                        i+=2;
                        flag=false;
                        letterCondition.signal();
                    }catch(Exception e){
                        e.printStackTrace();
                    }finally{
                        lock.unlock();
                    }
                }
            }
        }).start();

    }
}