Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public class VolatileTest1 {
  2. private static boolean isOver = false;
  3.  
  4. public static void main(String[] args) {
  5. Thread thread = new Thread(new Runnable() {
  6. @Override
  7. public void run() {
  8. int a=0;
  9.  
  10. while (!isOver){
  11. a=5;
  12. } ;
  13.  
  14. /* while (true){
  15. a=5;
  16. if(!isOver)break;
  17. } ;*/
  18. System.out.println(a);
  19. }
  20. });
  21. thread.start();
  22. try {
  23. Thread.sleep(100);
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. isOver = true;
  28. }
  29.  
  30. }
  31.  
  32. public class VolatileTest2 {
  33. private static boolean isOver = false;
  34.  
  35. public static void main(String[] args) {
  36. Thread thread = new Thread(new Runnable() {
  37. @Override
  38. public void run() {
  39. int a=0;
  40.  
  41. /* while (!isOver){
  42. a=5;
  43. } ;*/
  44.  
  45. while (true){
  46. a=5;
  47. if(!isOver)break;
  48. } ;
  49. System.out.println(a);
  50. }
  51. });
  52. thread.start();
  53. try {
  54. Thread.sleep(100);
  55. } catch (InterruptedException e) {
  56. e.printStackTrace();
  57. }
  58. isOver = true;
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement