Multithreading (2) Multithreading Safety and Synchronization

One, the environment

  idea

2. What is thread safety and why there is thread safety

Thread safety issues arise when multiple threads access a shared resource at the same time (usually queries do not occur)

3. Examples

If I now want to talk about a number of cycles plus one, and finally increase to 1000. But it needs to use 5 threads to add

 

class Count implements Runnable{
    private int count=0;

    public void run() {
        while (count<=1000){
            count=count+1;
            System.out.println(Thread.currentThread().getName()+",count:"+count);
        }

    }
}
public class ThreadTest {
    public static void main(String[] args) {
        Count  count=new Count();
        Thread t1=new Thread(count,"线程一");
        Thread t2=new Thread(count,"线程二");
        Thread t3=new Thread(count,"线程三");
        Thread t4=new Thread(count,"线程四");
        Thread t5=new Thread(count,"线程五");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }

}

result:

The code shows: it will increase to 1000 and the loop will end, so why does 1001 appear! !

Since the multi-threading is increasing now, it is possible that when the count increases to 999, two threads enter the while loop at the same time, and then it increases twice in a row.

 

So how to solve it! ! !

4. Use locks to solve

4.1 Synchronized code blocks

class Count implements Runnable{
    private volatile int  count=0;
    private static Object oj = new Object();

    public  void run() {
         while (count<=1000 ){
             synchronized (oj) { // oj is the lock object can be any object or this 
                if (count<=1000 ) {
                    count = count + 1;
                    System.out.println(Thread.currentThread().getName() + ",count:" + count);
                }
            }
        }

    }

}
public class ThreadTest {
    public static void main(String[] args) {
        Count  count=new Count();
        Thread t1=new Thread(count,"线程一");
        Thread t2=new Thread(count,"线程二");
        Thread t3=new Thread(count,"线程三");
        Thread t4=new Thread(count,"线程四");
        Thread t5=new Thread(count,"线程五");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }

}

4.2 Using Synchronous Methods

class Count implements Runnable{
    private volatile int  count=0;
    private static Object oj = new Object();

    public void run() {
        while (count<=1000){
            
            sole();
               
            }
        

    }
    public  synchronized  void sole(){ // The lock object of this lock is this 
        if (count<=1000 ) {
            count = count + 1;
            System.out.println(Thread.currentThread().getName() + ",count:" + count);
        }
    }
}
public class ThreadTest {
    public static void main(String[] args) {
        Count  count=new Count();
        Thread t1=new Thread(count,"线程一");
        Thread t2=new Thread(count,"线程二");
        Thread t3=new Thread(count,"线程三");
        Thread t4=new Thread(count,"线程四");
        Thread t5=new Thread(count,"线程五");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325505923&siteId=291194637