Guest User

Untitled

a guest
Oct 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. AN an= new AN(false);
  4.  
  5. Thread t1=new Thread(new Runnable() {
  6.  
  7. @Override
  8. public void run() {
  9.  
  10. try {
  11.  
  12. an.Alpha();
  13. } catch (InterruptedException e) {
  14. // TODO Auto-generated catch block
  15. e.printStackTrace();
  16. }
  17. }
  18. });
  19.  
  20.  
  21. Thread t2= new Thread(new Runnable() {
  22.  
  23. @Override
  24. public void run() {
  25. try {
  26. an.numbers();
  27. } catch (InterruptedException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31.  
  32. }
  33. });
  34.  
  35.  
  36. t1.start();
  37. t2.start();
  38. }
  39.  
  40. boolean flag;
  41.  
  42. AN(boolean flag)
  43. {
  44. this.flag=flag;
  45. }
  46. synchronized void Alpha() throws InterruptedException
  47. {
  48. if(flag==false)
  49. {
  50. for(char i='A'; i<='Z';i++)
  51. {
  52. System.out.println(+i);
  53. notifyAll();
  54. flag=true;
  55. }
  56. }
  57. else
  58. {
  59. wait();
  60. }
  61.  
  62. }
  63.  
  64. synchronized void numbers() throws InterruptedException
  65. {
  66. if(flag==true)
  67. {
  68. for(int i=1;i<=26;i++)
  69. {
  70. System.out.println(+i);
  71. notifyAll();
  72. flag=false;
  73. }
  74. }
  75. else
  76. {
  77. wait();
  78. }
  79. }
Add Comment
Please, Sign In to add comment