TwoThe

Untitled

Oct 26th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. public class Alphabets {
  2.  
  3.   public static void main(String[] args) throws Throwable {
  4.     final AN an = new AN(false);
  5.     new Thread(an::Alpha).start();
  6.     new Thread(an::numbers).start();
  7.   }
  8. }
  9.  
  10. class AN {
  11.  
  12.   boolean flag;
  13.  
  14.   AN(boolean flag) {
  15.     this.flag = flag;
  16.   }
  17.  
  18.   synchronized void Alpha() {
  19.     for (char i = 'A'; i <= 'Z'; i++) {
  20.       if (flag == false) {
  21.         System.out.print(+i + " ");
  22.         notifyAll();
  23.         flag = true;
  24.       }
  25.       if (i != 'Z') {
  26.         try {
  27.           wait();
  28.         } catch (InterruptedException ex) {
  29.         }
  30.       }
  31.     }
  32.   }
  33.  
  34.   synchronized void numbers() {
  35.     for (int i = 1; i <= 26; i++) {
  36.       if (flag == true) {
  37.         System.out.print(+i + " ");
  38.         notifyAll();
  39.         flag = false;
  40.       }
  41.       if (i != 26) {
  42.         try {
  43.           wait();
  44.         } catch (InterruptedException ex) {
  45.         }
  46.       }
  47.     }
  48.   }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment