java concurrent learning - Thread Tools Chapter VII of the JDK

一、ThreadLocal

  ThreadLocal class is used to isolate the object using multiple threads, in order to transfer the ThreadLocal class generic object is to isolate and simple: if we create an object in the main thread, and the need to give the following multi-threaded task pass this object, if this object is passed to the ThreadLocal, each thread gets then the object is independent, will not be changed by changing the other threads.

  ThreadLocal in a total of three common methods:

  

  get () method: Get ThreadLocal value associated with the current thread. 

 

  set (T value): Set ThreadLocal value associated with the current thread.

  

  the initialValue (): Set ThreadLocal initial value associated with the current thread.

  

  Let's look at a Liezi, create two threads, two threads together using an object, we have to observe the value of this object's value as well as ThreadLocal in this object:

  User object class:

public  class the User { 

    int num; 

    public the User ( int num) {
         the this .num = num; 
    } 

    / ** 
     * we only get method, and each have their plus 1 acquires num 
     * 
     * using the acquired synchronized to ensure num getNum is thread safe 
     * @return 
     * / 
    the synchronized  public  int getNum () {
         return NUM ++ ; 
    } 

}

  Threaded task categories:

public  class ThreadlocaDemo the extends the Thread { 

    User User; 

    ThreadlocaDemo (User User) { 
        the this the .user = User; 
    } 

    / ** 
     * Create a ThreadLocal object for its generic incoming User 
     * / 
    ThreadLocal <User> = userLoacl new new ThreadLocal < the User> () {
         / ** 
         * initialization method, the user sets the initial value of 
         * / 
        @Override 
        protected the User the initialValue () { 
            the User user = new new the User (10 );
             return user; 
        } 
    }; 


    @Override
     public  void RUN () { 

        for ( int I =. 1; I <. 3; I ++ ) {
             // break 
            the try { 
                the Thread.sleep ( 100 ); 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            } 
            // Print values acquired user 
            System.out.println (getName () + "the value of the user object" + user.getNum ());
             // print the user object ThreadLocal value 
            System.out.println (getName () + " ThreadLocal values of user objects "+ userLoacl.get () getNum ());. 

        } 

    } 

}

  Test categories:

public  class the Main { 

    public  static  void main (String [] args) {
         // create a common object 
        the User User = new new the User (10 );
         // creates two threads task 
        ThreadlocaDemo threadlocaDemo1 = new new ThreadlocaDemo (User); 
        ThreadlocaDemo threadlocaDemo2 = new new ThreadlocaDemo (User); 
        threadlocaDemo1.setName ( "thread 1 which is:" ); 
        threadlocaDemo2.setName ( "thread 2 which is:" ); 

        threadlocaDemo1.start (); 
        threadlocaDemo2.start (); 
    } 
}

Run results:

 

 According to operating results, we can clearly see, user ThreadLocal object is isolated, the outside user object is not isolation, is both threads were modified.

 

  

Guess you like

Origin www.cnblogs.com/daijiting/p/11585544.html