Advertisement
Guest User

YOLO

a guest
Feb 8th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. synchronized public static void threadFunc() {
  2. newLine = true;
  3. x = 0; y = 0;
  4.  
  5. Thread t1 = new Thread(){
  6. @Override
  7. public void run() {
  8. while (x == 0) {
  9. try {
  10. Thread.sleep((long)0.01);
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15.  
  16. System.out.print("a");
  17. y = 1;
  18. y = 0;
  19. System.out.print("d");
  20. y = 1;
  21.  
  22. synchronized(this) {
  23. if (newLine) {
  24. System.out.print("\n");
  25. newLine = false;
  26. }
  27. }
  28. }
  29. };
  30.  
  31. Thread t2 = new Thread(){
  32. @Override
  33. public void run() {
  34. System.out.print("b");
  35. x = 1;
  36.  
  37. while (y == 0) {
  38. try {
  39. Thread.sleep((long)0.01);
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45. System.out.print("c");
  46.  
  47. synchronized(this) {
  48. if (newLine) {
  49. System.out.print("\n");
  50. newLine = false;
  51. }
  52. }
  53. }
  54. };
  55.  
  56. t1.start();
  57. t2.start();
  58.  
  59. try {
  60. t1.join();
  61. t2.join();
  62. } catch (InterruptedException e) {
  63. e.printStackTrace();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement