Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class VolatileTest1 {
- private static boolean isOver = false;
- public static void main(String[] args) {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- int a=0;
- while (!isOver){
- a=5;
- } ;
- /* while (true){
- a=5;
- if(!isOver)break;
- } ;*/
- System.out.println(a);
- }
- });
- thread.start();
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- isOver = true;
- }
- }
- public class VolatileTest2 {
- private static boolean isOver = false;
- public static void main(String[] args) {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- int a=0;
- /* while (!isOver){
- a=5;
- } ;*/
- while (true){
- a=5;
- if(!isOver)break;
- } ;
- System.out.println(a);
- }
- });
- thread.start();
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- isOver = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement