java - volatile

volatile data between the main threads for visible

Different thread operations to the same object, the object will be first to own a copy of memory to run and then put it back over the operation.

If two threads operate with objects, object operation between the two is actually not the same, but each get a copy of main memory.

The volatile modified object properties, will ensure its visibility, it will be synchronized to the main memory when using this property, the other threads to know.

volatile ensure the consistency of object properties, but does not guarantee atomicity. You may have to change here in half, the other side has been changed again. . .

 

A inside questions:

It relates to a container, wherein the thread 1 is added to 10 digits, two threads monitoring thread 1 is stopped when the added 5 digits.

import java.util.ArrayList;

public class Vol {
    public volatile ArrayList<Integer> arr;

    public Vol(){
        arr = new ArrayList<Integer>();
    }

    public void add(int e){
        arr.add(e);
    }

    public int size(){
        return arr.size();
    }


}

 

main

public class test {
    public static void main(String[] args){
        Vol v =new Vol();

        Thread t1 = new Thread(){

            public int stop = 0;

            @Override
            public void run() {
                for(int i = 1; i <= 10; i++){
                    v.add(i);
                    System.out.println(i + "被加入了");

                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {
                while(true){
                    if(v.size() == 5){
                        try {
                            System.out.println("线程2停止");
                            break;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
        };

        t1.start();
        t2.start();
    }
}

result:

1 is added to the
2 was added to the
3 is added to the
4 were added to the
5 was added to the
thread 2 is stopped
6 was added to the
7 was added to the
8 was added to
9 were added to
10 is added to the

 

 

 

 

If altered, changed when the thread 1 add five numbers, thread 1 stop

With Thread.stop expired - - now want to stop a thread from the outside it is best to set a switch, external control switch, internal thread stops

public class test {

    public static class Thread1 extends Thread{
        public int stop = 0;
    }


    public static void main(String[] args){
        Vol v =new Vol();

        Thread1 t1 = new Thread1(){
            @Override
            public void run() {
                int i = 0;
                while(stop == 0){
                    i++;
                    v.add(i);
                    System.out.println(i + "被加入了");
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {
                while(true){
                    if(v.size() == 5){
                            try {
                                System.out.println("线程1停止");
                                t1.stop = 1;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            break;
                    }
                }
            }
        };

        t1.start();
        t2.start();
    }
}

 

 

You can try to remove volatile - - will always go, because t1 and t2 see objects v is in fact not the same, it is equal to their respective dry.

Guess you like

Origin www.cnblogs.com/clamp7724/p/11949085.html