java 线程安全

本秂侑毒 提交于 2020-04-04 09:21:12

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)\Java\jdk1.8.0_71\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.3\lib\idea_rt.jar=60091:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\charsets.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\deploy.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\access-bridge-32.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\cldrdata.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\dnsns.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\jaccess.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\jfxrt.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\localedata.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\nashorn.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\sunec.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\sunjce_provider.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\sunmscapi.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\sunpkcs11.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\ext\zipfs.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\javaws.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\jce.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\jfr.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\jfxswt.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\jsse.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\management-agent.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\plugin.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\resources.jar;D:\Program Files (x86)\Java\jdk1.8.0_71\jre\lib\rt.jar;D:\users\ligenyun\IdeaProjects\Test\out\production\day1" com.jack.test2.main
Thread-0卖票中:第1张票
Thread-2卖票中:第2张票
Thread-2卖票中:第3张票
Thread-2卖票中:第4张票
Thread-2卖票中:第5张票

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!