Multithreading learning -synchronized

Why Multithreading?

Data problems when multiple threads on the same resource (shared resource) changes caused by the operation

Multithreading solve the problem?

In block race shared resource single thread mode access operation

How to achieve single-threaded asked race resources?

Generally use the locking mechanism

What is the lock re-entry?

A thread acquired the lock of the object after the lock is also acquired in the object code block other race, until the thread releases the object, other threads can access the race of the object code block 

More than a few simple questions is my personal understanding is that some of this to say about my experience of synchronized genlock

synchroinzed Java lock key implicitly when it modified a method or block of code time, to ensure that only one thread at a time up to the implementation of the method or block of code

Core features: The lock is the object  

Demo1 lock verification synchroinzed region of race

public class ThreadDemo {
    /**
     * 竞态方法 method1
     *
     * */
    public  synchronized void method1(){
        try{
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getName()+"  over");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 普通方法 method2
     *
     * */
    public   void method2(){
        System.out.println(Thread.currentThread().getName()+"  normal  method");
    }

    /**
     * 竞态方法 method3
     *
     * */
    public  synchronized void method3(){
        try{
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getName()+"  over");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static  void main(String[] args){
        final ThreadDemo threadDemo = new ThreadDemo();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                threadDemo.method1();
            }
        },"t1");
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                threadDemo.method2();
                threadDemo.method3();
            }
        },"t2");

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

Results of the

t1
t2  normal  method
t1  over
t2
t2  over

It can be seen t1, t2 objects threadDemo two threads operate simultaneously performed, although the state of t1 threadDemo acquired lock, but does not affect other threads to access the non-race process, was blocked for all other threads that object race access to state resources, until he is finished, release the lock. It is to be emphasized that the present embodiment is of threadDemo lock object, but the object scope. Verify different objects placed next




  









Guess you like

Origin blog.csdn.net/leeahuamsg/article/details/79455151