java multithreading safe

 

What is the thread-safety issues

When multiple threads share the same member variable or static variable  . Do write operations will certainly receive interference from other threads, leading to a phenomenon called the data in question thread-safety issues (do read the operating data collision problem does not occur)

  Example:

  

class threadTrain1 implements Runnable {

    // 重写run()方法
    private static int count = 100;

    public void run() {
        while (count > 0) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
            sale();
        }
    }

    public void sale() {

            if (count > 0) {
                System.out.println(Thread.currentThread().getName() + " 出售 第 " + (100 - count + 1) + " 张票");
                count--;
            }
        
    }

}

public class thread_dame_1 {

    public static void main(String[] args) {

        threadTrain1 thread1 = new threadTrain1();
        Thread t1 = new Thread(thread1, " 一号窗口 ");
        Thread t2 = new Thread(thread1, " 二号窗口 ");

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

    }

}

 

  Results:

 

 

Repeat part ticket sale

Data conflicts occur when multiple threads share a member variable

Solve thread safety issues

Synchronous or synchronized using the lock (lock) to address security thread between the use of multiple threads

What needs to lock

Thread Synchronization ensures that only one thread access

There must be two or more threads, synchronization needs to happen

When multiple threads want to synchronize must use the same lock, a thread synchronization shared resources without interference from other threads

Synchronization code

the synchronized (same object (lock)) // This object can be any object used to lock this 
{
                 // thread codes that may occur conflicts 
            }

Example: 

    public void sale() {
        synchronized (this) {
            if (count > 0) {
                System.out.println(Thread.currentThread().getName() + " 出售 第 " + (100 - count + 1) + " 张票");
                count--;
            }
        }
    }

Or:

(Modification of the method the synchronization function)

    public synchronized void sale() {
        
            if (count > 0) {
                System.out.println(Thread.currentThread().getName() + " 出售 第 " + (100 - count + 1) + " 张票");
                count--;
            }

    }

result: 

 

 

 

 Static synchronous function

Static synchronous function is modified static synchronous function

Static can not lock this function using static synchronous byte code belongs to the static function

Bytecode can  getClass () method to obtain directly or  class name .class  represents

Example: 

    public static void sale() {
        synchronized (threadTrain1.class) {
            if (count > 0) {
                System.out.println(Thread.currentThread().getName() + " 出售 第 " + (100 - count + 1) + " 张票");
                count--;
            }
        }
    }

多线程三大特性

原子性 , 可见性 , 有序性

原子性 : 保证数据一致, 保证线程安全

可见性 : 多个线程访问同一个变量时 , 一个线程修改了值 , 其他线程能立即得到修改的值,若2个线程不在同一个cpu , 那么线程a 改变了值 , b线程使用还是原来的值 , a 修改的值 b 没有看到 这就是可见性

有序性 : 程序执行的顺序按照先后顺序执行 如之前的 join() 

 

 

 

Guess you like

Origin www.cnblogs.com/chenkuijiang/p/12181574.html
Recommended