java 线程安全

Posted biind

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 线程安全相关的知识,希望对你有一定的参考价值。

1. synchronized

2.Lock

package com.jack.test2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyRunnable3 implements Runnable {
    int len = 5;
    int tikets = len;
    Object obj = new Object();
    Lock l = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            l.lock();
            try {
                if (tikets > 0) {

                    System.out.println(Thread.currentThread().getName() + "卖票中:第" + (len + 1 - tikets) + "张票");

                    tikets--;
                }
            } finally {
                l.unlock();
            }

        }
    }
}
package com.jack.test2;

import static java.lang.Thread.sleep;

public class MyRunnable implements Runnable {
    int len = 5;
    int tikets = len;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (tikets > 0) {

                    System.out.println(Thread.currentThread().getName() + "卖票中:第" + (len + 1 - tikets) + "张票");

                    tikets--;
                }
            }
        }
    }
}
package com.jack.test2;

import static java.lang.Thread.sleep;

public class MyRunnable2 implements Runnable {
    static int len = 5;
    static int tikets = len;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            dosome();
        }
    }

    public static synchronized void dosome() {
        if (tikets > 0) {
            System.out.println(Thread.currentThread().getName() + "卖票中:第" + (len + 1 - tikets) + "张票");
            tikets--;
        }
    }

}

 

"D:Program Files (x86)Javajdk1.8.0_71injava.exe" "-javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.3libidea_rt.jar=60091:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.3in" -Dfile.encoding=UTF-8 -classpath "D:Program Files (x86)Javajdk1.8.0_71jrelibcharsets.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibdeploy.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextaccess-bridge-32.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextcldrdata.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextdnsns.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextjaccess.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextjfxrt.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextlocaledata.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibext
ashorn.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextsunec.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextsunjce_provider.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextsunmscapi.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextsunpkcs11.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibextzipfs.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibjavaws.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibjce.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibjfr.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibjfxswt.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibjsse.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibmanagement-agent.jar;D:Program Files (x86)Javajdk1.8.0_71jrelibplugin.jar;D:Program Files (x86)Javajdk1.8.0_71jrelib
esources.jar;D:Program Files (x86)Javajdk1.8.0_71jrelib
t.jar;D:usersligenyunIdeaProjectsTestoutproductionday1" com.jack.test2.main
Thread-0卖票中:第1张票
Thread-2卖票中:第2张票
Thread-2卖票中:第3张票
Thread-2卖票中:第4张票
Thread-2卖票中:第5张票

 

以上是关于java 线程安全的主要内容,如果未能解决你的问题,请参考以下文章

怎样去写线程安全的代码(Java)

怎样去写线程安全的代码(Java)

Java线程安全问题代码实现

java是线程安全的吗

HashMap 和 ConcurrentHashMap 的区别

Java线程 — 线程同步及安全问题