多线程打印:写两个线程,一个线程打印 1~52,另一个线程打印字母A-Z。 打印顺序为12A34B56C……5152Z。

方法1: wait,notify, synchronized

package com.hnist.lzn.thread;

public class PrintThread {
    /*
    写两个线程,一个线程打印 1~52,另一个线程打印字母A-Z。
    打印顺序为12A34B56C……5152Z。
     */
    static boolean index = true;

    static String str = "A";
    static class A implements Runnable{
        int i  = 1;
        @Override
        public void run() {

            synchronized (str){

                while(i <= 52){

                    if(index){

                        System.out.println(i++);
                        System.out.println(i++);
                        index = !index;
                        str.notify();
                    }else{
                        try{

                        str.wait();
                        }catch (Exception e){

                        }

                    }


                }


            }

        }
    }


    static class B implements Runnable{

        int i = 0;
        @Override
        public void run() {

            synchronized(str){

                while(i < 26){

                    if(!index){

                        System.out.println((char)(str.charAt(0)+i++));
                        index = !index;
                        str.notify();
                    }else{

                        try{

                           str.wait();
                        }catch (Exception e){

                        }

                    }

                }

            }

        }
    }

    public static void main(String[] args) {

        Thread th1 = new Thread(new A());
        Thread th2 = new Thread(new B());
        th1.start();
        th2.start();
    }


}

方法二:ReentrantLock condition

package com.hnist.lzn.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Condition {

    /*
    写两个线程,一个线程打印 1~52,另一个线程打印字母A-Z。
    打印顺序为12A34B56C……5152Z。
    用Conditon 实现
     */
    static Lock lock = new ReentrantLock();
    static java.util.concurrent.locks.Condition printcondi = lock.newCondition();
    static java.util.concurrent.locks.Condition printChar = lock.newCondition();
    static boolean index = true;
    static String str = "A";


    static class PrintNumber implements Runnable{

        int i = 1;
        @Override
        public void run() {

            lock.lock();
            try{

                while(i <= 52)
                {
                    if(index){

                        System.out.println(i++);
                        System.out.println(i++);
                        index = !index;
                        printChar.signal();
                    }else{

                        printcondi.await();

                    }


                }

            }catch (Exception e){

            }finally {
                lock.unlock();
            }

        }
    }

    static class PrintChar implements Runnable{

        int i = 0;
        @Override
        public void run() {

            lock.lock();
            try{

              while(i < 26){

                 if(!index){

                     System.out.println((char)(str.charAt(0)+i));
                     i++;
                     index = !index;
                     printcondi.signal();
                 }else{

                     printChar.await();
                 }
              }

            }catch (Exception e){

            }finally {
                lock.unlock();
            }

        }
    }

    public static void main(String[] args) {

        Thread th1 = new Thread(new PrintNumber());
        Thread th2 = new Thread(new PrintChar());
        th1.start();
        th2.start();

    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值