Java线程(练习题)
创始人
2024-11-14 15:35:00
0

Exercise

创建三个线程:一个线程打印 100个A,一个线程打印 100 个 B ,一个线程打印 100个C 输出效果:ABC ABC ABC…交替打印

package com.kane.exercise01;  public class PrintABC implements Runnable {      private static final Object lock = new Object();     private static int state = 0;      private String str;     private int num;      public PrintABC(String str, int num) {         this.str = str;         this.num = num;     }      @Override     public void run() {         for (int i = 0; i < 100; i++) {             synchronized (lock) {                 while (state % 3 != num) {                     try {                         lock.wait();                     } catch (InterruptedException e) {                         e.printStackTrace();                     }                 }                 System.out.print(str);                 state++;                 lock.notifyAll(); // 唤醒其他线程             }             if(state%3 == 0) {                 System.out.println("");             }         }     } }   
package com.kane.exercise01;  public class Demo1 {     public static void main(String[] args) {         Thread t1 = new Thread(new PrintABC("A",0));         Thread t2 = new Thread(new PrintABC("B",1));         Thread t3 = new Thread(new PrintABC("C",2));         t1.start();         t2.start();         t3.start();     } }  

编写两个线程,一个 线程打印1-52,另一个线程打印字母A-Z打印顺序是12A34B…,即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印

package com.kane.exercise02;  import java.util.concurrent.Semaphore;  public class Demo {     private static final Semaphore Numbersemaphore = new Semaphore(1);     private static final Semaphore Dansemaphore = new Semaphore(0);      public static void main(String[] args) {         Thread t1 = new Thread(new NumberPrint());         Thread t2 = new Thread(new DanPrint());         t1.start();         t2.start();     }      static class NumberPrint implements Runnable {          @Override         public void run() {             for (int i = 1; i < 53; i++) {                 try {                     Numbersemaphore.acquire();                     System.out.print(i + " ");                     if (i%2== 0) {                         Dansemaphore.release();                     } else {                         Numbersemaphore.release();                     }                 } catch (InterruptedException e) {                     throw new RuntimeException(e);                 }             }         }     }     static class DanPrint implements Runnable {          @Override         public void run() {             for (char c = 'A'; c <= 'Z'; c++) {                 try {                     Dansemaphore.acquire();                     System.out.print(c);                     System.out.println("");                     Numbersemaphore.release();                 } catch (InterruptedException e) {                     Thread.currentThread().interrupt();                 }             }         }     }  }  

相关内容

热门资讯

玩家必备教程(德州辅助神器软件... 《玩家必备教程(德州辅助神器软件)德州线上ai辅助神器(先前有挂)-哔哩哔哩》 德州线上ai辅助神器...
推荐十款"aa po... 推荐十款"aa poker有外挂吗"aapoker真的有猫腻吗(总是真的是有挂)-哔哩哔哩1、每一步...
Wpk最新黑科技!wpk专用辅... Wpk最新黑科技!wpk专用辅助程序(WPK ai辅助)原生是真的有挂(2026已更新)(哔哩哔哩)...
wepoke辅助插件!WePo... wepoke辅助插件!WePoKe有挂吗(WePoKe黑科技)都是存在有挂(有挂头条)-哔哩哔哩一、...
今日科普(德州ai软件购买)德... 今日科普(德州ai软件购买)德州ai辅助软件(竟然是真的有挂)-哔哩哔哩德州ai辅助软件平台为新手玩...
科技分享"aapok... 科技分享"aapokerai辅助"aapoker有外挂(好像真的是有挂)-哔哩哔哩1、许多玩家不知道...
wpK最新黑科技!wpk免费赛... 1、wpK最新黑科技!wpk免费赛规则(WPK ai辅助)起初存在有挂(2022已更新)(哔哩哔哩)...
wepoke辅助插件!wepo... wepoke辅助插件!wepoke辅助使用教程(WePoKe黑科技)都是是真的有挂(有挂分析)-哔哩...
实测教程(德州ai辅助软件)德... 实测教程(德州ai辅助软件)德州ai机器人(一向是有挂)-哔哩哔哩是一款可以让一直输的玩家,快速成为...
1.9分钟了解"aa... 1.9分钟了解"aa poker有外挂吗"aapoker外挂实测(原来真的有挂)-哔哩哔哩1、游戏颠...