ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线程交替输出A1B2C3.........Z26

2022-09-15 22:35:20  阅读:439  来源: 互联网

标签:Z26 synchronized Thread int Object A1B2C3 线程 countDownLatch new


为了完成作业。。。。直接贴代码,复制可直接运行

第一版本

public  static void main(String[] args){
    Object o=new Object();
    Thread t1=new Thread(()->{
        synchronized (o){ //wait 必须在有锁的情况下使用
            for (int i=0;i<26;i++){
                System.out.print((char) (i + 'A'));
                o.notify(); //唤醒其他线程
                try {
                    o.wait(); //自身等待 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            o.notify(); //必不可少,不然不会结束
        }
    });

    Thread t2=new Thread(()->{
        synchronized (o){
            for (int i=1;i<=26;i++){
                System.out.print(i);
                o.notify();
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            o.notify();
        }
    });
    t1.start();
    t2.start();
}

  


经过数次尝试,会出现1A2B3C 的情况;
第二版本
public  static void main(String[] args){
    Object o=new Object();
    CountDownLatch countDownLatch=new CountDownLatch(1); //门闩 1表示等待的数量
    Thread t1=new Thread(()->{
        countDownLatch.countDown();  //唤醒 把countDownLatch的值减1,减到0,就唤醒所有等待在这个countDownLatch上的线程
        synchronized (o){
            for (int i=0;i<26;i++){
                System.out.print((char) (i + 'A'));
                o.notify();
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            o.notify();
        }

    });

    Thread t2=new Thread(()->{
        try {
            countDownLatch.await();//进入等待状态
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (o){
            for (int i=1;i<=26;i++){
                System.out.print(i);
                o.notify();
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            o.notify();
        }
    });

    t1.start();
    t2.start();

}

  

第三种
public static void main(String[] args) {

    t1 = new Thread(() -> {
        for (int i = 0; i < 26; i++) {
            System.out.print((char) (i + 'A'));
            LockSupport.unpark(t2); //唤醒t2 
            LockSupport.park();//暂停当前线程
        }
    });

    t2 = new Thread(() -> {
        for (int i = 1; i <= 26; i++) {
            LockSupport.park();   //这个一定要在打印前暂停
            System.out.print(i); 
            LockSupport.unpark(t1);
        }
    });

    t1.start();
    t2.start();

}

  

 


TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:Z26,synchronized,Thread,int,Object,A1B2C3,线程,countDownLatch,new
来源: https://www.cnblogs.com/count0/p/16698090.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有