码迷,mamicode.com
首页 > 编程语言 > 详细

java 线程安全

时间:2020-04-04 09:37:35      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:gem   java   ati   enc   some   span   out   ike   jce   

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张票

 

java 线程安全

标签:gem   java   ati   enc   some   span   out   ike   jce   

原文地址:https://www.cnblogs.com/ligenyun/p/12630271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!